avoid some looping in enet_host_bandwidth_throttle

This commit is contained in:
lsalzman 2013-05-08 10:46:17 +03:00
parent 27d41dd2ae
commit 30d859f9a4

16
host.c
View file

@ -334,13 +334,15 @@ enet_host_bandwidth_throttle (ENetHost * host)
bandwidth, bandwidth,
throttle = 0, throttle = 0,
bandwidthLimit = 0; bandwidthLimit = 0;
int needsAdjustment; int needsAdjustment = 0;
ENetPeer * peer; ENetPeer * peer;
ENetProtocol command; ENetProtocol command;
if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL) if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
return; return;
host -> bandwidthThrottleEpoch = timeCurrent;
for (peer = host -> peers; for (peer = host -> peers;
peer < & host -> peers [host -> peerCount]; peer < & host -> peers [host -> peerCount];
++ peer) ++ peer)
@ -348,6 +350,9 @@ enet_host_bandwidth_throttle (ENetHost * host)
if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
continue; continue;
if (peer -> incomingBandwidth != 0)
needsAdjustment = 1;
++ peersTotal; ++ peersTotal;
dataTotal += peer -> outgoingDataTotal; dataTotal += peer -> outgoingDataTotal;
} }
@ -356,13 +361,17 @@ enet_host_bandwidth_throttle (ENetHost * host)
return; return;
peersRemaining = peersTotal; peersRemaining = peersTotal;
needsAdjustment = 1;
if (host -> outgoingBandwidth == 0) if (host -> outgoingBandwidth == 0)
bandwidth = ~0; bandwidth = ~0;
else else
bandwidth = (host -> outgoingBandwidth * elapsedTime) / 1000; bandwidth = (host -> outgoingBandwidth * elapsedTime) / 1000;
if (dataTotal < bandwidth)
throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
else
throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
while (peersRemaining > 0 && needsAdjustment != 0) while (peersRemaining > 0 && needsAdjustment != 0)
{ {
needsAdjustment = 0; needsAdjustment = 0;
@ -398,7 +407,6 @@ enet_host_bandwidth_throttle (ENetHost * host)
peer -> outgoingBandwidthThrottleEpoch = timeCurrent; peer -> outgoingBandwidthThrottleEpoch = timeCurrent;
needsAdjustment = 1; needsAdjustment = 1;
-- peersRemaining; -- peersRemaining;
bandwidth -= peerBandwidth; bandwidth -= peerBandwidth;
@ -477,8 +485,6 @@ enet_host_bandwidth_throttle (ENetHost * host)
} }
} }
host -> bandwidthThrottleEpoch = timeCurrent;
for (peer = host -> peers; for (peer = host -> peers;
peer < & host -> peers [host -> peerCount]; peer < & host -> peers [host -> peerCount];
++ peer) ++ peer)