From 6a0abf25c3d52a90d7ba8547b84fa01b9d393b45 Mon Sep 17 00:00:00 2001 From: Michael Lidgren Date: Fri, 6 Feb 2015 19:23:32 +0100 Subject: [PATCH] Fixed issue with recycling count and resent messages --- Lidgren.Network/NetPeer.MessagePools.cs | 10 +++++++--- Lidgren.Network/NetPeer.Send.cs | 3 +++ Lidgren.Network/NetReliableSenderChannel.cs | 9 ++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Lidgren.Network/NetPeer.MessagePools.cs b/Lidgren.Network/NetPeer.MessagePools.cs index 49aaa2b..0798fe6 100644 --- a/Lidgren.Network/NetPeer.MessagePools.cs +++ b/Lidgren.Network/NetPeer.MessagePools.cs @@ -187,10 +187,14 @@ namespace Lidgren.Network { if (m_outgoingMessagesPool == null) return; - - NetException.Assert(msg.m_recyclingCount == 0, "Wrong recycling count! Should be zero; found " + msg.m_recyclingCount); - +#if DEBUG NetException.Assert(m_outgoingMessagesPool.Contains(msg) == false, "Recyling already recycled message! Thread race?"); + if (msg.m_recyclingCount != 0) + LogWarning("Wrong recycling count! should be zero; found " + msg.m_recyclingCount); +#endif + // setting m_recyclingCount to zero SHOULD be an unnecessary maneuver, if it's not zero something is wrong + // however, in RELEASE, we'll just have to accept this and move on with life + msg.m_recyclingCount = 0; byte[] storage = msg.m_data; msg.m_data = null; diff --git a/Lidgren.Network/NetPeer.Send.cs b/Lidgren.Network/NetPeer.Send.cs index c75db17..80b868f 100644 --- a/Lidgren.Network/NetPeer.Send.cs +++ b/Lidgren.Network/NetPeer.Send.cs @@ -220,7 +220,10 @@ namespace Lidgren.Network msg.m_isSent = true; if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.UnconnectedData) == false) + { + Interlocked.Decrement(ref msg.m_recyclingCount); return; // dropping unconnected message since it's not enabled for receiving + } NetIncomingMessage om = CreateIncomingMessage(NetIncomingMessageType.UnconnectedData, msg.LengthBytes); om.Write(msg); diff --git a/Lidgren.Network/NetReliableSenderChannel.cs b/Lidgren.Network/NetReliableSenderChannel.cs index a4a16db..1f9a13a 100644 --- a/Lidgren.Network/NetReliableSenderChannel.cs +++ b/Lidgren.Network/NetReliableSenderChannel.cs @@ -66,11 +66,12 @@ namespace Lidgren.Network // for (int i = 0; i < m_storedMessages.Length; i++) { - NetOutgoingMessage om = m_storedMessages[i].Message; + var storedMsg = m_storedMessages[i]; + NetOutgoingMessage om = storedMsg.Message; if (om == null) continue; - double t = m_storedMessages[i].LastSent; + double t = storedMsg.LastSent; if (t > 0 && (now - t) > m_resendDelay) { // deduce sequence number @@ -89,7 +90,8 @@ namespace Lidgren.Network //m_connection.m_peer.LogVerbose("Resending due to delay #" + m_storedMessages[i].SequenceNumber + " " + om.ToString()); m_connection.m_statistics.MessageResent(MessageResendReason.Delay); - m_connection.QueueSendMessage(om, m_storedMessages[i].SequenceNumber); + Interlocked.Increment(ref om.m_recyclingCount); // increment this since it's being decremented in QueueSendMessage + m_connection.QueueSendMessage(om, storedMsg.SequenceNumber); m_storedMessages[i].LastSent = now; m_storedMessages[i].NumSent++; @@ -247,6 +249,7 @@ namespace Lidgren.Network m_storedMessages[slot].LastSent = now; m_storedMessages[slot].NumSent++; m_connection.m_statistics.MessageResent(MessageResendReason.HoleInSequence); + Interlocked.Increment(ref rmsg.m_recyclingCount); // increment this since it's being decremented in QueueSendMessage m_connection.QueueSendMessage(rmsg, rnr); } }