forked from space-wizards/RobustToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetPeerStatistics.cs
180 lines (152 loc) · 5.1 KB
/
NetPeerStatistics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright (c) 2010 Michael Lidgren
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
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.Text;
using System.Diagnostics;
namespace Lidgren.Network
{
/// <summary>
/// Statistics for a NetPeer instance
/// </summary>
public sealed class NetPeerStatistics
{
private readonly NetPeer m_peer;
internal int m_sentPackets;
internal int m_receivedPackets;
internal int m_sentMessages;
internal int m_receivedMessages;
internal int m_receivedFragments;
internal int m_sentBytes;
internal int m_receivedBytes;
internal NetPeerStatistics(NetPeer peer)
{
m_peer = peer;
Reset();
}
internal void Reset()
{
m_sentPackets = 0;
m_receivedPackets = 0;
m_sentMessages = 0;
m_receivedMessages = 0;
m_receivedFragments = 0;
m_sentBytes = 0;
m_receivedBytes = 0;
}
/// <summary>
/// Gets the number of sent packets since the NetPeer was initialized
/// </summary>
public int SentPackets => m_sentPackets;
/// <summary>
/// Gets the number of received packets since the NetPeer was initialized
/// </summary>
public int ReceivedPackets => m_receivedPackets;
/// <summary>
/// Gets the number of sent messages since the NetPeer was initialized
/// </summary>
public int SentMessages => m_sentMessages;
/// <summary>
/// Gets the number of received messages since the NetPeer was initialized
/// </summary>
public int ReceivedMessages => m_receivedMessages;
/// <summary>
/// Gets the number of received fragments since the NetPeer was initialized
/// </summary>
public int ReceivedFragments => m_receivedFragments;
/// <summary>
/// Gets the number of sent bytes since the NetPeer was initialized
/// </summary>
public int SentBytes => m_sentBytes;
/// <summary>
/// Gets the number of received bytes since the NetPeer was initialized
/// </summary>
public int ReceivedBytes => m_receivedBytes;
public int ResentMessages => ResentMessagesDueToDelays + ResentMessagesDueToHoles;
public int ResentMessagesDueToHoles { get; private set; }
public int ResentMessagesDueToDelays { get; private set; }
public int DroppedMessages { get; private set; }
/// <summary>
/// Gets the number of unsent and stored messages for this peer
/// </summary>
public void GetUnsentAndStoredMessages(out int numUnsent, out int numStored)
{
numUnsent = 0;
numStored = 0;
lock (m_peer.m_connections)
{
foreach (var connection in m_peer.m_connections)
{
connection.Statistics.GetUnsentAndStoredMessages(out var cUnsent, out var cStored);
numUnsent += cUnsent;
numStored += cStored;
}
}
}
#if !USE_RELEASE_STATISTICS
[Conditional("DEBUG")]
#endif
internal void PacketSent(int numBytes, int numMessages)
{
m_sentPackets++;
m_sentBytes += numBytes;
m_sentMessages += numMessages;
}
#if !USE_RELEASE_STATISTICS
[Conditional("DEBUG")]
#endif
internal void PacketReceived(int numBytes, int numMessages, int numFragments)
{
m_receivedPackets++;
m_receivedBytes += numBytes;
m_receivedMessages += numMessages;
m_receivedFragments += numFragments;
}
#if !USE_RELEASE_STATISTICS
[Conditional("DEBUG")]
#endif
internal void MessageResent(MessageResendReason reason)
{
if (reason == MessageResendReason.Delay)
ResentMessagesDueToDelays++;
else
ResentMessagesDueToHoles++;
}
#if !USE_RELEASE_STATISTICS
[Conditional("DEBUG")]
#endif
internal void MessageDropped()
{
DroppedMessages++;
}
/// <summary>
/// Returns a string that represents this object
/// </summary>
public override string ToString()
{
#if DEBUG || USE_RELEASE_STATISTICS
return @$"{m_peer.ConnectionsCount} connections
Sent {m_sentBytes} bytes in {m_sentMessages} messages in {m_sentPackets} packets
Received {m_receivedBytes} bytes in {m_receivedMessages} messages (of which {m_receivedFragments} fragments) in {m_receivedPackets} packets";
#else
return @$"{m_peer.ConnectionsCount} connections
Sent (n/a) bytes in (n/a) messages in (n/a) packets
Received (n/a) bytes in (n/a) messages in (n/a) packets";
#endif
}
}
}