Esempio n. 1
0
static jboolean OSNetworkSystem_selectImpl(JNIEnv* env, jclass,
        jobjectArray readFDArray, jobjectArray writeFDArray, jint countReadC,
        jint countWriteC, jintArray outFlags, jlong timeoutMs) {

    // Initialize the fd_sets.
    int maxFd = -1;
    fd_set readFds;
    fd_set writeFds;
    FD_ZERO(&readFds);
    FD_ZERO(&writeFds);
    bool initialized = initFdSet(env, readFDArray, countReadC, &readFds, &maxFd) &&
                       initFdSet(env, writeFDArray, countWriteC, &writeFds, &maxFd);
    if (!initialized) {
        return -1;
    }

    // Initialize the timeout, if any.
    timeval tv;
    timeval* tvp = NULL;
    if (timeoutMs >= 0) {
        tv = toTimeval(timeoutMs);
        tvp = &tv;
    }

    // Perform the select.
    int result = select(maxFd + 1, &readFds, &writeFds, NULL, tvp);
    if (result == 0) {
        // Timeout.
        return JNI_FALSE;
    } else if (result == -1) {
        // Error.
        if (errno == EINTR) {
            return JNI_FALSE;
        } else {
            jniThrowSocketException(env, errno);
            return JNI_FALSE;
        }
    }

    // Translate the result into the int[] we're supposed to fill in.
    ScopedIntArrayRW flagArray(env, outFlags);
    if (flagArray.get() == NULL) {
        return JNI_FALSE;
    }
    return translateFdSet(env, readFDArray, countReadC, readFds, flagArray.get(), 0, SOCKET_OP_READ) &&
            translateFdSet(env, writeFDArray, countWriteC, writeFds, flagArray.get(), countReadC, SOCKET_OP_WRITE);
}
Esempio n. 2
0
void SelectManager::loop()
{
    int ret;
    char buf[1];

    for (; !m_isStop;)
    {
        initFdSet();
        ret = select(m_maxFd + 1, &m_readSet, &m_writeSet, NULL, NULL);
        if (ret == -1)
        {
            if (m_isStop) 
            {
                INFOLOG("recv exit signal, exit loop");
            }else
            {
                INFOLOG("EINTR");
            }

            ERRORLOG1("select err, %s", strerror(errno));

            break;
        }
        if (0 == ret)
        {
            ERRORLOG("select return 0");

            break;
        }

        if (FD_ISSET(m_recvNotifyFd, &m_readSet))
        {
            if (read(m_recvNotifyFd, buf, sizeof(buf)) == -1)
            {
                ERRORLOG1("write err, %s", strerror(errno));
            }
            if (0 == *buf)
            {
                INFOLOG("recv exit notify, exit loop");
            }
            if (ret == 1) continue;
        }
        handleFdSet();
    }
    m_isStop = 2;
}