public ValueTask StartAsync(ObjectDelivererProtocol protocol, IPacketRule packetRule, IDeliveryBox <T>?deliveryBox = null) { if (protocol == null || packetRule == null) { return(default(ValueTask)); } this.currentProtocol = protocol; this.currentProtocol.SetPacketRule(packetRule); this.deliveryBox = deliveryBox; this.currentProtocol.Connected.Subscribe(x => { this.ConnectedList.Add(x.Target); this.connected.OnNext(x); }); this.currentProtocol.Disconnected.Subscribe(x => { this.ConnectedList.Remove(x.Target); this.disconnected.OnNext(x); }); this.currentProtocol.ReceiveData.Subscribe(x => { var data = new DeliverData <T>() { Sender = x.Sender, Buffer = x.Buffer, }; if (deliveryBox != null) { data.Message = deliveryBox.BufferToMessage(x.Buffer); } this.receiveData.OnNext(data); }); this.ConnectedList.Clear(); return(this.currentProtocol.StartAsync()); }
private async Task TestLogFileAsync(IPacketRule packetRule) { var tempFilePath = System.IO.Path.GetTempFileName(); { CountdownEvent condition0 = new CountdownEvent(1); var sender = new ProtocolLogWriter() { FilePath = tempFilePath, }; sender.SetPacketRule(packetRule.Clone()); sender.Connected.Subscribe(x => condition0.Signal()); await sender.StartAsync(); if (!condition0.Wait(1000)) { Assert.Fail(); } var expected = new byte[] { 1, 2, 3 }; for (byte i = 100; i > 0; --i) { expected[0] = i; await sender.SendAsync(expected); } await sender.DisposeAsync(); } { CountdownEvent condition0 = new CountdownEvent(1); var reader = new ProtocolLogReader() { FilePath = tempFilePath, CutFirstInterval = true, }; reader.SetPacketRule(packetRule.Clone()); reader.Connected.Subscribe(x => condition0.Signal()); using (var condition = new CountdownEvent(100)) using (reader.ReceiveData.Subscribe(x => { var expected2 = new byte[] { (byte)condition.CurrentCount, 2, 3 }; Assert.IsTrue(x.Buffer.ToArray().SequenceEqual(expected2)); condition.Signal(); System.Diagnostics.Debug.WriteLine(condition.CurrentCount); })) { await reader.StartAsync(); if (!condition0.Wait(1000)) { Assert.Fail(); } if (!condition.Wait(3000)) { Assert.Fail(); } } await reader.DisposeAsync(); } }
public void SetPacketRule(IPacketRule packetRule) { this.PacketRule = packetRule; packetRule.Initialize(); }
private async Task TestUDPAsync(IPacketRule packetRule) { CountdownEvent condition0 = new CountdownEvent(2); var receiver = new ProtocolUdpSocketReceiver() { BoundPort = 9013, }; receiver.SetPacketRule(packetRule.Clone()); var sender = new ProtocolUdpSocketSender() { DestinationIpAddress = "127.0.0.1", DestinationPort = 9013, }; sender.SetPacketRule(packetRule.Clone()); using (receiver.Connected.Subscribe(x => condition0.Signal())) using (sender.Connected.Subscribe(x => condition0.Signal())) { await sender.StartAsync(); await receiver.StartAsync(); if (!condition0.Wait(3000)) { Assert.Fail(); } } { var expected = new byte[] { 1, 2, 3 }; using (var condition = new CountdownEvent(100)) using (receiver.ReceiveData.Subscribe(x => { var expected2 = new byte[] { (byte)condition.CurrentCount, 2, 3 }; Assert.IsTrue(x.Buffer.ToArray().SequenceEqual(expected2)); condition.Signal(); })) { for (byte i = 100; i > 0; --i) { expected[0] = i; await sender.SendAsync(expected); var sw = System.Diagnostics.Stopwatch.StartNew(); await Task.Run(async() => { while (condition.CurrentCount != i - 1 && sw.ElapsedMilliseconds < 3000) { await Task.Delay(1); } if (condition.CurrentCount != i - 1) { Assert.Fail(); } }); } if (!condition.Wait(3000)) { Assert.Fail(); } } } await sender.DisposeAsync(); await receiver.DisposeAsync(); }
private async Task TestSharedMemoryAsync(IPacketRule packetRule) { CountdownEvent condition0 = new CountdownEvent(2); var sender = new ProtocolSharedMemory() { SharedMemoryName = "test_shared_memory", SharedMemorySize = 10, }; sender.SetPacketRule(packetRule.Clone()); var receiver = new ProtocolSharedMemory() { SharedMemoryName = "test_shared_memory", SharedMemorySize = 10, }; receiver.SetPacketRule(packetRule.Clone()); using (sender.Connected.Subscribe(x => condition0.Signal())) using (receiver.Connected.Subscribe(x => condition0.Signal())) { await sender.StartAsync(); await receiver.StartAsync(); if (!condition0.Wait(1000)) { Assert.Fail(); } } { var expected = new byte[] { 1, 2, 3 }; using (var condition = new CountdownEvent(1)) using (receiver.ReceiveData.Subscribe(x => { Assert.IsTrue(x.Buffer.ToArray().SequenceEqual(expected)); condition.Signal(); })) { await sender.SendAsync(expected); if (!condition.Wait(3000)) { Assert.Fail(); } } } { var expected = new byte[] { 1, 2, 3 }; using (var condition = new CountdownEvent(1)) using (sender.ReceiveData.Subscribe(x => { Assert.IsTrue(x.Buffer.ToArray().SequenceEqual(expected)); condition.Signal(); })) { await receiver.SendAsync(expected); if (!condition.Wait(3000)) { Assert.Fail(); } } } await sender.DisposeAsync(); await receiver.DisposeAsync(); }
private async Task TestTCPAsync(IPacketRule packetRule) { CountdownEvent condition0 = new CountdownEvent(2); var client = new ProtocolTcpIpClient() { IpAddress = "127.0.0.1", Port = 9013, AutoConnectAfterDisconnect = true, }; client.SetPacketRule(packetRule.Clone()); var server = new ProtocolTcpIpServer() { ListenPort = 9013, }; server.SetPacketRule(packetRule.Clone()); using (client.Connected.Subscribe(x => condition0.Signal())) using (server.Connected.Subscribe(x => condition0.Signal())) { await server.StartAsync(); await client.StartAsync(); if (!condition0.Wait(1000)) { Assert.Fail(); } } { var expected = new byte[] { 1, 2, 3 }; using (var condition = new CountdownEvent(100)) using (server.ReceiveData.Subscribe(x => { var expected2 = new byte[] { (byte)condition.CurrentCount, 2, 3 }; Assert.IsTrue(x.Buffer.ToArray().SequenceEqual(expected2)); condition.Signal(); })) { for (byte i = 100; i > 0; --i) { expected[0] = i; await client.SendAsync(expected); } if (!condition.Wait(3000)) { Assert.Fail(); } } } { var expected = new byte[] { 1, 2, 3 }; using (var condition = new CountdownEvent(100)) using (client.ReceiveData.Subscribe(x => { var expected2 = new byte[] { (byte)condition.CurrentCount, 2, 3 }; Assert.IsTrue(x.Buffer.ToArray().SequenceEqual(expected2)); condition.Signal(); })) { for (byte i = 100; i > 0; --i) { expected[0] = i; await server.SendAsync(expected); } if (!condition.Wait(3000)) { Assert.Fail(); } } } { using (var condition = new CountdownEvent(1)) using (client.Disconnected.Subscribe(x => condition.Signal())) { await server.DisposeAsync(); if (!condition.Wait(3000)) { Assert.Fail(); } } } { using (var condition = new CountdownEvent(1)) using (client.Connected.Subscribe(x => condition.Signal())) { server = new ProtocolTcpIpServer() { ListenPort = 9013, }; server.SetPacketRule(packetRule.Clone()); await server.StartAsync(); if (!condition.Wait(5000)) { Assert.Fail(); } } using (var condition = new CountdownEvent(1)) using (server.Disconnected.Subscribe(x => condition.Signal())) { await client.DisposeAsync(); if (!condition.Wait(3000)) { Assert.Fail(); } } } await client.DisposeAsync(); await server.DisposeAsync(); }