forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeedBench.cs
More file actions
240 lines (196 loc) · 7.16 KB
/
Copy pathSpeedBench.cs
File metadata and controls
240 lines (196 loc) · 7.16 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using LiteNetLib;
using LiteNetLib.Utils;
namespace LibSample
{
internal class SpeedBench : IExample
{
public class Server : INetEventListener, IDisposable
{
public int ReliableReceived;
public int UnreliableReceived;
readonly NetManager _server;
public NetStatistics Stats
{
get { return _server.Statistics; }
}
public Server()
{
_server = new NetManager(this);
_server.AutoRecycle = true;
_server.UpdateTime = 1;
_server.SimulatePacketLoss = false;
_server.SimulationPacketLossChance = 20;
_server.EnableStatistics = true;
_server.Start(9050);
}
public void PollEvents()
{
_server.PollEvents();
}
void INetEventListener.OnNetworkError(IPEndPoint endPoint, SocketError socketErrorCode)
{
}
void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency)
{
}
public void OnConnectionRequest(ConnectionRequest request)
{
request.AcceptIfKey("ConnKey");
}
void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
{
var isReliable = reader.GetBool();
var data = reader.GetString();
if (isReliable)
{
ReliableReceived++;
}
else
{
UnreliableReceived++;
}
}
void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader,
UnconnectedMessageType messageType)
{
}
void INetEventListener.OnPeerConnected(NetPeer peer)
{
}
void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
}
public void Dispose()
{
_server.Stop();
}
}
public class Client : INetEventListener, IDisposable
{
public int ReliableSent;
public int UnreliableSent;
readonly NetManager _client;
readonly NetDataWriter _writer;
NetPeer _peer;
public NetStatistics Stats
{
get { return _client.Statistics; }
}
public Client()
{
_writer = new NetDataWriter();
_client = new NetManager(this);
_client.UnsyncedEvents = true;
_client.AutoRecycle = true;
_client.SimulatePacketLoss = false;
_client.SimulationPacketLossChance = 20;
_client.EnableStatistics = true;
_client.Start();
}
public void SendUnreliable(string pData)
{
_writer.Reset();
_writer.Put(false);
_writer.Put(pData);
_peer.Send(_writer, DeliveryMethod.Unreliable);
UnreliableSent++;
}
public void SendReliable(string pData)
{
_writer.Reset();
_writer.Put(true);
_writer.Put(pData);
_peer.Send(_writer, DeliveryMethod.ReliableOrdered);
ReliableSent++;
}
public void Connect()
{
_peer = _client.Connect("localhost", 9050, "ConnKey");
}
void INetEventListener.OnNetworkError(IPEndPoint endPoint, SocketError socketErrorCode)
{
}
void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency)
{
}
public void OnConnectionRequest(ConnectionRequest request)
{
request.AcceptIfKey("ConnKey");
}
void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
{
}
void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader,
UnconnectedMessageType messageType)
{
}
void INetEventListener.OnPeerConnected(NetPeer peer)
{
}
void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
}
public void Dispose()
{
_client.Stop();
}
}
private const string DATA = "The quick brown fox jumps over the lazy dog";
private const int MAX_LOOP_COUNT = 750;
private const int UNRELIABLE_MESSAGES_PER_LOOP = 1000;
private const int RELIABLE_MESSAGES_PER_LOOP = 350;
private static bool CLIENT_RUNNING = true;
public void Run()
{
Console.WriteLine("Testing LiteNetLib...");
Thread serverThread = new Thread(StartServer);
serverThread.Start();
Thread clientThread = new Thread(StartClient);
clientThread.Start();
Console.WriteLine("Processing...");
serverThread.Join();
clientThread.Join();
Console.WriteLine("Test is end.");
}
private static void StartServer()
{
using (Server s = new Server())
{
while (CLIENT_RUNNING)
{
s.PollEvents();
Thread.Sleep(1);
}
Thread.Sleep(10000);
s.PollEvents();
Console.WriteLine("SERVER RECEIVED -> Reliable: " + s.ReliableReceived + ", Unreliable: " + s.UnreliableReceived);
Console.WriteLine("SERVER STATS:\n" + s.Stats);
}
}
private static void StartClient()
{
using (Client c = new Client())
{
c.Connect();
for (int i = 0; i < MAX_LOOP_COUNT; i++)
{
for (int ui = 0; ui < UNRELIABLE_MESSAGES_PER_LOOP; ui++)
c.SendUnreliable(DATA);
for (int ri = 0; ri < RELIABLE_MESSAGES_PER_LOOP; ri++)
c.SendReliable(DATA);
}
int dataSize = MAX_LOOP_COUNT * Encoding.UTF8.GetByteCount(DATA) * (UNRELIABLE_MESSAGES_PER_LOOP + RELIABLE_MESSAGES_PER_LOOP);
Console.WriteLine("DataSize: {0}b, {1}kb, {2}mb", dataSize, dataSize / 1024, dataSize / 1024 / 1024);
Thread.Sleep(10000);
CLIENT_RUNNING = false;
Console.WriteLine("CLIENT SENT -> Reliable: " + c.ReliableSent + ", Unreliable: " + c.UnreliableSent);
Console.WriteLine("CLIENT STATS:\n" + c.Stats);
}
}
}
}