From badcd6c01d440037c24c9fb4d3f3fc3aa498367b Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 20 Oct 2024 16:32:16 -0500 Subject: [PATCH] Fix waking early to send pings --- protocol.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/protocol.c b/protocol.c index 1f4e657..f3269ac 100644 --- a/protocol.c +++ b/protocol.c @@ -1758,6 +1758,17 @@ enet_protocol_compute_wait_timeout(ENetHost * host, enet_uint32 timeout) { if (! ENET_TIME_LESS (currentPeer -> nextTimeout, host -> serviceTime)) { timeout = ENET_MIN (timeout, ENET_TIME_DIFFERENCE (currentPeer -> nextTimeout, host -> serviceTime) + 1); + } + + if (currentPeer -> lastReceiveTime) { + enet_uint32 timeSinceLastRecv = ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> lastReceiveTime); + if (timeSinceLastRecv >= currentPeer -> pingInterval) { + // Ping is due now for this peer + return 0; + } else { + timeout = ENET_MIN (timeout, currentPeer -> pingInterval - timeSinceLastRecv); + } + } else { timeout = ENET_MIN (timeout, currentPeer -> pingInterval); } }