[enet-cvs] CVS: enet peer.c,1.13,1.14 protocol.c,1.23,1.24

Lee Salzman enet at sferik.cubik.org
Mon Nov 17 09:25:39 PST 2003


Update of /home/enet/cvsroot/enet
In directory sferik:/tmp/cvs-serv19824

Modified Files:
	peer.c protocol.c 
Log Message:
much stricter and saner disconnect handling


Index: peer.c
===================================================================
RCS file: /home/enet/cvsroot/enet/peer.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- peer.c	2003/11/16 18:18:24	1.13
+++ peer.c	2003/11/17 17:25:37	1.14
@@ -285,6 +285,41 @@
     }
 }
 
+void
+enet_peer_reset_queues (ENetPeer * peer)
+{
+    ENetChannel * channel;
+
+    while (enet_list_empty (& peer -> acknowledgements) == 0)
+      enet_free (enet_list_remove (enet_list_begin (& peer -> acknowledgements)));
+
+    enet_peer_reset_outgoing_commands (& peer -> sentReliableCommands);
+    enet_peer_reset_outgoing_commands (& peer -> sentUnreliableCommands);
+    enet_peer_reset_outgoing_commands (& peer -> outgoingReliableCommands);
+    enet_peer_reset_outgoing_commands (& peer -> outgoingUnreliableCommands);
+
+    if (peer -> channels != NULL && peer -> channelCount > 0)
+    {
+        for (channel = peer -> channels;
+             channel < & peer -> channels [peer -> channelCount];
+             ++ channel)
+        {
+            channel -> outgoingReliableSequenceNumber = 0;
+            channel -> outgoingUnreliableSequenceNumber = 0;
+            channel -> incomingReliableSequenceNumber = 0;
+            channel -> incomingUnreliableSequenceNumber = 0;
+
+            enet_peer_reset_incoming_commands (& channel -> incomingReliableCommands);
+            enet_peer_reset_incoming_commands (& channel -> incomingUnreliableCommands);
+        }
+
+        enet_free (peer -> channels);
+    }
+
+    peer -> channels = NULL;
+    peer -> channelCount = 0;
+}
+
 /** Forcefully disconnects a peer.
     @param peer peer to forcefully disconnect
     @remarks The foreign host represented by the peer is not notified of the disconnection and will timeout
@@ -293,8 +328,6 @@
 void
 enet_peer_reset (ENetPeer * peer)
 {
-    ENetChannel * channel;
-
     peer -> outgoingPeerID = 0xFFFF;
     peer -> challenge = 0;
 
@@ -331,35 +364,8 @@
     peer -> reliableDataInTransit = 0;
     peer -> outgoingReliableSequenceNumber = 0;
     peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
-
-    while (enet_list_empty (& peer -> acknowledgements) == 0)
-      enet_free (enet_list_remove (enet_list_begin (& peer -> acknowledgements)));
-
-    enet_peer_reset_outgoing_commands (& peer -> sentReliableCommands);
-    enet_peer_reset_outgoing_commands (& peer -> sentUnreliableCommands);
-    enet_peer_reset_outgoing_commands (& peer -> outgoingReliableCommands);
-    enet_peer_reset_outgoing_commands (& peer -> outgoingUnreliableCommands);
-
-    if (peer -> channels != NULL && peer -> channelCount > 0)
-    {
-        for (channel = peer -> channels;
-             channel < & peer -> channels [peer -> channelCount];
-             ++ channel)
-        {
-            channel -> outgoingReliableSequenceNumber = 0;
-            channel -> outgoingUnreliableSequenceNumber = 0;
-            channel -> incomingReliableSequenceNumber = 0;
-            channel -> incomingUnreliableSequenceNumber = 0;
-             
-            enet_peer_reset_incoming_commands (& channel -> incomingReliableCommands);
-            enet_peer_reset_incoming_commands (& channel -> incomingUnreliableCommands);
-        }
-
-        enet_free (peer -> channels);
-    }
 
-    peer -> channels = NULL;
-    peer -> channelCount = 0;
+    enet_peer_reset_queues (peer);
 }
 
 /** Sends a ping request to a peer.
@@ -397,8 +403,11 @@
     ENetProtocol command;
 
     if (peer -> state == ENET_PEER_STATE_DISCONNECTING ||
-        peer -> state == ENET_PEER_STATE_DISCONNECTED)
+        peer -> state == ENET_PEER_STATE_DISCONNECTED ||
+        peer -> state == ENET_PEER_STATE_ZOMBIE)
       return;
+
+    enet_peer_reset_queues (peer);
 
     peer -> state = ENET_PEER_STATE_DISCONNECTING;
 

Index: protocol.c
===================================================================
RCS file: /home/enet/cvsroot/enet/protocol.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- protocol.c	2003/11/16 18:24:14	1.23
+++ protocol.c	2003/11/17 17:25:37	1.24
@@ -90,12 +90,13 @@
     }
 }
 
-static void
+static ENetProtocolCommand
 enet_protocol_remove_sent_reliable_command (ENetPeer * peer, enet_uint32 reliableSequenceNumber, enet_uint8 channelID)
 {
     ENetOutgoingCommand * outgoingCommand;
     ENetListIterator currentCommand;
-   
+    ENetProtocolCommand commandNumber;
+
     for (currentCommand = enet_list_begin (& peer -> sentReliableCommands);
          currentCommand != enet_list_end (& peer -> sentReliableCommands);
          currentCommand = enet_list_next (currentCommand))
@@ -110,6 +111,8 @@
     if (currentCommand == enet_list_end (& peer -> sentReliableCommands))
       return;
 
+    commandNumber = outgoingCommand -> command.header.command;
+
     enet_list_remove (& outgoingCommand -> outgoingCommandList);
 
     if (outgoingCommand -> packet != NULL)
@@ -125,11 +128,13 @@
     enet_free (outgoingCommand);
 
     if (enet_list_empty (& peer -> sentReliableCommands))
-      return;
+      return commandNumber;
     
     outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentReliableCommands);
     
     peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
+
+    return commandNumber;
 } 
 
 static ENetPeer *
@@ -170,7 +175,7 @@
     if (currentPeer >= & host -> peers [host -> peerCount])
       return NULL;
 
-    currentPeer -> state = ENET_PEER_STATE_CONNECTING;
+    currentPeer -> state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
     currentPeer -> challenge = header -> challenge;
     currentPeer -> address = host -> receivedAddress;
     currentPeer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
@@ -259,7 +264,8 @@
     ENetPacket * packet;
 
     if (command -> header.commandLength <= sizeof (ENetProtocolSendReliable) ||
-        command -> header.channelID >= peer -> channelCount)
+        command -> header.channelID >= peer -> channelCount ||
+        peer -> state != ENET_PEER_STATE_CONNECTED)
       return;
 
     packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendReliable),
@@ -275,7 +281,8 @@
     ENetPacket * packet;
 
     if (command -> header.commandLength <= sizeof (ENetProtocolSendUnreliable) ||
-        command -> header.channelID >= peer -> channelCount)
+        command -> header.channelID >= peer -> channelCount ||
+        peer -> state != ENET_PEER_STATE_CONNECTED)
       return;
 
     packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable),
@@ -299,7 +306,8 @@
     ENetIncomingCommand * startCommand;
 
     if (command -> header.commandLength <= sizeof (ENetProtocolSendFragment) ||
-        command -> header.channelID >= peer -> channelCount)
+        command -> header.channelID >= peer -> channelCount ||
+        peer -> state != ENET_PEER_STATE_CONNECTED)
       return;
 
     startSequenceNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.startSequenceNumber);
@@ -407,7 +415,12 @@
     if (command -> header.commandLength < sizeof (ENetProtocolDisconnect))
       return;
 
-    peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECTION;
+    enet_peer_reset_queues (peer);
+
+    if (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE)
+      peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT;
+    else
+      peer -> state = ENET_PEER_STATE_ZOMBIE;
 }
 
 static int
@@ -416,7 +429,8 @@
     enet_uint32 roundTripTime,
            receivedSentTime,
            receivedReliableSequenceNumber;
-           
+    ENetProtocolCommand commandNumber;
+
     if (command -> header.commandLength < sizeof (ENetProtocolAcknowledge))
       return 0;
 
@@ -456,11 +470,14 @@
          
     receivedReliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedReliableSequenceNumber);
 
-    enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command -> header.channelID);
+    commandNumber = enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command -> header.channelID);
 
     switch (peer -> state)
     {
-    case ENET_PEER_STATE_CONNECTING:
+    case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
+       if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT)
+         return 0;
+
        host -> recalculateBandwidthLimits = 1;
 
        peer -> state = ENET_PEER_STATE_CONNECTED;
@@ -471,7 +488,7 @@
        return 1;
 
     case ENET_PEER_STATE_DISCONNECTING:
-       if (enet_list_empty (& peer -> sentReliableCommands) == 0)
+       if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
          return 0;
 
        host -> recalculateBandwidthLimits = 1;
@@ -496,7 +513,8 @@
     enet_uint16 mtu;
     enet_uint32 windowSize;
 
-    if (command -> header.commandLength < sizeof (ENetProtocolVerifyConnect))
+    if (command -> header.commandLength < sizeof (ENetProtocolVerifyConnect) ||
+        peer -> state != ENET_PEER_STATE_CONNECTING)
       return;
 
     if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount ||
@@ -509,6 +527,8 @@
         return;
     }
 
+    peer -> state = ENET_PEER_STATE_CONNECTED;
+
     peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
 
     mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu);
@@ -554,16 +574,20 @@
     header -> peerID = ENET_NET_TO_HOST_16 (header -> peerID);
     header -> sentTime = ENET_NET_TO_HOST_32 (header -> sentTime);
 
-    if (header -> peerID >= host -> peerCount)
+    if (header -> peerID == 0xFFFF)
       peer = NULL;
     else
+    if (header -> peerID >= host -> peerCount)
+      return 0;
+    else
     {
        peer = & host -> peers [header -> peerID];
 
        if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
+           peer -> state == ENET_PEER_STATE_ZOMBIE || 
            host -> receivedAddress.host != peer -> address.host ||
            header -> challenge != peer -> challenge)
-         peer = NULL;
+         return 0;
        else
          peer -> address.port = host -> receivedAddress.port;
     }
@@ -656,7 +680,22 @@
 
        if (peer != NULL &&
            (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE) != 0)
-         enet_peer_queue_acknowledgement (peer, command, header -> sentTime);        
+       {
+           switch (peer -> state)
+           {
+           case ENET_PEER_STATE_DISCONNECTING:
+              break;
+
+           case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
+              if (command -> header.command != ENET_PROTOCOL_COMMAND_DISCONNECT)
+                break;
+
+           default:   
+              enet_peer_queue_acknowledgement (peer, command, header -> sentTime);        
+
+              break;
+           }
+       }
     }
 
     if (event -> type != ENET_EVENT_TYPE_NONE)
@@ -724,6 +763,10 @@
 
        acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  
+       if (peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT &&
+           acknowledgement -> command.header.command != ENET_PROTOCOL_COMMAND_DISCONNECT)
+         continue;
+
        currentAcknowledgement = enet_list_next (currentAcknowledgement);
 
        buffer -> data = command;
@@ -970,7 +1013,8 @@
          currentPeer < & host -> peers [host -> peerCount];
          ++ currentPeer)
     {
-        if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
+        if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
+            currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
           continue;
 
         host -> commandCount = 0;
@@ -982,22 +1026,21 @@
      
         if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol))
         {
-           if (checkForTimeouts != 0 &&
-               enet_list_empty (& currentPeer -> sentReliableCommands) == 0 &&
-               ENET_TIME_GREATER_EQUAL (timeCurrent, currentPeer -> nextTimeout) &&
-               enet_protocol_check_timeouts (host, currentPeer, event) == 1)
-             return 1;
-           
-           if (enet_list_empty (& currentPeer -> outgoingReliableCommands) == 0)
-             enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
-           else
-           if (enet_list_empty (& currentPeer -> sentReliableCommands) &&
-               ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL &&
-               currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
-           { 
-              enet_peer_ping (currentPeer);
-              enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
-           }
+            if (checkForTimeouts != 0 &&
+                enet_list_empty (& currentPeer -> sentReliableCommands) == 0 &&
+                ENET_TIME_GREATER_EQUAL (timeCurrent, currentPeer -> nextTimeout) &&
+                enet_protocol_check_timeouts (host, currentPeer, event) == 1)
+              return 1;
+        }
+        if (enet_list_empty (& currentPeer -> outgoingReliableCommands) == 0)
+          enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
+        else
+        if (enet_list_empty (& currentPeer -> sentReliableCommands) &&
+            ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL &&
+            currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
+        { 
+            enet_peer_ping (currentPeer);
+            enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
         }
                       
         if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol) &&



More information about the enet-cvs mailing list