gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r3463 - GNUnet/src/transports


From: grothoff
Subject: [GNUnet-SVN] r3463 - GNUnet/src/transports
Date: Sun, 8 Oct 2006 14:37:04 -0700 (PDT)

Author: grothoff
Date: 2006-10-08 14:37:03 -0700 (Sun, 08 Oct 2006)
New Revision: 3463

Modified:
   GNUnet/src/transports/tcp_helper.c
Log:
fix

Modified: GNUnet/src/transports/tcp_helper.c
===================================================================
--- GNUnet/src/transports/tcp_helper.c  2006-10-08 20:40:25 UTC (rev 3462)
+++ GNUnet/src/transports/tcp_helper.c  2006-10-08 21:37:03 UTC (rev 3463)
@@ -50,11 +50,6 @@
   struct SocketHandle * sock;
 
   /**
-   * number of users of this session (reference count)
-   */
-  int users;
-
-  /**
    * mutex for synchronized access to 'users'
    */
   struct MUTEX * lock;
@@ -70,6 +65,16 @@
    */
   int expectingWelcome;
 
+  /**
+   * number of users of this session (reference count)
+   */
+  int users;
+
+  /**
+   * Is this session active with select?
+   */
+  int in_select;
+
 } TCPSession;
 
 /* *********** globals ************* */
@@ -98,22 +103,26 @@
 
   GE_ASSERT(ectx, selector != NULL);
   MUTEX_LOCK(tcpsession->lock);
+  GE_ASSERT(ectx, tcpsession->users > 0);
   tcpsession->users--;
-  if (tcpsession->users > 0) {
+  if ( (tcpsession->users > 0) ||
+       (tcpsession->in_select == YES) ) {
     MUTEX_UNLOCK(tcpsession->lock);
     return OK;
   }  
+#if DEBUG_TCP
   GE_LOG(ectx,
         GE_DEBUG | GE_USER | GE_BULK,
         "TCP disconnect closes socket session.\n");
-
+#endif
   select_disconnect(selector,
                    tcpsession->sock);
   MUTEX_UNLOCK(tcpsession->lock);
-  MUTEX_DESTROY(tcpsession->lock);
-  FREE(tcpsession);  
-  FREE(tsession);
-
+  if (tcpsession->in_select == NO) {
+    MUTEX_DESTROY(tcpsession->lock);
+    FREE(tcpsession);  
+    FREE(tsession);
+  }
   return OK;
 }
 
@@ -243,7 +252,8 @@
   tcpSession->sender = *(coreAPI->myIdentity);
   tcpSession->expectingWelcome = YES;
   tcpSession->lock = MUTEX_CREATE(YES);
-  tcpSession->users = 1; /* select only, core has not seen this tsession! */
+  tcpSession->users = 0;
+  tcpSession->in_select = YES;
   tsession = MALLOC(sizeof(TSession));
   tsession->ttype = TCP_PROTOCOL_NUMBER;
   tsession->internal = tcpSession;
@@ -256,7 +266,18 @@
                                 struct SocketHandle * sock,
                                 void * sock_ctx) {
   TSession * tsession = sock_ctx;
-  tcpDisconnect(tsession);
+  TCPSession * tcpSession = tsession->internal;
+
+  MUTEX_LOCK(tcpSession->lock);
+  tcpSession->in_select = NO;
+  if (tcpSession->users == 0) {
+    MUTEX_UNLOCK(tcpSession->lock);
+    MUTEX_DESTROY(tcpSession->lock);
+    FREE(tcpSession);
+    FREE(tsession);
+  } else {
+    MUTEX_UNLOCK(tcpSession->lock);
+  }
 }
 
 /**
@@ -353,13 +374,16 @@
   tsession->internal = tcpSession;
   tsession->ttype = protocolNumber;
   tcpSession->lock = MUTEX_CREATE(YES);
-  tcpSession->users = 2; /* caller + select */
+  tcpSession->users = 1; /* caller */
+  tcpSession->in_select = NO;
   tcpSession->sender = helo->senderIdentity;
   tcpSession->expectingWelcome = NO;
   MUTEX_LOCK(tcplock);
-  select_connect(selector,
-                tcpSession->sock,
-                tsession);
+  if (OK == 
+      select_connect(selector,
+                    tcpSession->sock,
+                    tsession))
+    tcpSession->in_select = YES;
 
   /* send our node identity to the other side to fully establish the
      connection! */
@@ -380,8 +404,7 @@
           "Could not sent TCP welcome message, closing connection.\n");
 #endif
     /* disconnect caller -- error! */
-    tcpDisconnect(tsession); /* for caller */
-    tcpDisconnect(tsession); /* for select */
+    tcpDisconnect(tsession);
     MUTEX_UNLOCK(tcplock);
     return SYSERR;
   } else if (stats != NULL) 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]