public async System.Threading.Tasks.Task Test() { var listener = new Regulus.Network.Web.Listener(); listener.Bind("http://127.0.0.1:12345/"); var peers = new System.Collections.Concurrent.ConcurrentQueue <Web.Peer>(); listener.AcceptEvent += peers.Enqueue; var connecter = new Regulus.Network.Web.Connecter(new System.Net.WebSockets.ClientWebSocket()); var connectResult = await connecter.ConnectAsync("ws://127.0.0.1:12345/"); Xunit.Assert.True(connectResult); var ar = new Regulus.Utility.AutoPowerRegulator(new Utility.PowerRegulator()); Web.Peer peer; while (!peers.TryDequeue(out peer)) { ar.Operate(); } IStreamable server = peer; var serverReceiveBuffer = new byte[5]; var serverReceiveTask = server.Receive(serverReceiveBuffer, 0, 5); IStreamable client = connecter; var clientSendCount = await client.Send(new byte[] { 1, 2, 3, 4, 5 }, 0, 5); var serverReceiveCount = await serverReceiveTask; Xunit.Assert.Equal(5, serverReceiveCount); Xunit.Assert.Equal(5, clientSendCount); }
public async void Push(System.Collections.Generic.IEnumerable <TPackage> packages) { var buffer = _CreateBuffer(packages.ToArray()); if (buffer.Length == 0) { return; } IWaitableValue <int> wv = null; try { wv = _Peer.Send(buffer, 0, buffer.Length); } catch (System.Exception ex) { Regulus.Utility.Log.Instance.WriteInfo(ex.ToString()); ErrorEvent(); } if (wv == null) { return; } var sendCount = await wv; _WriteEnd(sendCount); }
public async void CommunicationDevicePopTest() { byte[] sendBuf = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; byte[] recvBuf = new byte[10]; Stream cd = new Regulus.Remote.Standalone.Stream(); IStreamable peer = cd as IStreamable; var result1 = peer.Send(sendBuf, 0, 4); int sendResult1 = await result1; var result2 = peer.Send(sendBuf, 4, 6); int sendResult2 = await result2; var streamTask1 = cd.Pop(recvBuf, 0, 3); int stream1 = await streamTask1; var streamTask2 = cd.Pop(recvBuf, stream1, recvBuf.Length - stream1); int stream2 = await streamTask2; var streamTask3 = cd.Pop(recvBuf, stream1 + stream2, recvBuf.Length - (stream1 + stream2)); int stream3 = await streamTask3; Assert.Equal(10, stream3 + stream2 + stream1); Assert.Equal((byte)0, recvBuf[0]); Assert.Equal((byte)1, recvBuf[1]); Assert.Equal((byte)2, recvBuf[2]); Assert.Equal((byte)3, recvBuf[3]); Assert.Equal((byte)4, recvBuf[4]); Assert.Equal((byte)5, recvBuf[5]); Assert.Equal((byte)6, recvBuf[6]); Assert.Equal((byte)7, recvBuf[7]); Assert.Equal((byte)8, recvBuf[8]); Assert.Equal((byte)9, recvBuf[9]); }
public void CommunicationDevicePopTest() { byte[] sendBuf = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; byte[] recvBuf = new byte[10]; Stream cd = new Regulus.Remote.Standalone.Stream(); IStreamable peer = cd as IStreamable; System.Threading.Tasks.Task <int> result1 = peer.Send(sendBuf, 0, 4); int sendResult1 = result1.GetAwaiter().GetResult(); System.Threading.Tasks.Task <int> result2 = peer.Send(sendBuf, 4, 6); int sendResult2 = result2.GetAwaiter().GetResult(); System.Threading.Tasks.Task <int> streamTask1 = cd.Pop(recvBuf, 0, 3); int stream1 = streamTask1.GetAwaiter().GetResult(); System.Threading.Tasks.Task <int> streamTask2 = cd.Pop(recvBuf, stream1, recvBuf.Length - stream1); int stream2 = streamTask2.GetAwaiter().GetResult(); System.Threading.Tasks.Task <int> streamTask3 = cd.Pop(recvBuf, stream1 + stream2, recvBuf.Length - (stream1 + stream2)); int stream3 = streamTask3.GetAwaiter().GetResult(); Assert.AreEqual(10, stream3 + stream2 + stream1); Assert.AreEqual((byte)0, recvBuf[0]); Assert.AreEqual((byte)1, recvBuf[1]); Assert.AreEqual((byte)2, recvBuf[2]); Assert.AreEqual((byte)3, recvBuf[3]); Assert.AreEqual((byte)4, recvBuf[4]); Assert.AreEqual((byte)5, recvBuf[5]); Assert.AreEqual((byte)6, recvBuf[6]); Assert.AreEqual((byte)7, recvBuf[7]); Assert.AreEqual((byte)8, recvBuf[8]); Assert.AreEqual((byte)9, recvBuf[9]); }
private void _Run() { var apr = new Regulus.Utility.AutoPowerRegulator(new Utility.PowerRegulator()); while (!_Stop) { apr.Operate(); TPackage[] pkgs; while (_SendPkgs.TryDequeue(out pkgs)) { var buffer = _CreateBuffer(pkgs); var resultTask = _Peer.Send(buffer, 0, buffer.Length); var sendSize = resultTask.GetAwaiter().GetResult(); NetworkMonitor.Instance.Write.Set(sendSize); } } }