forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdpMulticastTests.cs
274 lines (229 loc) · 9.75 KB
/
UdpMulticastTests.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using NetCoreServer;
using Xunit;
namespace tests
{
class MulticastUdpClient : NetCoreServer.UdpClient
{
public bool Connected { get; set; }
public bool Disconnected { get; set; }
public bool Errors { get; set; }
public MulticastUdpClient(string address, int port) : base(address, port) {}
protected override void OnConnected() { Connected = true; ReceiveAsync(); }
protected override void OnDisconnected() { Disconnected = true; }
protected override void OnReceived(EndPoint endpoint, byte[] buffer, long offset, long size) { ReceiveAsync(); }
protected override void OnError(SocketError error) { Errors = true; }
}
class MulticastUdpServer : UdpServer
{
public bool Started { get; set; }
public bool Stopped { get; set; }
public bool Errors { get; set; }
public MulticastUdpServer(IPAddress address, int port) : base(address, port) {}
protected override void OnStarted() { Started = true; ReceiveAsync(); }
protected override void OnStopped() { Stopped = true; }
protected override void OnError(SocketError error) { Errors = true; }
}
public class UdpMulticastTests
{
[Fact(DisplayName = "UDP server multicast test")]
public void UdpMulticastServerTest()
{
string listenAddress = "0.0.0.0";
string multicastAddress = "239.255.0.1";
int multicastPort = 3335;
// Create and start multicast server
var server = new MulticastUdpServer(IPAddress.Any, 0);
Assert.True(server.Start(multicastAddress, multicastPort));
while (!server.IsStarted)
Thread.Yield();
// Create and connect multicast client
var client1 = new MulticastUdpClient(listenAddress, multicastPort);
client1.SetupMulticast(true);
Assert.True(client1.Connect());
while (!client1.IsConnected)
Thread.Yield();
// Join multicast group
client1.JoinMulticastGroup(multicastAddress);
Thread.Sleep(100);
// Multicast some data to all clients
server.Multicast("test");
// Wait for all data processed...
while (client1.BytesReceived != 4)
Thread.Yield();
// Create and connect multicast client
var client2 = new MulticastUdpClient(listenAddress, multicastPort);
client2.SetupMulticast(true);
Assert.True(client2.Connect());
while (!client2.IsConnected)
Thread.Yield();
// Join multicast group
client2.JoinMulticastGroup(multicastAddress);
Thread.Sleep(100);
// Multicast some data to all clients
server.Multicast("test");
// Wait for all data processed...
while ((client1.BytesReceived != 8) || (client2.BytesReceived != 4))
Thread.Yield();
// Create and connect multicast client
var client3 = new MulticastUdpClient(listenAddress, multicastPort);
client3.SetupMulticast(true);
Assert.True(client3.Connect());
while (!client3.IsConnected)
Thread.Yield();
// Join multicast group
client3.JoinMulticastGroup(multicastAddress);
Thread.Sleep(100);
// Multicast some data to all clients
server.Multicast("test");
// Wait for all data processed...
while ((client1.BytesReceived != 12) || (client2.BytesReceived != 8) || (client3.BytesReceived != 4))
Thread.Yield();
// Leave multicast group
client1.LeaveMulticastGroup(multicastAddress);
Thread.Sleep(100);
// Disconnect the multicast client
Assert.True(client1.Disconnect());
while (client1.IsConnected)
Thread.Yield();
// Multicast some data to all clients
server.Multicast("test");
// Wait for all data processed...
while ((client1.BytesReceived != 12) || (client2.BytesReceived != 12) || (client3.BytesReceived != 8))
Thread.Yield();
// Leave multicast group
client2.LeaveMulticastGroup(multicastAddress);
Thread.Sleep(100);
// Disconnect the multicast client
Assert.True(client2.Disconnect());
while (client2.IsConnected)
Thread.Yield();
// Multicast some data to all clients
server.Multicast("test");
// Wait for all data processed...
while ((client1.BytesReceived != 12) || (client2.BytesReceived != 12) || (client3.BytesReceived != 12))
Thread.Yield();
// Leave multicast group
client3.LeaveMulticastGroup(multicastAddress);
Thread.Sleep(100);
// Disconnect the multicast client
Assert.True(client3.Disconnect());
while (client3.IsConnected)
Thread.Yield();
// Stop the Echo server
Assert.True(server.Stop());
while (server.IsStarted)
Thread.Yield();
// Check the multicast server state
Assert.True(server.Started);
Assert.True(server.Stopped);
Assert.True(server.BytesSent > 0);
Assert.True(server.BytesReceived == 0);
Assert.True(!server.Errors);
// Check the multicast client state
Assert.True(client1.BytesSent == 0);
Assert.True(client2.BytesSent == 0);
Assert.True(client3.BytesSent == 0);
Assert.True(client1.BytesReceived == 12);
Assert.True(client2.BytesReceived == 12);
Assert.True(client3.BytesReceived == 12);
Assert.True(!client1.Errors);
Assert.True(!client2.Errors);
Assert.True(!client3.Errors);
}
[Fact(DisplayName = "UDP server multicast random test")]
public void UdpMulticastServerRandomTest()
{
string listenAddress = "0.0.0.0";
string multicastAddress = "239.255.0.1";
int multicastPort = 3336;
// Create and start multicast server
var server = new MulticastUdpServer(IPAddress.Any, 0);
Assert.True(server.Start(multicastAddress, multicastPort));
while (!server.IsStarted)
Thread.Yield();
// Test duration in seconds
int duration = 10;
// Clients collection
var clients = new List<MulticastUdpClient>();
// Start random test
var rand = new Random();
var start = DateTime.UtcNow;
while ((DateTime.UtcNow - start).TotalSeconds < duration)
{
// Create a new client and connect
if ((rand.Next() % 100) == 0)
{
if (clients.Count < 100)
{
// Create and connect Echo client
var client = new MulticastUdpClient(listenAddress, multicastPort);
clients.Add(client);
client.SetupMulticast(true);
client.Connect();
while (!client.IsConnected)
Thread.Yield();
// Join multicast group
client.JoinMulticastGroup(multicastAddress);
Thread.Sleep(100);
}
}
// Connect/Disconnect the random client
else if ((rand.Next() % 100) == 0)
{
if (clients.Count > 0)
{
int index = rand.Next() % clients.Count;
var client = clients[index];
if (client.IsConnected)
{
// Leave multicast group
client.LeaveMulticastGroup(multicastAddress);
Thread.Sleep(100);
client.Disconnect();
while (client.IsConnected)
Thread.Yield();
}
else
{
client.Connect();
while (!client.IsConnected)
Thread.Yield();
// Join multicast group
client.JoinMulticastGroup(multicastAddress);
Thread.Sleep(100);
}
}
}
// Multicast a message to all clients
else if ((rand.Next() % 10) == 0)
{
server.Multicast("test");
}
// Sleep for a while...
Thread.Sleep(1);
}
// Disconnect clients
foreach (var client in clients)
{
client.Disconnect();
while (client.IsConnected)
Thread.Yield();
}
// Stop the multicast server
Assert.True(server.Stop());
while (server.IsStarted)
Thread.Yield();
// Check the multicast server state
Assert.True(server.Started);
Assert.True(server.Stopped);
Assert.True(server.BytesSent > 0);
Assert.True(server.BytesReceived == 0);
Assert.True(!server.Errors);
}
}
}