[ENet-discuss] Question about ENet and reconnecting

Patrick Klos pklos at lauferwind.com
Wed Jul 31 06:46:31 PDT 2013


Hello ENet mailing list,

I am new to ENet and have been tasked with learning how it works in our 
environment.

After running some packet traces, I noticed that when our "server" uses 
enet_host_broadcast(), it would sometimes send the broadcasted packet to 
a given "client" peer twice (with different sequence numbers).  I 
tracked it down to the fact that the client timed out the server, the 
client uses enet_peer_reset() to discard the apparently broken 
connection, and then used enet_host_connect() to reconnect to the 
server.  The problem appears when the server accepts the new connection 
for the client while still maintaining the old connection for the 
client.  This happens because the new connection has a different 
"connectID" value, so enet_protocol_handle_connect() [protocol.c line 
297] doesn't associate the new connection with old connection using the 
same IP address and port.

Before I jump to conclusions, let me ask is there any scenario where 
ENet would want to maintain more than connection between a pair of IP 
addresses and ports?  Could there be a legitimate use for setting up 
more than one ENet connection between a single client and server?

And if the answer to the previous question is NO, would there be any 
harm in adding code to enet_protocol_handle_connect() such that if it 
runs into a peer that uses the same IP address and port (but obviously a 
different connectID) that the new connection is coming in on, 
enet_protocol_handle_connect() would immediately reset the old peer and 
then proceed let the new peer be created?

Perhaps changing this: (starting at line 290)

     for (currentPeer = host -> peers;
          currentPeer < & host -> peers [host -> peerCount];
          ++ currentPeer)
     {
         if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
             currentPeer -> address.host == host -> receivedAddress.host &&
             currentPeer -> address.port == host -> receivedAddress.port &&
             currentPeer -> connectID == command -> connect.connectID)
           return NULL;
     }

To this?

     for (currentPeer = host -> peers;
          currentPeer < & host -> peers [host -> peerCount];
          ++ currentPeer)
     {
         if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
             currentPeer -> address.host == host -> receivedAddress.host &&
             currentPeer -> address.port == host -> receivedAddress.port)
         {
             // if using the same connectID, behave like before
             if (currentPeer -> connectID == command -> connect.connectID)
                 return NULL;

             // new connection from existing peer - reset the existing 
connection now
             enet_peer_reset(currentPeer);

             // we can stop looking since we won't find another match
             break;
         }
     }

Thanks,

Patrick



More information about the ENet-discuss mailing list