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(); }
public void AsynchronousSendMessageSyncReceiveMessage() { byte[] data = new byte[36]; Random rndGen = new Random(); rndGen.NextBytes(data); byte[] length = new byte[4]; Formatting.SizeToBytes(data.Length, length, 0); Message input = Formatting.BytesToMessage(data); ManualResetEvent evt = new ManualResetEvent(false); Uri serverUri = new Uri(SizedTcpTransportBindingElement.SizedTcpScheme + "://" + Environment.MachineName + ":" + Port); object channel = ReflectionHelper.CreateInstance( typeof(SizedTcpTransportBindingElement), "JsonRpcOverTcp.Channels.SizedTcpRequestChannel", new ByteStreamMessageEncodingBindingElement().CreateMessageEncoderFactory().Encoder, BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue), Mocks.GetChannelManagerBase(), new EndpointAddress(serverUri), serverUri); ReflectionHelper.CallMethod(channel, "Open"); object state = new object(); bool success = true; ReflectionHelper.CallMethod(channel, "BeginSendMessage", input, TimeSpan.FromMinutes(1), new AsyncCallback(delegate(IAsyncResult asyncResult) { try { if (!Object.ReferenceEquals(asyncResult.AsyncState, state)) { success = false; Console.WriteLine("Error, state not preserved"); } else { ReflectionHelper.CallMethod(channel, "EndSendMessage", asyncResult); Message output = (Message)ReflectionHelper.CallMethod(channel, "ReceiveMessage", TimeSpan.FromMinutes(1)); 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>()); byte[] outputBytes = Formatting.MessageToBytes(output); Assert.Equal(data, outputBytes, new ArrayComparer <byte>()); } catch (Exception e) { Console.WriteLine("Error: " + e); success = false; } } } finally { evt.Set(); } }), state); evt.WaitOne(); Assert.True(success, "Error in callback"); ReflectionHelper.CallMethod(channel, "Close"); }