]> git.seodisparate.com - UDPConnection/commitdiff
Fix packet timeout with large heartbeat times
authorStephen Seo <seo.disparate@gmail.com>
Wed, 12 Mar 2025 08:44:41 +0000 (17:44 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 12 Mar 2025 08:44:54 +0000 (17:44 +0900)
src/UDPConnection.cpp

index 0588f76a24f4e7958bb0f77ac55982dfa3503c06..02676b507ceb552e427bebf87ba6787c8005c6ef 100644 (file)
@@ -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<std::shared_mutex> 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