From: Stephen Seo Date: Wed, 12 Mar 2025 08:44:41 +0000 (+0900) Subject: Fix packet timeout with large heartbeat times X-Git-Tag: 1.4^2~1 X-Git-Url: https://git.seodisparate.com/stephenseo/server_config?a=commitdiff_plain;h=cb3b45963c977bcfb1f2e73b5b10fe5f81fe4f4f;p=UDPConnection Fix packet timeout with large heartbeat times --- 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