Example #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;
    }
}
/**
 * See pcsl_socket.h for definition.
 */
int pcsl_socket_write_finish(
    void *handle,
    char *pData,
    int len,
    int *pBytesWritten,
    void *context)
{
    int status;

    status = pcsl_socket_write_common(handle, pData, len, pBytesWritten);

    if (status == PCSL_NET_WOULDBLOCK) {
        na_register_for_write(handle);
    } else {
        na_unregister_for_write(handle);
    }
    return status;
}
/**
 * See pcsl_socket.h for definition.
 */
int pcsl_socket_write_start(
    void *handle,
    char *pData,
    int len,
    int *pBytesWritten,
    void **pContext)
{
    int status;

    status = pcsl_socket_write_common(handle, pData, len, pBytesWritten);

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

    return status;
}
Example #4
0
/**
 * See pcsl_datagram.h for definition.
 */
int pcsl_datagram_write_finish(
	void *handle,
    unsigned char *ipBytes,
	int port,
    char *buffer,
    int length,
	int *pBytesWritten,
	void *context)
{
    int status;

    status = pcsl_datagram_write_common(handle, ipBytes, port, buffer, 
                length, pBytesWritten);
    
    if (status == PCSL_NET_WOULDBLOCK) {
        na_register_for_write(handle);
    } else {        
        na_unregister_for_write(handle);
    }
    return status;
}
/**
 * See pcsl_socket.h for definition.
 */
int pcsl_socket_open_start(
    unsigned char *ipBytes,
    int port,
    void **pHandle,
    void **pContext)
{
    int truebuf = 1;
    int fd = -1;
    struct sockaddr_in addr;
    int status;
    int flags;

    fd = socket(AF_INET, SOCK_STREAM, 0);
    lastError = errno;
    if (fd == INVALID_SOCKET) {
        return PCSL_NET_IOERROR;
    }

    status =
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &truebuf, sizeof(truebuf));
    lastError = errno;

    if (status == -1) {
        /*
         * DEBUG:
         * printf("setsockopt failed errno=%d\n", errno);
         */
        (void)close(fd);
        return PCSL_NET_IOERROR;
    }

    addr.sin_family      = AF_INET;
    addr.sin_port        = htons((unsigned short)port);

    memcpy(&addr.sin_addr.s_addr, ipBytes, sizeof(addr.sin_addr.s_addr));

    flags = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    status = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
    lastError = errno;

    /*
       connect() may return 0 in case of non-blocking sockets if
       server is on the same host as client. No need to create a
       a blocked handle if connection is successfully established
    */

    if (status == 0) {
        *pHandle = na_create(fd);
        return PCSL_NET_SUCCESS;
    }

    if ((status == SOCKET_ERROR) &&
            (errno == EINPROGRESS || errno == EALREADY)) {
        *pContext = NULL;
        *pHandle = na_create(fd);
        na_register_for_write(*pHandle);
        return PCSL_NET_WOULDBLOCK;
    }

    close(fd);
    return PCSL_NET_CONNECTION_NOTFOUND;
}