[Mit-cadr-cvs] r384 - trunk/emulator/usim

ggilley at common-lisp.net ggilley at common-lisp.net
Sat Dec 1 23:45:29 UTC 2012


Author: ggilley
Date: Sat Dec  1 15:45:28 2012
New Revision: 384

Log:
handle closing connections

Modified:
   trunk/emulator/usim/Files.c
   trunk/emulator/usim/chaos.c

Modified: trunk/emulator/usim/Files.c
==============================================================================
--- trunk/emulator/usim/Files.c	Sat Dec  1 11:32:54 2012	(r383)
+++ trunk/emulator/usim/Files.c	Sat Dec  1 15:45:28 2012	(r384)
@@ -832,6 +832,9 @@
 		errcode = BUG;		/* Default error is bad syntax */
         
         packet = chaos_connection_dequeue(conn);
+        if (packet == 0)
+            return NULL;
+
         t->t_packet = packet;
         t->t_connection = conn;
 
@@ -4772,6 +4775,8 @@
     
 loop:
 	packet = chaos_connection_dequeue(x->x_fh->f_connection);
+    if (packet == 0)
+        return -1;
 
     switch ((packet->opcode & 0xff00) >> 8) {
         case EOFOP:
@@ -4994,6 +4999,8 @@
         for (;;) {
             
             packet = chaos_connection_dequeue(conn);
+            if (packet == 0)
+                break;
 
             switch (packet->opcode >> 8) {
                 case 0200:
@@ -5067,6 +5074,8 @@
         for (;;) {
             
             packet = chaos_connection_dequeue(conn);
+            if (packet == 0)
+                break;
 
             switch (packet->opcode >> 8) {
                 case 0200:

Modified: trunk/emulator/usim/chaos.c
==============================================================================
--- trunk/emulator/usim/chaos.c	Sat Dec  1 11:32:54 2012	(r383)
+++ trunk/emulator/usim/chaos.c	Sat Dec  1 15:45:28 2012	(r384)
@@ -584,6 +584,7 @@
         if (connections[i] == 0)
         {
             chaos_connection *conn = (chaos_connection *)malloc(sizeof(chaos_connection));
+            static unsigned short index = 0x0101;
             
             conn->state = cs_closed;
             conn->packetnumber = 0;
@@ -591,7 +592,8 @@
             conn->remoteindex = 0;
             conn->remoteaddr = 0;
             conn->localaddr = CHAOS_SERVER_ADDRESS;
-            conn->localindex = (unsigned short)(((i+1) << 8) | (i + 1));
+            conn->localindex = index;
+            index += 0x0100;
             conn->queuehead = 0;
             conn->queuetail = 0;
  
@@ -779,6 +781,9 @@
     packet_queue *node = NULL;
     unsigned short nextpacket = conn->lastreceived + 1;
 
+    if (conn->state == cs_closed)
+        return 0;
+
     for (;;)
     {
         pthread_mutex_lock(&conn->queuelock);
@@ -835,10 +840,13 @@
 #if defined(OSX)
         dispatch_semaphore_wait(conn->queuesem, DISPATCH_TIME_FOREVER);
 #else
-	pthread_mutex_lock(&conn->queuesem);
-	pthread_cond_wait(&conn->queuecond, &conn->queuesem);
-	pthread_mutex_unlock(&conn->queuesem);
+        pthread_mutex_lock(&conn->queuesem);
+        pthread_cond_wait(&conn->queuecond, &conn->queuesem);
+        pthread_mutex_unlock(&conn->queuesem);
 #endif
+        
+        if (conn->state == cs_closed)
+            return 0;
     }
 }
 
@@ -917,8 +925,12 @@
         
         chaos_connection_queue(conn, pkt);
 
+        conn->state = cs_rfcsent;
+
         // wait for answer
         chaos_packet *answer = chaos_connection_dequeue(conn);
+        if (answer == 0)
+            return conn;
         
         conn->remoteindex = answer->sourceindex;
         conn->state = cs_open;
@@ -1080,7 +1092,6 @@
 #endif
                 return 0;
             }
-            
             if (conn)
             {
                 chaos_packet *pkt = malloc((size_t)size);
@@ -1290,7 +1301,21 @@
         }
         if (conn && (packet->opcode >> 8) == CHAOS_OPCODE_CLS)
         {
+#if CHAOS_DEBUG
             printf("chaos: got close\n");
+#endif
+            conn->state = cs_closed;
+            
+#if defined(OSX)
+            dispatch_semaphore_signal(conn->queuesem);
+#else
+            pthread_mutex_lock(&conn->queuesem);
+            pthread_cond_signal(&conn->queuecond);
+            pthread_mutex_unlock(&conn->queuesem);
+#endif
+            usleep(100000);      // wait for queue to wake up
+            chaos_delete_connection(conn);
+            return 0;
         }
         if (conn && (cmp_gt(conn->lastreceived, packet->number) || (conn->lastreceived == packet->number)))
         {




More information about the mit-cadr-cvs mailing list