ntohs(cs->serv_addr.sin_port),strerror(errno));
if (setsockopt(cs->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
- tlog(TL_CRIT|TL_EXIT, "socketsockopt failed: %s", strerror(errno));
+ tlog(TL_CRIT|TL_EXIT, "socketsockopt failed: %s (d%)", strerror(errno), errno);
return CS_ERROR;
}
TC_fillConnection(TC_Connection *sc, char *name, u_int32_t port) {
if ( !sc )
sc = (TC_Connection *)t0malloc(sizeof(TC_Connection));
+ else
+ memset(sc, 0, sizeof(TC_Connection));
sc->serv_addr.sin_family = AF_INET;
sc->serv_addr.sin_addr.s_addr = (name) ? inet_addr(name) : htonl(INADDR_ANY);
sc->serv_addr.sin_port = htons(port);
}
cs->state = CS_INPROCESS;
- return TC_ServerConnect( cs );
+ return TC_ServerConnect( cs, 0 );
}
u_int32_t
-TC_ServerConnect( TC_Connection *cs ) {
- fd_set fdwr, fdexcept;
+TC_ServerConnect( TC_Connection *cs, int timeout ) {
+ struct pollfd pfd;
int ret;
- struct timeval timeout;
if ( cs->state != CS_INPROCESS )
return cs->state;
- FD_ZERO( &fdwr );
- FD_ZERO( &fdexcept );
-
- FD_SET(cs->fd, &fdwr);
- FD_SET(cs->fd, &fdexcept);
-
- memset( &timeout, 0, sizeof(struct timeval));
-
- ret = select( cs->fd+1, NULL, &fdwr, &fdexcept, &timeout ) ;
+ pfd.fd = cs->fd;
+ pfd.events = POLLOUT;
+ pfd.revents = 0;
+ ret = poll( &pfd, 1, timeout );
if ( ret<0 ) {
- tlog( TL_CRIT, "TC_ServerConnect: select: %s",
+ tlog( TL_CRIT, "TC_ServerConnect: poll: %s",
strerror(errno));
cs->state = CS_ERROR;
return CS_ERROR;
} else if ( ret == 0 )
return CS_INPROCESS;
- if ( FD_ISSET(cs->fd, &fdexcept) ) {
- tlog( TL_CRIT, "TC_ServerConnect: select return connect error for %s:%d",
+ if ( (pfd.revents & (POLLHUP | POLLNVAL | POLLERR)) ) {
+ tlog( TL_CRIT, "TC_ServerConnect: poll return connect error for %s:%d",
inet_ntoa(cs->serv_addr.sin_addr), ntohs(cs->serv_addr.sin_port));
cs->state = CS_ERROR;
return CS_ERROR;
}
- if ( ! FD_ISSET(cs->fd, &fdwr) )
+ if ( ! (pfd.revents & POLLOUT) )
return CS_INPROCESS;
u_int32_t
TC_Talk( TC_Connection *cs ) {
- u_int32_t ret = TC_ServerInitConnect( cs );
+ if ( cs->state==CS_NOTINITED )
+ TC_ServerInitConnect( cs );
- while( ret == CS_INPROCESS ) {
- ret = TC_ServerConnect(cs);
- }
+ while( cs->state == CS_INPROCESS )
+ TC_ServerConnect(cs, 100);
- if ( ret != CS_CONNECTED )
- return ret;
+ if ( cs->state != CS_CONNECTED )
+ return cs->state;
- while( ret != CS_FINISHSEND ) {
- ret = TC_Send(cs);
- if ( ret == CS_ERROR ) return ret;
+ cs->state = CS_SEND;
+ cs->ptr = cs->buf;
+ while( cs->state != CS_FINISHSEND ) {
+ while( !TC_ReadyIO( &cs, 1, 100) );
+ if ( TC_Send(cs) == CS_ERROR ) return CS_ERROR;
}
cs->state = CS_READ;