From 84d4647329a3c1b9c8536596deeca81f7c8c2526 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Fri, 5 Jan 2024 10:19:49 +0100 Subject: [PATCH] Scale resend timeout exponentially --- src/constants.h | 5 ----- src/network/connection.cpp | 18 +++++++++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/constants.h b/src/constants.h index 1ebfc7503..b4a2c7767 100644 --- a/src/constants.h +++ b/src/constants.h @@ -42,11 +42,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #define CONNECTION_TIMEOUT 30 -#define RESEND_TIMEOUT_MIN 0.1 -#define RESEND_TIMEOUT_MAX 3.0 -// resend_timeout = avg_rtt * this -#define RESEND_TIMEOUT_FACTOR 4 - /* Server */ diff --git a/src/network/connection.cpp b/src/network/connection.cpp index 2c6bde456..dada76f83 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -51,6 +51,15 @@ namespace con #define PING_TIMEOUT 5.0f +// exponent base +#define RESEND_SCALE_BASE 1.5f + +// since spacing is exponential the numbers here shouldn't be too high +// (it's okay to start out quick) +#define RESEND_TIMEOUT_MIN 0.1f +#define RESEND_TIMEOUT_MAX 2.0f +#define RESEND_TIMEOUT_FACTOR 2 + u16 BufferedPacket::getSeqnum() const { if (size() < BASE_HEADER_SIZE + 3) @@ -354,7 +363,10 @@ std::list> MutexAutoLock listlock(m_list_mutex); std::list> timed_outs; for (auto &packet : m_list) { - if (packet->time < timeout) + // resend time scales exponentially with each cycle + const float pkt_timeout = timeout * powf(RESEND_SCALE_BASE, packet->resend_count); + + if (packet->time < pkt_timeout) continue; // caller will resend packet so reset time and increase counter @@ -980,14 +992,14 @@ void UDPPeer::reportRTT(float rtt) } RTTStatistics(rtt,"rudp",MAX_RELIABLE_WINDOW_SIZE*10); + // use this value to decide the resend timeout float timeout = getStat(AVG_RTT) * RESEND_TIMEOUT_FACTOR; if (timeout < RESEND_TIMEOUT_MIN) timeout = RESEND_TIMEOUT_MIN; if (timeout > RESEND_TIMEOUT_MAX) timeout = RESEND_TIMEOUT_MAX; - MutexAutoLock usage_lock(m_exclusive_access_mutex); - resend_timeout = timeout; + setResendTimeout(timeout); } bool UDPPeer::Ping(float dtime,SharedBuffer& data)