forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetStatistics.cs
More file actions
81 lines (69 loc) · 2.35 KB
/
NetStatistics.cs
File metadata and controls
81 lines (69 loc) · 2.35 KB
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
using System.Threading;
namespace LiteNetLib
{
public sealed class NetStatistics
{
private long _packetsSent;
private long _packetsReceived;
private long _bytesSent;
private long _bytesReceived;
private long _packetLoss;
public long PacketsSent => Interlocked.Read(ref _packetsSent);
public long PacketsReceived => Interlocked.Read(ref _packetsReceived);
public long BytesSent => Interlocked.Read(ref _bytesSent);
public long BytesReceived => Interlocked.Read(ref _bytesReceived);
public long PacketLoss => Interlocked.Read(ref _packetLoss);
public long PacketLossPercent
{
get
{
long sent = PacketsSent, loss = PacketLoss;
return sent == 0 ? 0 : loss * 100 / sent;
}
}
public void Reset()
{
Interlocked.Exchange(ref _packetsSent, 0);
Interlocked.Exchange(ref _packetsReceived, 0);
Interlocked.Exchange(ref _bytesSent, 0);
Interlocked.Exchange(ref _bytesReceived, 0);
Interlocked.Exchange(ref _packetLoss, 0);
}
public void IncrementPacketsSent()
{
Interlocked.Increment(ref _packetsSent);
}
public void IncrementPacketsReceived()
{
Interlocked.Increment(ref _packetsReceived);
}
public void AddBytesSent(long bytesSent)
{
Interlocked.Add(ref _bytesSent, bytesSent);
}
public void AddBytesReceived(long bytesReceived)
{
Interlocked.Add(ref _bytesReceived, bytesReceived);
}
public void IncrementPacketLoss()
{
Interlocked.Increment(ref _packetLoss);
}
public void AddPacketLoss(long packetLoss)
{
Interlocked.Add(ref _packetLoss, packetLoss);
}
public override string ToString()
{
return
string.Format(
"BytesReceived: {0}\nPacketsReceived: {1}\nBytesSent: {2}\nPacketsSent: {3}\nPacketLoss: {4}\nPacketLossPercent: {5}\n",
BytesReceived,
PacketsReceived,
BytesSent,
PacketsSent,
PacketLoss,
PacketLossPercent);
}
}
}