Пример #1
0
/**
 * See pcsl_network.h for definition.
 */
void pcsl_add_network_notifier(
    void *handle,
    int event) {
    switch (event) {
    case PCSL_NET_CHECK_READ:
    case PCSL_NET_CHECK_ACCEPT:
        na_register_for_read(handle);
        break;
    case PCSL_NET_CHECK_WRITE:
        na_register_for_write(handle);
        break;
    case PCSL_NET_CHECK_EXCEPTION:
        /* need revisit */
        break;
    }
}
Пример #2
0
/**
 * See pcsl_socket.h for definition.
 */
int pcsl_socket_read_start(
    void *handle,
    unsigned char *pData,
    int len,
    int *pBytesRead,
    void **pContext)
{
    int status;

    status = pcsl_socket_read_common(handle, pData, len, pBytesRead);

    if (status == PCSL_NET_WOULDBLOCK) {
        na_register_for_read(handle);
        *pContext = NULL;
    }

    return status;
}
Пример #3
0
/**
 * See pcsl_socket.h for definition.
 */
int pcsl_socket_read_finish(
    void *handle,
    unsigned char *pData,
    int len,
    int *pBytesRead,
    void *context)
{
    int status;

    status = pcsl_socket_read_common(handle, pData, len, pBytesRead);

    if (status == PCSL_NET_WOULDBLOCK) {
        na_register_for_read(handle);
    } else {
        na_unregister_for_read(handle);
    }

    return status;
}
/**
 * Common functionality for accept start and finish
 */
static int pcsl_serversocket_accept_common(
    void *handle,
    void **pConnectionHandle,
    void **pContext)
{
    int connfd;
    int flags;
    struct sockaddr_in sa;
    int saLen = sizeof (sa);
    int listenfd = na_get_fd(handle);

    connfd = accept(listenfd, (struct sockaddr *)&sa, (socklen_t *)&saLen);

    if (connfd == SOCKET_ERROR) {
        if (errno == EWOULDBLOCK || errno == ECONNABORTED) {
            /*
             * The "listenfd" is marked as non-blocking and no connections 
             * are present to be accepted.
             */
            na_register_for_read((void *)handle);
            *pContext = NULL;
            return PCSL_NET_WOULDBLOCK;
        } else {
            na_unregister_for_read((void *)handle);
            return PCSL_NET_IOERROR;
        }
    } else {
        na_unregister_for_read((void *)handle);
        /*
         * Linux accept does _not_ inherit socket flags like O_NONBLOCK.
         * So, irrespective of the blocking or non-blockimg mode of listenfd,
         * connfd, must be set to non-blocking mode; otherwise VM will 
         * be blocked for any I/O operation on this new socket descriptor.
         */
        flags = fcntl(connfd, F_GETFL, 0);
        fcntl(connfd, F_SETFL, flags | O_NONBLOCK);

        *pConnectionHandle = na_create(connfd);

        return PCSL_NET_SUCCESS;
    }
}
Пример #5
0
/**
 * See pcsl_datagram.h for definition.
 */
int pcsl_datagram_read_start(
    void *handle, 
    unsigned char *pAddress,
    int *port, 
    char *buffer, 
    int length, 
    int *pBytesRead,
    void **pContext)
{
    int status;

    status = pcsl_datagram_read_common(handle, pAddress, port,
                buffer, length, pBytesRead);

    if (status == PCSL_NET_WOULDBLOCK) {
        na_register_for_read(handle);
        *pContext = NULL;
    }

    return status;
}