more coorect comparing
[tedtools.git] / tcp.c
diff --git a/tcp.c b/tcp.c
index df10a46..ecc3bf1 100644 (file)
--- 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;