From cb3b45963c977bcfb1f2e73b5b10fe5f81fe4f4f Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 12 Mar 2025 17:44:41 +0900 Subject: [PATCH] Fix packet timeout with large heartbeat times --- src/UDPConnection.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index 0588f76..02676b5 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -1690,7 +1690,22 @@ void UDPC::Context::update_impl() { && "Every entry in sentPkts must have a " "corresponding entry in sentInfoMap"); auto duration = now - sentInfoIter->second->sentTime; - if(duration > UDPC::PACKET_TIMEOUT_TIME) { + std::chrono::milliseconds double_heartbeat_duration; + + // Account for possible heartbeat duration longer than 1 + // second. + { + std::shared_lock lock(heartbeatMutex); + double_heartbeat_duration = heartbeatDuration * 2; + } + + // Pick the longer of PACKET_TIMEOUT_TIME and + // double_heartbeat_duration. Timeout if longer than the + // greater of the two. + if((UDPC::PACKET_TIMEOUT_TIME >= double_heartbeat_duration + && duration > UDPC::PACKET_TIMEOUT_TIME) + || (UDPC::PACKET_TIMEOUT_TIME < double_heartbeat_duration + && duration > double_heartbeat_duration)) { bool pktSigned = sentIter->data[UDPC_MIN_HEADER_SIZE] == 1; if((pktSigned -- 2.49.0