Search in sources :

Example 6 with StructPollfd

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

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)

Example 7 with StructPollfd

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

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 selectedOp = 0;
        if ((pollFd.revents & POLLIN) != 0) {
            selectedOp = ops & (OP_ACCEPT | OP_READ);
        } else if ((pollFd.revents & POLLOUT) != 0) {
            if (key.isConnected()) {
                selectedOp = ops & OP_WRITE;
            } else {
                selectedOp = ops & OP_CONNECT;
            }
        }
        if (selectedOp != 0) {
            boolean wasSelected = mutableSelectedKeys.contains(key);
            if (wasSelected && key.readyOps() != selectedOp) {
                key.setReadyOps(key.readyOps() | selectedOp);
                ++readyKeyCount;
            } else if (!wasSelected) {
                key.setReadyOps(selectedOp);
                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