protected override void OnApplicationShutdown() { base.OnApplicationShutdown(); foreach (var client in _clients.ToList()) { client.Dispose(); } _udpServer.Dispose(); }
public void UdpAdapter_Communication_Is_One_On_One() { List <UdpAdapterEntry> entries = new List <UdpAdapterEntry>(); ResonanceUdpServer udp = new ResonanceUdpServer(9999); udp.ConnectionRequest += (x, e) => { var serverAdapter = e.Accept(); var entry = entries.Last(); entry.ServerAdapter = serverAdapter; serverAdapter.DataAvailable += (_, __) => { entry.MessageCount++; Task.Factory.StartNew(() => { serverAdapter.Write(new byte[] { 4, 3, 2, 1 }); }); }; serverAdapter.Connect(); }; udp.Start(); for (int i = 0; i < 10; i++) { UdpAdapter adapter = new UdpAdapter("127.0.0.1", 9999); var entry = new UdpAdapterEntry() { ClientAdapter = adapter }; entries.Add(entry); adapter.DataAvailable += (_, __) => { entry.MessageCount++; }; adapter.Connect(); Thread.Sleep(100); adapter.Write(new byte[] { 1, 2, 3, 4 }); } Thread.Sleep(5000); foreach (var entry in entries) { Assert.IsTrue(entry.MessageCount == 2); entry.ClientAdapter.Dispose(); entry.ServerAdapter.Dispose(); } udp.Dispose(); }
public void UdpAdapter_Throws_Exception_After_Server_Shutdown() { ResonanceUdpServer udp = new ResonanceUdpServer(9999); udp.Start(); udp.Dispose(); UdpAdapter adapter = new UdpAdapter("127.0.0.1", 9999); try { adapter.Connect(); Assert.Fail("Exception was not thrown."); } catch { } adapter.Dispose(); }
public void UdpAdapter_Connection_Success() { ResonanceUdpServer udp = new ResonanceUdpServer(9999); udp.ConnectionRequest += (x, e) => { e.Accept(); }; udp.Start(); UdpAdapter adapter = new UdpAdapter("127.0.0.1", 9999); adapter.Connect(); Assert.IsTrue(adapter.State == ResonanceComponentState.Connected); adapter.Dispose(); udp.Dispose(); }
public void UdpAdapter_Throws_Exception_On_Connection_Declined() { ResonanceUdpServer udp = new ResonanceUdpServer(9999); udp.ConnectionRequest += (x, e) => { e.Decline(); }; udp.Start(); UdpAdapter adapter = new UdpAdapter("127.0.0.1", 9999); try { adapter.Connect(); Assert.Fail("Exception was not thrown."); } catch { } adapter.Dispose(); udp.Dispose(); }