Skip to content

Commit

Permalink
Port improved ping interval logic into the control receive loop
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed Oct 20, 2024
1 parent 12e603e commit d3d3e6c
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/ControlStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// This is a private header, but it just contains some time macros
#include <enet/time.h>

#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif

// NV control stream packet header for TCP
typedef struct _NVCTL_TCP_PACKET_HEADER {
unsigned short type;
Expand Down Expand Up @@ -1064,10 +1068,24 @@ static void controlReceiveThreadFunc(void* context) {
// We add 1 ms just to ensure we're unlikely to undershoot the sleep() and have to
// do a tiny sleep for another iteration before the timeout is ready to be serviced.
waitTimeMs = ENET_TIME_DIFFERENCE(peer->nextTimeout, client->serviceTime) + 1;
if (waitTimeMs > peer->pingInterval) {
waitTimeMs = peer->pingInterval;
}

// Ensure we don't sleep through a ping
if (peer->lastReceiveTime && peer->lastSendTime) {
enet_uint32 timeSinceLastRecv = ENET_TIME_DIFFERENCE(client->serviceTime, peer->lastReceiveTime);
enet_uint32 timeSinceLastSend = ENET_TIME_DIFFERENCE(client->serviceTime, peer->lastSendTime);
enet_uint32 timeSinceLastComm = MIN(timeSinceLastSend, timeSinceLastRecv);

if (timeSinceLastComm >= peer->pingInterval) {
// Ping is due now for this peer
waitTimeMs = 0;
} else {
waitTimeMs = MIN(waitTimeMs, peer->pingInterval - timeSinceLastComm);
}
}
else {
waitTimeMs = MIN(waitTimeMs, peer->pingInterval);
}
}

PltUnlockMutex(&enetMutex);
Expand Down

0 comments on commit d3d3e6c

Please sign in to comment.