[enet-cvs] CVS: enet host.c,1.3,1.4 peer.c,1.4,1.5 protocol.c,1.3,1.4

Lee Salzman enet-discuss@lists.puremagic.com
Mon, 10 Jun 2002 18:22:04 -0600


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

Modified Files:
	host.c peer.c protocol.c 
Log Message:
Fixed many bugs in the throttle and rate limiting.
Added throttle parameter configuration and made it symmetric.



Index: host.c
===================================================================
RCS file: /home/enet/cvsroot/enet/host.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- host.c	2002/06/08 02:29:12	1.3
+++ host.c	2002/06/11 00:22:02	1.4
@@ -123,7 +123,10 @@
     command.connect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
     command.connect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
     command.connect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
-
+    command.connect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
+    command.connect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
+    command.connect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
+    
     enet_peer_queue_outgoing_command (currentPeer, & command, NULL, 0, 0);
 
     return currentPeer;
@@ -171,7 +174,7 @@
     ENetPeer * peer;
     ENetProtocol command;
 
-    if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_EPOCH)
+    if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
       return;
 
     for (peer = host -> peers;
@@ -226,6 +229,9 @@
             if (peer -> packetThrottleLimit == 0)
               peer -> packetThrottleLimit = 1;
             
+            if (peer -> packetThrottle > peer -> packetThrottleLimit)
+              peer -> packetThrottle = peer -> packetThrottleLimit;
+
             peer -> outgoingBandwidthThrottleEpoch = timeCurrent;
 
             
@@ -246,10 +252,12 @@
           continue;
 
         peer -> packetThrottleLimit = throttle;
+
+        if (peer -> packetThrottle > peer -> packetThrottleLimit)
+          peer -> packetThrottle = peer -> packetThrottleLimit;
     }
     
-    if (host -> incomingBandwidth > 0 &&
-        host -> recalculateBandwidthLimits)
+    if (host -> recalculateBandwidthLimits)
     {
        host -> recalculateBandwidthLimits = 0;
 
@@ -257,6 +265,9 @@
        bandwidth = host -> incomingBandwidth;
        needsAdjustment = 1;
 
+       if (bandwidth == 0)
+         bandwidthLimit = 0;
+       else
        while (peersRemaining > 0 && needsAdjustment != 0)
        {
            needsAdjustment = 0;
@@ -270,7 +281,8 @@
                    peer -> incomingBandwidthThrottleEpoch == timeCurrent)
                  continue;
 
-               if (bandwidthLimit <= peer -> outgoingBandwidth)
+               if (peer -> outgoingBandwidth > 0 &&
+                   bandwidthLimit > peer -> outgoingBandwidth)
                  continue;
 
                peer -> incomingBandwidthThrottleEpoch = timeCurrent;

Index: peer.c
===================================================================
RCS file: /home/enet/cvsroot/enet/peer.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- peer.c	2002/06/08 02:48:49	1.4
+++ peer.c	2002/06/11 00:22:02	1.5
@@ -1,13 +1,34 @@
 #include <enet/memory.h>
 #include <enet/enet.h>
 
+void
+enet_peer_throttle_configure (ENetPeer * peer, uint32 interval, uint32 acceleration, uint32 deceleration)
+{
+    ENetProtocol command;
+
+    peer -> packetThrottleInterval = interval;
+    peer -> packetThrottleAcceleration = acceleration;
+    peer -> packetThrottleDeceleration = deceleration;
+
+    command.header.command = ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE;
+    command.header.channelID = 0xFF;
+    command.header.flags = 0;
+    command.header.commandLength = sizeof (ENetProtocolThrottleConfigure);
+
+    command.throttleConfigure.packetThrottleInterval = ENET_HOST_TO_NET_32 (interval);
+    command.throttleConfigure.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (acceleration);
+    command.throttleConfigure.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (deceleration);
+
+    enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
+}
+
 int
 enet_peer_throttle (ENetPeer * peer, uint32 statistic, uint32 mean, uint32 variance)
 {
     if (mean > variance &&
         statistic < mean - variance)
     {
-        peer -> packetThrottle += ENET_PEER_PACKET_THROTTLE_ACCELERATION;
+        peer -> packetThrottle += peer -> packetThrottleAcceleration;
 
         if (peer -> packetThrottle > peer -> packetThrottleLimit)
           peer -> packetThrottle = peer -> packetThrottleLimit;
@@ -17,8 +38,8 @@
     else
     if (statistic > mean + variance)
     {
-        if (peer -> packetThrottle > ENET_PEER_PACKET_THROTTLE_DECELERATION)
-          peer -> packetThrottle -= ENET_PEER_PACKET_THROTTLE_DECELERATION;
+        if (peer -> packetThrottle > peer -> packetThrottleDeceleration)
+          peer -> packetThrottle -= peer -> packetThrottleDeceleration;
         else
           peer -> packetThrottle = 0;
 
@@ -241,6 +262,9 @@
     peer -> packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE;
     peer -> packetThrottleCounter = 0;
     peer -> packetThrottleEpoch = 0;
+    peer -> packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION;
+    peer -> packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION;
+    peer -> packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL;
     peer -> bestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
     peer -> roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
     peer -> roundTripTimeVariance = 0;

Index: protocol.c
===================================================================
RCS file: /home/enet/cvsroot/enet/protocol.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- protocol.c	2002/06/08 02:29:12	1.3
+++ protocol.c	2002/06/11 00:22:02	1.4
@@ -161,6 +161,9 @@
     currentPeer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
     currentPeer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.incomingBandwidth);
     currentPeer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.outgoingBandwidth);
+    currentPeer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleInterval);
+    currentPeer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleAcceleration);
+    currentPeer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleDeceleration);
     currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
     currentPeer -> channelCount = channelCount;
 
@@ -208,6 +211,9 @@
     verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
     verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
     verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
+    verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
+    verifyCommand.verifyConnect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
+    verifyCommand.verifyConnect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
 
     enet_peer_queue_outgoing_command (currentPeer, & verifyCommand, NULL, 0, 0);
 
@@ -319,7 +325,7 @@
 static void
 enet_protocol_handle_bandwidth_limit (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
 {
-    if (command -> header.commandLength < sizeof (ENetProtocolPing))
+    if (command -> header.commandLength < sizeof (ENetProtocolBandwidthLimit))
       return;
 
     peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
@@ -327,6 +333,17 @@
 }
 
 static void
+enet_protocol_handle_throttle_configure (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
+{
+    if (command -> header.commandLength < sizeof (ENetProtocolThrottleConfigure))
+      return;
+
+    peer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleInterval);
+    peer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleAcceleration);
+    peer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleDeceleration);
+}
+
+static void
 enet_protocol_handle_disconnect (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
 {
     if (command -> header.commandLength < sizeof (ENetProtocolDisconnect))
@@ -368,7 +385,7 @@
     }
 
     if (peer -> packetThrottleEpoch == 0 ||
-        timeCurrent - peer -> packetThrottleEpoch >= ENET_PEER_PACKET_THROTTLE_EPOCH)
+        timeCurrent - peer -> packetThrottleEpoch >= peer -> packetThrottleInterval)
     {
         peer -> bestRoundTripTime = peer -> roundTripTime;
         peer -> packetThrottleEpoch = timeCurrent;
@@ -421,13 +438,15 @@
     uint16 packetSize;
     uint32 windowSize;
     size_t channelCount;
+    
 
     if (command -> header.commandLength < sizeof (ENetProtocolVerifyConnect))
       return;
-
-    channelCount = ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount);
 
-    if (channelCount != peer -> channelCount)
+    if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount ||
+        ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
+        ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
+        ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration)
     {
         peer -> state = ENET_PEER_STATE_ZOMBIE;
 
@@ -569,6 +588,11 @@
 
           break;
 
+       case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
+          enet_protocol_handle_throttle_configure (host, peer, command);
+
+          break;
+
        default:
           break;
        }
@@ -929,7 +953,7 @@
         if (currentPeer -> packetLossEpoch == 0)
           currentPeer -> packetLossEpoch = timeCurrent;
         else
-        if (timeCurrent - currentPeer -> packetLossEpoch >= ENET_PEER_PACKET_LOSS_EPOCH &&
+        if (timeCurrent - currentPeer -> packetLossEpoch >= ENET_PEER_PACKET_LOSS_INTERVAL &&
             currentPeer -> packetsSent > 0)
         {
            uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
@@ -1017,7 +1041,7 @@
 
     do
     {
-       if (timeCurrent - host -> bandwidthThrottleEpoch >= ENET_HOST_BANDWIDTH_THROTTLE_EPOCH)
+       if (timeCurrent - host -> bandwidthThrottleEpoch >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
          enet_host_bandwidth_throttle (host);
 
        switch (enet_protocol_receive_incoming_commands (host, event))