From 5b4a4a7fc5bf08ebc064033fd0047fe0b38daba4 Mon Sep 17 00:00:00 2001 From: lidgren Date: Sat, 26 Mar 2011 08:18:21 +0000 Subject: [PATCH] Statistics fixed and USE_RELEASE_STATISTICS defines added --- Lidgren.Network/NetConnectionStatistics.cs | 34 ++++++++++++++++++++++ Lidgren.Network/NetPeer.Internal.cs | 7 +++++ Lidgren.Network/NetPeerStatistics.cs | 22 ++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Lidgren.Network/NetConnectionStatistics.cs b/Lidgren.Network/NetConnectionStatistics.cs index 3b51247..88498ff 100644 --- a/Lidgren.Network/NetConnectionStatistics.cs +++ b/Lidgren.Network/NetConnectionStatistics.cs @@ -16,6 +16,10 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRA TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +// Uncomment the line below to get statistics in RELEASE builds +//#define USE_RELEASE_STATISTICS + using System; using System.Collections.Generic; using System.Text; @@ -89,6 +93,15 @@ namespace Lidgren.Network // public double LastSendRespondedTo { get { return m_connection.m_lastSendRespondedTo; } } +#if USE_RELEASE_STATISTICS + internal void PacketSent(int numBytes, int numMessages) + { + NetException.Assert(numBytes > 0 && numMessages > 0); + m_sentPackets++; + m_sentBytes += numBytes; + m_sentMessages += numMessages; + } +#else [Conditional("DEBUG")] internal void PacketSent(int numBytes, int numMessages) { @@ -97,7 +110,17 @@ namespace Lidgren.Network m_sentBytes += numBytes; m_sentMessages += numMessages; } +#endif +#if USE_RELEASE_STATISTICS + internal void PacketReceived(int numBytes, int numMessages) + { + NetException.Assert(numBytes > 0 && numMessages > 0); + m_receivedPackets++; + m_receivedBytes += numBytes; + m_receivedMessages += numMessages; + } +#else [Conditional("DEBUG")] internal void PacketReceived(int numBytes, int numMessages) { @@ -106,7 +129,17 @@ namespace Lidgren.Network m_receivedBytes += numBytes; m_receivedMessages += numMessages; } +#endif +#if USE_RELEASE_STATISTICS + internal void MessageResent(MessageResendReason reason) + { + if (reason == MessageResendReason.Delay) + m_resentMessagesDueToDelay++; + else + m_resentMessagesDueToHole++; + } +#else [Conditional("DEBUG")] internal void MessageResent(MessageResendReason reason) { @@ -115,6 +148,7 @@ namespace Lidgren.Network else m_resentMessagesDueToHole++; } +#endif /// /// Returns a string that represents this object diff --git a/Lidgren.Network/NetPeer.Internal.cs b/Lidgren.Network/NetPeer.Internal.cs index 385d93d..9365b8f 100644 --- a/Lidgren.Network/NetPeer.Internal.cs +++ b/Lidgren.Network/NetPeer.Internal.cs @@ -321,6 +321,7 @@ namespace Lidgren.Network // // parse packet into messages // + int numMessages = 0; int ptr = 0; while ((bytesReceived - ptr) >= NetConstants.HeaderByteSize) { @@ -330,6 +331,8 @@ namespace Lidgren.Network // 15 bits - Sequence number // 16 bits - Payload length in bits + numMessages++; + NetMessageType tp = (NetMessageType)m_receiveBuffer[ptr++]; byte low = m_receiveBuffer[ptr++]; @@ -401,6 +404,10 @@ namespace Lidgren.Network } ptr += payloadByteLength; } + + m_statistics.PacketReceived(bytesReceived, numMessages); + if (sender != null) + sender.m_statistics.PacketReceived(bytesReceived, numMessages); } private void ReceivedUnconnectedLibraryMessage(double now, IPEndPoint senderEndpoint, NetMessageType tp, int ptr, int payloadByteLength) diff --git a/Lidgren.Network/NetPeerStatistics.cs b/Lidgren.Network/NetPeerStatistics.cs index 1891549..c622792 100644 --- a/Lidgren.Network/NetPeerStatistics.cs +++ b/Lidgren.Network/NetPeerStatistics.cs @@ -17,6 +17,10 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR TH USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +// Uncomment the line below to get statistics in RELEASE builds +//#define USE_RELEASE_STATISTICS + using System; using System.Text; using System.Diagnostics; @@ -101,6 +105,14 @@ namespace Lidgren.Network /// public int BytesInRecyclePool { get { return m_peer.m_storagePoolBytes; } } +#if USE_RELEASE_STATISTICS + internal void PacketSent(int numBytes, int numMessages) + { + m_sentPackets++; + m_sentBytes += numBytes; + m_sentMessages += numMessages; + } +#else [Conditional("DEBUG")] internal void PacketSent(int numBytes, int numMessages) { @@ -108,7 +120,16 @@ namespace Lidgren.Network m_sentBytes += numBytes; m_sentMessages += numMessages; } +#endif +#if USE_RELEASE_STATISTICS + internal void PacketReceived(int numBytes, int numMessages) + { + m_receivedPackets++; + m_receivedBytes += numBytes; + m_receivedMessages += numMessages; + } +#else [Conditional("DEBUG")] internal void PacketReceived(int numBytes, int numMessages) { @@ -116,6 +137,7 @@ namespace Lidgren.Network m_receivedBytes += numBytes; m_receivedMessages += numMessages; } +#endif /// /// Returns a string that represents this object