int syncsocket_stop_write(SyncSocket* ssocket) { if (ssocket == NULL || ssocket->fd < 0 || ssocket->iolooper == NULL) { errno = EINVAL; return -1; } iolooper_del_write(ssocket->iolooper, ssocket->fd); return 0; }
SyncSocket* syncsocket_connect(int fd, SockAddress* sockaddr, int timeout) { IoLooper* looper = NULL; int connect_status; SyncSocket* sync_socket; socket_set_nonblock(fd); for(;;) { connect_status = socket_connect(fd, sockaddr); if (connect_status >= 0) { // Connected. Create IoLooper for the helper. looper = iolooper_new(); break; } if (errno == EINPROGRESS || errno == EAGAIN || errno == EWOULDBLOCK) { // Connection is in progress. Wait till it's finished. looper = iolooper_new(); iolooper_add_write(looper, fd); connect_status = iolooper_wait(looper, timeout); if (connect_status > 0) { iolooper_del_write(looper, fd); } else { iolooper_free(looper); return NULL; } } else if (errno != EINTR) { return NULL; } } // We're now connected. Lets initialize SyncSocket instance // for this connection. sync_socket = malloc(sizeof(SyncSocket)); if (sync_socket == NULL) { derror("PANIC: not enough memory\n"); exit(1); } sync_socket->iolooper = looper; sync_socket->fd = fd; return sync_socket; }
void iolooper_modify( IoLooper* iol, int fd, int oldflags, int newflags ) { if (fd < 0) return; int changed = oldflags ^ newflags; if ((changed & IOLOOPER_READ) != 0) { if ((newflags & IOLOOPER_READ) != 0) iolooper_add_read(iol, fd); else iolooper_del_read(iol, fd); } if ((changed & IOLOOPER_WRITE) != 0) { if ((newflags & IOLOOPER_WRITE) != 0) iolooper_add_write(iol, fd); else iolooper_del_write(iol, fd); } }