tnt/tnt_io.c
/** Waiting for connection while handling signal events. */
Waiting for connection while handling signal events.
Waiting for connection while handling signal events .
static enum tnt_error
tnt_io_connect_do(struct tnt_stream_net *s, struct sockaddr *addr,
		       socklen_t addr_size)
{
	enum tnt_error result = tnt_io_nonblock(s, 1);
	if (result != TNT_EOK)
		return result;
	if (connect(s->fd, (struct sockaddr*)addr, addr_size) != -1)
		return TNT_EOK;
	if (errno == EINPROGRESS) {
		struct timeval start_connect;
		if (gettimeofday(&start_connect, NULL) == -1) {
			s->errno_ = errno;
			return TNT_ESYSTEM;
		}
		int timeout = TIMEVAL_TO_MSEC(s->opt.tmout_connect);
		while (1) {
			struct pollfd fds[1];
			fds[0].fd = s->fd;
			fds[0].events = POLLOUT;
			int ret = poll(fds, 1, timeout);
			if (ret == -1) {
				if (errno == EINTR || errno == EAGAIN) {
					struct timeval curr;
					if (gettimeofday(&curr, NULL) == -1) {
						s->errno_ = errno;
						return TNT_ESYSTEM;
					}
					int passed_time = TIMEVAL_DIFF_MSEC(
						curr, start_connect);
					if (passed_time >= timeout) {
						return TNT_ETMOUT;
					}
				} else {
					s->errno_ = errno;
					return TNT_ESYSTEM;
				}
			} else if (ret == 0) {
				return TNT_ETMOUT;
			} else {
				break;
			}
		}
		int opt = 0;
		socklen_t len = sizeof(opt);
		if ((getsockopt(s->fd, SOL_SOCKET, SO_ERROR,
				&opt, &len) == -1) || opt) {
			s->errno_ = (opt) ? opt : errno;
			return TNT_ESYSTEM;
		}
	} else {
		s->errno_ = errno;
		return TNT_ESYSTEM;
	}
	result = tnt_io_nonblock(s, 0);
	if (result != TNT_EOK)
		return result;
	return TNT_EOK;
}
static enum tnt_error tnt_io_connect_do ( struct tnt_stream_net * s , struct sockaddr * addr , socklen_t addr_size ) { enum tnt_error result = tnt_io_nonblock ( s , 1 ) ; if ( result != TNT_EOK ) return result ; if ( connect ( s -> fd , ( struct sockaddr * ) addr , addr_size ) != -1 ) return TNT_EOK ; if ( errno == EINPROGRESS ) { struct timeval start_connect ; if ( gettimeofday ( & start_connect , NULL ) == -1 ) { s -> errno_ = errno ; return TNT_ESYSTEM ; } int timeout = TIMEVAL_TO_MSEC ( s -> opt . tmout_connect ) ; while ( 1 ) { struct pollfd fds [ 1 ] ; fds [ 0 ] . fd = s -> fd ; fds [ 0 ] . events = POLLOUT ; int ret = poll ( fds , 1 , timeout ) ; if ( ret == -1 ) { if ( errno == EINTR || errno == EAGAIN ) { struct timeval curr ; if ( gettimeofday ( & curr , NULL ) == -1 ) { s -> errno_ = errno ; return TNT_ESYSTEM ; } int passed_time = TIMEVAL_DIFF_MSEC ( curr , start_connect ) ; if ( passed_time >= timeout ) { return TNT_ETMOUT ; } } else { s -> errno_ = errno ; return TNT_ESYSTEM ; } } else if ( ret == 0 ) { return TNT_ETMOUT ; } else { break ; } } int opt = 0 ; socklen_t len = sizeof ( opt ) ; if ( ( getsockopt ( s -> fd , SOL_SOCKET , SO_ERROR , & opt , & len ) == -1 ) || opt ) { s -> errno_ = ( opt ) ? opt : errno ; return TNT_ESYSTEM ; } } else { s -> errno_ = errno ; return TNT_ESYSTEM ; } result = tnt_io_nonblock ( s , 0 ) ; if ( result != TNT_EOK ) return result ; return TNT_EOK ; }
Waiting for connection while handling signal events.
Waiting for connection while handling signal events .
/* setting nonblock */ /* get start connect time */ /* set initial timer */ /* get current time */ /* check timeout */ /* timeout */ /* timeout */ /* we have a event on socket */ /* checking error status */ /* setting block */