X-Git-Url: http://www.sigaev.ru/git/gitweb.cgi?a=blobdiff_plain;f=tcp.c;h=ecc3bf1c1a8a64ba1533b3b8fc0c9725b2697446;hb=8860fab0d48d01f3f48928780d47a126707c7b56;hp=df10a467aa7f49dd56d1a5c49ed4e92397fda44a;hpb=89a9787cf0f3b1e33bedd63ef0a8cfd99074ca72;p=tedtools.git diff --git a/tcp.c b/tcp.c index df10a46..ecc3bf1 100644 --- a/tcp.c +++ b/tcp.c @@ -156,7 +156,8 @@ TC_AcceptTcp(TC_Connection *cs) { TC_Connection * TC_fillConnection(TC_Connection *sc, char *name, u_int32_t port) { if ( !sc ) - sc = (TC_Connection *)t0malloc(sizeof(TC_Connection)); + sc = (TC_Connection *)tmalloc(sizeof(TC_Connection)); + 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); @@ -184,6 +185,10 @@ TC_ServerInitConnect( TC_Connection *cs ) { if (fcntl(cs->fd,F_SETFL,flags|O_NDELAY) < 0 ) tlog(TL_ALARM,"fcntl O_NDELAY - %s",strerror(errno)); + flags=1; + if (setsockopt(cs->fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) < 0) + tlog(TL_ALARM, "socketsockopt failed: %s (%d)", strerror(errno), errno); + if ( connect(cs->fd, (struct sockaddr *) &(cs->serv_addr), sizeof(struct sockaddr_in)) < 0 ) { if ( errno == EINPROGRESS || errno == EALREADY ) { @@ -191,7 +196,7 @@ TC_ServerInitConnect( TC_Connection *cs ) { return CS_INPROCESS; } else if (errno != EISCONN && errno != EALREADY && errno != EWOULDBLOCK && errno != EAGAIN) { - tlog(TL_DEBUG,"open4: %s:%d - %s", + tlog(TL_DEBUG,"connect: %s:%d - %s", inet_ntoa(cs->serv_addr.sin_addr), ntohs(cs->serv_addr.sin_port), strerror(errno)); shutdown(cs->fd,SHUT_RDWR); @@ -252,6 +257,8 @@ TC_ReadyIO( TC_Connection **cs, int number, int timeout ) { int ret,i, fdnum=0; if ( number==0 || cs ==NULL ) { + if (timeout<0) + timeout=1000; usleep( timeout * 1000.0 ); return 0; } @@ -266,6 +273,14 @@ TC_ReadyIO( TC_Connection **cs, int number, int timeout ) { } cs[i]->readyio=0; } + + if ( fdnum==0 ) { + tfree(pfd); + if (timeout<0) + timeout=1000; + usleep( timeout * 1000.0 ); + return 0; + } ret = poll( pfd, fdnum, timeout ); if ( ret<0 ) { tlog( TL_CRIT, "TC_ReadyIO: poll: %s", @@ -423,18 +438,20 @@ TC_FreeConnection( TC_Connection *cs ) { 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, 100); - } + 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;