Search in sources :

Example 1 with StructPollfd

use of libcore.io.StructPollfd in project XobotOS by xamarin.

the class SelectorImpl method setPollFd.

private void setPollFd(int i, FileDescriptor fd, int events, Object object) {
    StructPollfd pollFd = pollFds.get(i);
    pollFd.fd = fd;
    pollFd.events = (short) events;
    pollFd.userData = object;
}
Also used : StructPollfd(libcore.io.StructPollfd)

Example 2 with StructPollfd

use of libcore.io.StructPollfd in project robovm by robovm.

the class PlainSocketImpl method accept.

@Override
protected void accept(SocketImpl newImpl) throws IOException {
    if (usingSocks()) {
        ((PlainSocketImpl) newImpl).socksBind();
        ((PlainSocketImpl) newImpl).socksAccept();
        return;
    }
    try {
        // RovmVM note: accept() on Darwin does not honor the SO_RCVTIMEO
        // set using setSoTimeout() on blocking sockets. As a work around we 
        // do poll() if a timeout has been set followed by an accept().
        int timeout = (Integer) getOption(SO_TIMEOUT);
        int flags = Libcore.os.fcntlVoid(fd, F_GETFL);
        if (timeout > 0 && (flags & O_NONBLOCK) == 0) {
            StructPollfd pfd = new StructPollfd();
            pfd.fd = fd;
            pfd.events = (short) (POLLIN | POLLERR);
            StructPollfd[] pfds = new StructPollfd[] { pfd };
            long start = System.currentTimeMillis();
            long deadline = start + timeout;
            while (true) {
                try {
                    if (timeout <= 0 || Libcore.os.poll(pfds, timeout) == 0) {
                        throw new SocketTimeoutException("accept() timed out");
                    }
                    break;
                } catch (ErrnoException e) {
                    if (e.errno == EINTR) {
                        long now = System.currentTimeMillis();
                        timeout = (int) (deadline - now);
                    } else {
                        throw e;
                    }
                }
            }
        }
        InetSocketAddress peerAddress = new InetSocketAddress();
        FileDescriptor clientFd = Libcore.os.accept(fd, peerAddress);
        // TODO: we can't just set newImpl.fd to clientFd because a nio SocketChannel may
        // be sharing the FileDescriptor. http://b//4452981.
        newImpl.fd.setInt$(clientFd.getInt$());
        newImpl.address = peerAddress.getAddress();
        newImpl.port = peerAddress.getPort();
    } catch (ErrnoException errnoException) {
        if (errnoException.errno == EAGAIN) {
            throw new SocketTimeoutException(errnoException);
        }
        throw errnoException.rethrowAsSocketException();
    }
    // Reset the client's inherited read timeout to the Java-specified default of 0.
    newImpl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(0));
    newImpl.localport = IoBridge.getSocketLocalPort(newImpl.fd);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ErrnoException(libcore.io.ErrnoException) InetSocketAddress(java.net.InetSocketAddress) StructPollfd(libcore.io.StructPollfd) FileDescriptor(java.io.FileDescriptor)

Example 3 with StructPollfd

use of libcore.io.StructPollfd in project robovm by robovm.

the class SelectorImpl method setPollFd.

private void setPollFd(int i, FileDescriptor fd, int events, Object object) {
    StructPollfd pollFd = pollFds.get(i);
    pollFd.fd = fd;
    pollFd.events = (short) events;
    pollFd.userData = object;
}
Also used : StructPollfd(libcore.io.StructPollfd)

Example 4 with StructPollfd

use of libcore.io.StructPollfd in project j2objc by google.

the class SelectorImpl method setPollFd.

private void setPollFd(int i, FileDescriptor fd, int events, Object object) {
    StructPollfd pollFd = pollFds.get(i);
    pollFd.fd = fd;
    pollFd.events = (short) events;
    pollFd.userData = object;
}
Also used : StructPollfd(libcore.io.StructPollfd)

Example 5 with StructPollfd

use of libcore.io.StructPollfd in project j2objc by google.

the class SelectorImpl method processPollFds.

/**
     * Updates the key ready ops and selected key set.
     */
private int processPollFds() throws IOException {
    if (pollFds.get(0).revents == POLLIN) {
        // Read bytes from the wakeup pipe until the pipe is empty.
        byte[] buffer = new byte[8];
        while (IoBridge.read(wakeupIn, buffer, 0, 1) > 0) {
        }
    }
    int readyKeyCount = 0;
    for (int i = 1; i < pollFds.size(); ++i) {
        StructPollfd pollFd = pollFds.get(i);
        if (pollFd.revents == 0) {
            continue;
        }
        if (pollFd.fd == null) {
            break;
        }
        SelectionKeyImpl key = (SelectionKeyImpl) pollFd.userData;
        pollFd.fd = null;
        pollFd.userData = null;
        int ops = key.interestOpsNoCheck();
        int selectedOps = 0;
        if ((pollFd.revents & POLLHUP) != 0) {
            // If there was an error condition, we definitely want to wake listeners,
            // regardless of what they're waiting for. Failure is always interesting.
            selectedOps |= ops;
        }
        if ((pollFd.revents & POLLIN) != 0) {
            selectedOps |= ops & (OP_ACCEPT | OP_READ);
        }
        if ((pollFd.revents & POLLOUT) != 0) {
            if (key.isConnected()) {
                selectedOps |= ops & OP_WRITE;
            } else {
                selectedOps |= ops & OP_CONNECT;
            }
        }
        if (selectedOps != 0) {
            boolean wasSelected = mutableSelectedKeys.contains(key);
            if (wasSelected && key.readyOps() != selectedOps) {
                key.setReadyOps(key.readyOps() | selectedOps);
                ++readyKeyCount;
            } else if (!wasSelected) {
                key.setReadyOps(selectedOps);
                mutableSelectedKeys.add(key);
                ++readyKeyCount;
            }
        }
    }
    return readyKeyCount;
}
Also used : StructPollfd(libcore.io.StructPollfd)

Aggregations

StructPollfd (libcore.io.StructPollfd)7 FileDescriptor (java.io.FileDescriptor)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ErrnoException (libcore.io.ErrnoException)1