cleanup
authorteodor <teodor>
Thu, 30 Sep 2004 13:40:44 +0000 (13:40 +0000)
committerteodor <teodor>
Thu, 30 Sep 2004 13:40:44 +0000 (13:40 +0000)
tcp.c

diff --git a/tcp.c b/tcp.c
index d65060c..ecc3bf1 100644 (file)
--- a/tcp.c
+++ b/tcp.c
@@ -156,9 +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));
-       else 
-               memset(sc, 0, 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);
@@ -186,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 ) {
@@ -193,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);
@@ -254,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;
        }
@@ -268,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",