public void SynchronousSendReceive() { byte[] length = new byte[4]; byte[] data = new byte[36]; Random rndGen = new Random(); rndGen.NextBytes(data); Formatting.SizeToBytes(data.Length, length, 0); byte[] toSend = new byte[length.Length + data.Length]; Array.Copy(length, 0, toSend, 0, length.Length); Array.Copy(data, 0, toSend, length.Length, data.Length); object channel = ReflectionHelper.CreateInstance( typeof(SizedTcpTransportBindingElement), "JsonRpcOverTcp.Channels.SizedTcpBaseChannel", null, BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue), Mocks.GetChannelManagerBase()); Socket socket = Mocks.GetConnectedSocket(Port); ReflectionHelper.SetField(channel, "socket", socket); ReflectionHelper.CallMethod(channel, "SocketSend", toSend); ReflectionHelper.CallMethod(channel, "SocketReceiveBytes", toSend.Length); Assert.Equal(2, this.server.ReceivedBytes.Count); Assert.Equal(length, this.server.ReceivedBytes[0], new ArrayComparer <byte>()); Assert.Equal(data, this.server.ReceivedBytes[1], new ArrayComparer <byte>()); socket.Close(); }
public void AsynchronousSendSynchronousReceive() { byte[] length = new byte[4]; byte[] data = new byte[36]; Random rndGen = new Random(); rndGen.NextBytes(data); Formatting.SizeToBytes(data.Length, length, 0); byte[] toSend = new byte[length.Length + data.Length]; Array.Copy(length, 0, toSend, 0, length.Length); Array.Copy(data, 0, toSend, length.Length, data.Length); ManualResetEvent evt = new ManualResetEvent(false); object channel = ReflectionHelper.CreateInstance( typeof(SizedTcpTransportBindingElement), "JsonRpcOverTcp.Channels.SizedTcpBaseChannel", null, BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue), Mocks.GetChannelManagerBase()); Socket socket = Mocks.GetConnectedSocket(Port); ReflectionHelper.SetField(channel, "socket", socket); object state = new object(); bool success = true; ReflectionHelper.CallMethod(channel, "BeginSocketSend", toSend, new AsyncCallback(delegate(IAsyncResult asyncResult) { try { if (!Object.ReferenceEquals(asyncResult.AsyncState, state)) { success = false; Console.WriteLine("Error, state not preserved"); } else { ReflectionHelper.CallMethod(channel, "EndSocketSend", asyncResult); ReflectionHelper.CallMethod(channel, "SocketReceiveBytes", toSend.Length); try { Assert.Equal(2, this.server.ReceivedBytes.Count); Assert.Equal(length, this.server.ReceivedBytes[0], new ArrayComparer <byte>()); Assert.Equal(data, this.server.ReceivedBytes[1], new ArrayComparer <byte>()); } catch (Exception e) { Console.WriteLine("Error: " + e); success = false; } } } finally { evt.Set(); } }), state); evt.WaitOne(); Assert.True(success, "Error in callback"); socket.Close(); }