From dff2f0131f494227e1e0c3e365c897a419b41e7e Mon Sep 17 00:00:00 2001 From: Michael Lidgren Date: Wed, 20 May 2015 09:14:58 +0200 Subject: [PATCH] Receiving an ack for a reliable message sent less than two seconds ago will postpone timeout --- Lidgren.Network/NetConnection.cs | 5 +++++ Lidgren.Network/NetReliableSenderChannel.cs | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Lidgren.Network/NetConnection.cs b/Lidgren.Network/NetConnection.cs index 06a5dca..da81ba7 100644 --- a/Lidgren.Network/NetConnection.cs +++ b/Lidgren.Network/NetConnection.cs @@ -106,6 +106,11 @@ namespace Lidgren.Network m_remoteEndPoint = endPoint; } + internal void ResetTimeout(double now) + { + m_timeoutDeadline = now + m_peerConfiguration.m_connectionTimeout; + } + internal void SetStatus(NetConnectionStatus status, string reason) { // user or library thread diff --git a/Lidgren.Network/NetReliableSenderChannel.cs b/Lidgren.Network/NetReliableSenderChannel.cs index eac8bfe..8ea1ad7 100644 --- a/Lidgren.Network/NetReliableSenderChannel.cs +++ b/Lidgren.Network/NetReliableSenderChannel.cs @@ -135,9 +135,14 @@ namespace Lidgren.Network return; } - private void DestoreMessage(int storeIndex) + private void DestoreMessage(double now, int storeIndex, out bool resetTimeout) { - NetOutgoingMessage storedMessage = m_storedMessages[storeIndex].Message; + // reset timeout if we receive ack within kThreshold of sending it + const double kThreshold = 2.0; + var srm = m_storedMessages[storeIndex]; + resetTimeout = (srm.NumSent == 1) && (now - srm.LastSent < kThreshold); + + var storedMessage = srm.Message; // on each destore; reduce recyclingcount so that when all instances are destored, the outgoing message can be recycled Interlocked.Decrement(ref storedMessage.m_recyclingCount); @@ -177,8 +182,9 @@ namespace Lidgren.Network // ack arrived right on time NetException.Assert(seqNr == m_windowStart); + bool resetTimeout; m_receivedAcks[m_windowStart] = false; - DestoreMessage(m_windowStart % m_windowSize); + DestoreMessage(now, m_windowStart % m_windowSize, out resetTimeout); m_windowStart = (m_windowStart + 1) % NetConstants.NumSequenceNumbers; // advance window if we already have early acks @@ -186,13 +192,16 @@ namespace Lidgren.Network { //m_connection.m_peer.LogDebug("Using early ack for #" + m_windowStart + "..."); m_receivedAcks[m_windowStart] = false; - DestoreMessage(m_windowStart % m_windowSize); + bool rt; + DestoreMessage(now, m_windowStart % m_windowSize, out rt); + resetTimeout |= rt; NetException.Assert(m_storedMessages[m_windowStart % m_windowSize].Message == null); // should already be destored m_windowStart = (m_windowStart + 1) % NetConstants.NumSequenceNumbers; //m_connection.m_peer.LogDebug("Advancing window to #" + m_windowStart); } - + if (resetTimeout) + m_connection.ResetTimeout(now); return; }