public Message Process(Message input) { Console.WriteLine("[service] input = {0}", input); byte[] bytes = Formatting.MessageToBytes(input); Debugging.PrintBytes(bytes); return(Formatting.BytesToMessage(bytes)); }
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"); }
static void Main(string[] args) { int port = 8000; SocketsServer server = new SocketsServer(port, new CalculatorService()); server.StartServing(); Console.WriteLine("Started the simple server"); CustomBinding binding = new CustomBinding(new SizedTcpTransportBindingElement()); EndpointAddress address = new EndpointAddress( SizedTcpTransportBindingElement.SizedTcpScheme + "://localhost:" + port); ChannelFactory <IUntypedTest> factory = new ChannelFactory <IUntypedTest>(binding, address); IUntypedTest proxy = factory.CreateChannel(); string[] allInputs = new string[] { "{\"method\":\"Add\",\"params\":[5, 8],\"id\":1}", "{\"method\":\"Multiply\",\"params\":[5, 8],\"id\":2}", "{\"method\":\"Divide\",\"params\":[5, 0],\"id\":3}", }; foreach (string input in allInputs) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); Console.WriteLine("Input: {0}", input); Message inputMessage = Formatting.BytesToMessage(inputBytes); Message outputMessage = proxy.Process(inputMessage); Console.WriteLine("Received output: {0}", outputMessage); byte[] outputBytes = Formatting.MessageToBytes(outputMessage); Console.WriteLine("Output bytes:"); Debugging.PrintBytes(outputBytes); } ((IClientChannel)proxy).Close(); factory.Close(); Console.WriteLine(); Console.WriteLine("Now using the typed interface"); ChannelFactory <ITypedTest> typedFactory = new ChannelFactory <ITypedTest>(binding, address); typedFactory.Endpoint.Behaviors.Add(new JsonRpcEndpointBehavior()); ITypedTest typedProxy = typedFactory.CreateChannel(); Console.WriteLine("Calling Add"); int result = typedProxy.Add(5, 8); Console.WriteLine(" ==> Result: {0}", result); Console.WriteLine(); Console.WriteLine("Calling Multiply"); result = typedProxy.Multiply(5, 8); Console.WriteLine(" ==> Result: {0}", result); Console.WriteLine(); Console.WriteLine("Calling Divide (throws)"); try { result = typedProxy.Divide(5, 0); Console.WriteLine(" ==> Result: {0}", result); } catch (JsonRpcException e) { Console.WriteLine("Error: {0}", e.JsonException); } Console.WriteLine(); Console.WriteLine("Now using the typed asynchronous interface"); var asyncTypedFactory = new ChannelFactory <ITypedTestAsync>(binding, address); asyncTypedFactory.Endpoint.Behaviors.Add(new JsonRpcEndpointBehavior()); ITypedTestAsync asyncTypedProxy = asyncTypedFactory.CreateChannel(); AutoResetEvent evt = new AutoResetEvent(false); Console.WriteLine("Calling BeginAdd"); asyncTypedProxy.BeginAdd(5, 8, delegate(IAsyncResult ar) { result = asyncTypedProxy.EndAdd(ar); Console.WriteLine(" ==> Result: {0}", result); Console.WriteLine(); evt.Set(); }, null); evt.WaitOne(); Console.WriteLine("Calling BeginMultiply"); asyncTypedProxy.BeginMultiply(5, 8, delegate(IAsyncResult ar) { result = asyncTypedProxy.EndMultiply(ar); Console.WriteLine(" ==> Result: {0}", result); Console.WriteLine(); evt.Set(); }, null); evt.WaitOne(); Console.WriteLine("Calling BeginDivide (throws)"); asyncTypedProxy.BeginDivide(5, 0, delegate(IAsyncResult ar) { try { result = asyncTypedProxy.EndDivide(ar); Console.WriteLine(" ==> Result: {0}", result); } catch (JsonRpcException e) { Console.WriteLine("Error: {0}", e.JsonException); } Console.WriteLine(); evt.Set(); }, null); evt.WaitOne(); }
public void AsynchronousRequest() { byte[] data = new byte[36]; Random rndGen = new Random(); rndGen.NextBytes(data); 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); ChannelBase channelBase = (ChannelBase)channel; IRequestChannel requestChannel = (IRequestChannel)channel; channelBase.Open(); object state = new object(); bool success = true; requestChannel.BeginRequest(input, new AsyncCallback(delegate(IAsyncResult asyncResult) { try { if (!Object.ReferenceEquals(asyncResult.AsyncState, state)) { success = false; Console.WriteLine("Error, state not preserved"); } else { Message output = requestChannel.EndRequest(asyncResult); try { 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"); channelBase.Close(); }