|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Sockets; |
| 5 | +using System.Threading; |
| 6 | +using NetCoreServer; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace tests |
| 10 | +{ |
| 11 | + class MulticastUdpClient : NetCoreServer.UdpClient |
| 12 | + { |
| 13 | + public bool Connected { get; set; } |
| 14 | + public bool Disconnected { get; set; } |
| 15 | + public bool Errors { get; set; } |
| 16 | + |
| 17 | + public MulticastUdpClient(string address, int port) : base(address, port) {} |
| 18 | + |
| 19 | + protected override void OnConnected() { Connected = true; Receive(); } |
| 20 | + protected override void OnDisconnected() { Disconnected = true; } |
| 21 | + protected override void OnReceived(IPEndPoint endpoint, byte[] buffer, long size) { Receive(); } |
| 22 | + protected override void OnError(SocketError error) { Errors = true; } |
| 23 | + } |
| 24 | + |
| 25 | + class MulticastUdpServer : UdpServer |
| 26 | + { |
| 27 | + public bool Started { get; set; } |
| 28 | + public bool Stopped { get; set; } |
| 29 | + public bool Errors { get; set; } |
| 30 | + |
| 31 | + public MulticastUdpServer(IPAddress address, int port) : base(address, port) {} |
| 32 | + |
| 33 | + protected override void OnStarted() { Started = true; Receive(); } |
| 34 | + protected override void OnStopped() { Stopped = true; } |
| 35 | + protected override void OnError(SocketError error) { Errors = true; } |
| 36 | + } |
| 37 | + |
| 38 | + public class UdpMulticastTests |
| 39 | + { |
| 40 | + [Fact(DisplayName = "UDP server multicast test")] |
| 41 | + public void UdpMulticastServerTest() |
| 42 | + { |
| 43 | + string listenAddress = "0.0.0.0"; |
| 44 | + string multicastAddress = "239.255.0.1"; |
| 45 | + int multicastPort = 3335; |
| 46 | + |
| 47 | + // Create and start multicast server |
| 48 | + var server = new MulticastUdpServer(IPAddress.Any, 0); |
| 49 | + Assert.True(server.Start(multicastAddress, multicastPort)); |
| 50 | + while (!server.IsStarted) |
| 51 | + Thread.Yield(); |
| 52 | + |
| 53 | + // Create and connect multicast client |
| 54 | + var client1 = new MulticastUdpClient(listenAddress, multicastPort); |
| 55 | + client1.SetupMulticast(true); |
| 56 | + Assert.True(client1.Connect()); |
| 57 | + while (!client1.IsConnected) |
| 58 | + Thread.Yield(); |
| 59 | + |
| 60 | + // Join multicast group |
| 61 | + client1.JoinMulticastGroup(multicastAddress); |
| 62 | + Thread.Sleep(100); |
| 63 | + |
| 64 | + // Multicast some data to all clients |
| 65 | + server.MulticastSync("test"); |
| 66 | + |
| 67 | + // Wait for all data processed... |
| 68 | + while (client1.BytesReceived != 4) |
| 69 | + Thread.Yield(); |
| 70 | + |
| 71 | + // Create and connect multicast client |
| 72 | + var client2 = new MulticastUdpClient(listenAddress, multicastPort); |
| 73 | + client2.SetupMulticast(true); |
| 74 | + Assert.True(client2.Connect()); |
| 75 | + while (!client2.IsConnected) |
| 76 | + Thread.Yield(); |
| 77 | + |
| 78 | + // Join multicast group |
| 79 | + client2.JoinMulticastGroup(multicastAddress); |
| 80 | + Thread.Sleep(100); |
| 81 | + |
| 82 | + // Multicast some data to all clients |
| 83 | + server.MulticastSync("test"); |
| 84 | + |
| 85 | + // Wait for all data processed... |
| 86 | + while ((client1.BytesReceived != 8) || (client2.BytesReceived != 4)) |
| 87 | + Thread.Yield(); |
| 88 | + |
| 89 | + // Create and connect multicast client |
| 90 | + var client3 = new MulticastUdpClient(listenAddress, multicastPort); |
| 91 | + client3.SetupMulticast(true); |
| 92 | + Assert.True(client3.Connect()); |
| 93 | + while (!client3.IsConnected) |
| 94 | + Thread.Yield(); |
| 95 | + |
| 96 | + // Join multicast group |
| 97 | + client3.JoinMulticastGroup(multicastAddress); |
| 98 | + Thread.Sleep(100); |
| 99 | + |
| 100 | + // Multicast some data to all clients |
| 101 | + server.MulticastSync("test"); |
| 102 | + |
| 103 | + // Wait for all data processed... |
| 104 | + while ((client1.BytesReceived != 12) || (client2.BytesReceived != 8) || (client3.BytesReceived != 4)) |
| 105 | + Thread.Yield(); |
| 106 | + |
| 107 | + // Leave multicast group |
| 108 | + client1.LeaveMulticastGroup(multicastAddress); |
| 109 | + Thread.Sleep(100); |
| 110 | + |
| 111 | + // Disconnect the multicast client |
| 112 | + Assert.True(client1.Disconnect()); |
| 113 | + while (client1.IsConnected) |
| 114 | + Thread.Yield(); |
| 115 | + |
| 116 | + // Multicast some data to all clients |
| 117 | + server.MulticastSync("test"); |
| 118 | + |
| 119 | + // Wait for all data processed... |
| 120 | + while ((client1.BytesReceived != 12) || (client2.BytesReceived != 12) || (client3.BytesReceived != 8)) |
| 121 | + Thread.Yield(); |
| 122 | + |
| 123 | + // Leave multicast group |
| 124 | + client2.LeaveMulticastGroup(multicastAddress); |
| 125 | + Thread.Sleep(100); |
| 126 | + |
| 127 | + // Disconnect the multicast client |
| 128 | + Assert.True(client2.Disconnect()); |
| 129 | + while (client2.IsConnected) |
| 130 | + Thread.Yield(); |
| 131 | + |
| 132 | + // Multicast some data to all clients |
| 133 | + server.MulticastSync("test"); |
| 134 | + |
| 135 | + // Wait for all data processed... |
| 136 | + while ((client1.BytesReceived != 12) || (client2.BytesReceived != 12) || (client3.BytesReceived != 12)) |
| 137 | + Thread.Yield(); |
| 138 | + |
| 139 | + // Leave multicast group |
| 140 | + client3.LeaveMulticastGroup(multicastAddress); |
| 141 | + Thread.Sleep(100); |
| 142 | + |
| 143 | + // Disconnect the multicast client |
| 144 | + Assert.True(client3.Disconnect()); |
| 145 | + while (client3.IsConnected) |
| 146 | + Thread.Yield(); |
| 147 | + |
| 148 | + // Stop the Echo server |
| 149 | + Assert.True(server.Stop()); |
| 150 | + while (server.IsStarted) |
| 151 | + Thread.Yield(); |
| 152 | + |
| 153 | + // Check the multicast server state |
| 154 | + Assert.True(server.Started); |
| 155 | + Assert.True(server.Stopped); |
| 156 | + Assert.True(server.BytesSent == 20); |
| 157 | + Assert.True(server.BytesReceived == 0); |
| 158 | + Assert.True(!server.Errors); |
| 159 | + |
| 160 | + // Check the multicast client state |
| 161 | + Assert.True(client1.BytesSent == 0); |
| 162 | + Assert.True(client2.BytesSent == 0); |
| 163 | + Assert.True(client3.BytesSent == 0); |
| 164 | + Assert.True(client1.BytesReceived == 12); |
| 165 | + Assert.True(client2.BytesReceived == 12); |
| 166 | + Assert.True(client3.BytesReceived == 12); |
| 167 | + Assert.True(!client1.Errors); |
| 168 | + Assert.True(!client2.Errors); |
| 169 | + Assert.True(!client3.Errors); |
| 170 | + } |
| 171 | + |
| 172 | + [Fact(DisplayName = "UDP server multicast random test")] |
| 173 | + public void UdpMulticastServerRandomTest() |
| 174 | + { |
| 175 | + string listenAddress = "0.0.0.0"; |
| 176 | + string multicastAddress = "239.255.0.1"; |
| 177 | + int multicastPort = 3336; |
| 178 | + |
| 179 | + // Create and start multicast server |
| 180 | + var server = new MulticastUdpServer(IPAddress.Any, 0); |
| 181 | + Assert.True(server.Start(multicastAddress, multicastPort)); |
| 182 | + while (!server.IsStarted) |
| 183 | + Thread.Yield(); |
| 184 | + |
| 185 | + // Test duration in seconds |
| 186 | + int duration = 10; |
| 187 | + |
| 188 | + // Clients collection |
| 189 | + var clients = new List<MulticastUdpClient>(); |
| 190 | + |
| 191 | + // Start random test |
| 192 | + var rand = new Random(); |
| 193 | + var start = DateTime.UtcNow; |
| 194 | + while ((DateTime.UtcNow - start).TotalSeconds < duration) |
| 195 | + { |
| 196 | + // Create a new client and connect |
| 197 | + if ((rand.Next() % 100) == 0) |
| 198 | + { |
| 199 | + if (clients.Count < 100) |
| 200 | + { |
| 201 | + // Create and connect Echo client |
| 202 | + var client = new MulticastUdpClient(listenAddress, multicastPort); |
| 203 | + clients.Add(client); |
| 204 | + client.SetupMulticast(true); |
| 205 | + client.Connect(); |
| 206 | + while (!client.IsConnected) |
| 207 | + Thread.Yield(); |
| 208 | + |
| 209 | + // Join multicast group |
| 210 | + client.JoinMulticastGroup(multicastAddress); |
| 211 | + Thread.Sleep(100); |
| 212 | + } |
| 213 | + } |
| 214 | + // Connect/Disconnect the random client |
| 215 | + else if ((rand.Next() % 100) == 0) |
| 216 | + { |
| 217 | + if (clients.Count > 0) |
| 218 | + { |
| 219 | + int index = rand.Next() % clients.Count; |
| 220 | + var client = clients[index]; |
| 221 | + if (client.IsConnected) |
| 222 | + { |
| 223 | + // Leave multicast group |
| 224 | + client.LeaveMulticastGroup(multicastAddress); |
| 225 | + Thread.Sleep(100); |
| 226 | + |
| 227 | + client.Disconnect(); |
| 228 | + while (client.IsConnected) |
| 229 | + Thread.Yield(); |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + client.Connect(); |
| 234 | + while (!client.IsConnected) |
| 235 | + Thread.Yield(); |
| 236 | + |
| 237 | + // Join multicast group |
| 238 | + client.JoinMulticastGroup(multicastAddress); |
| 239 | + Thread.Sleep(100); |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + // Multicast a message to all clients |
| 244 | + else if ((rand.Next() % 10) == 0) |
| 245 | + { |
| 246 | + server.MulticastSync("test"); |
| 247 | + } |
| 248 | + |
| 249 | + // Sleep for a while... |
| 250 | + Thread.Sleep(1); |
| 251 | + } |
| 252 | + |
| 253 | + // Disconnect clients |
| 254 | + foreach (var client in clients) |
| 255 | + { |
| 256 | + client.Disconnect(); |
| 257 | + while (client.IsConnected) |
| 258 | + Thread.Yield(); |
| 259 | + } |
| 260 | + |
| 261 | + // Stop the multicast server |
| 262 | + Assert.True(server.Stop()); |
| 263 | + while (server.IsStarted) |
| 264 | + Thread.Yield(); |
| 265 | + |
| 266 | + // Check the multicast server state |
| 267 | + Assert.True(server.Started); |
| 268 | + Assert.True(server.Stopped); |
| 269 | + Assert.True(server.BytesSent > 0); |
| 270 | + Assert.True(server.BytesReceived == 0); |
| 271 | + Assert.True(!server.Errors); |
| 272 | + } |
| 273 | + } |
| 274 | +} |
0 commit comments