int iolooper_wait_absolute(IoLooper* iol, int64_t deadline) { int64_t timeout = deadline - iolooper_now(); /* If the deadline has passed, set the timeout to 0, this allows us * to poll the file descriptor nonetheless */ if (timeout < 0) timeout = 0; return iolooper_wait(iol, timeout); }
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; }