Accessing the response object?

Feb 12, 2012 at 4:21 AM
Edited Feb 12, 2012 at 4:28 AM

I started with CustomProtocol QuickStart.  Added a command.  I am looking to do an implementation to support Json-Rpc.  Can I use the SendResponse to send back the result object?  At this point, I don't know how to access the responseObject on the client side?  How is this accomplished?

Or should I look to establish a 2nd channel, one to process calls, and the other channel to process results. The Rpc-spec does offer a mechanism to tie up the messages again.  I also have a question on why the reader is needed on the client side, in order to force it to go (it's like it forces the flush, even though I have a flush).  

 

	[TestFixture]
	public class Core3ProtocolServerTests
	{
		private Core3ProtocolServer m_Server;
		private IServerConfig m_Config;

		[TestFixtureSetUp]
		public void Setup()
		{
			m_Config = new ServerConfig
			{
				Port = 911,
				Ip = "Any",
				MaxConnectionNumber = 1000,
				Mode = SocketMode.Async,
				Name = "Core3ProtocolServer"
				
			};

			m_Server = new Core3ProtocolServer();
			m_Server.Setup(new RootConfig { LoggingMode = LoggingMode.Console },
				m_Config,
				SocketServerFactory.Instance);
		}

		[SetUp]
		public void StartServer()
		{
			m_Server.Start();
		}

		[TearDown]
		public void StopServer()
		{
			m_Server.Stop();
		}

		[Test]
		public void TestCustomProtocol()
		{
			EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_Config.Port);

			using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
			{
				socket.Connect(serverAddress);

				var socketStream = new NetworkStream(socket);
				var reader = new StreamReader(socketStream, Encoding.ASCII, false);
				var writer = new StreamWriter(socketStream, Encoding.ASCII, 1024);

				RpcCall rpcCall = new RpcCall { id = "1", method = "listMethods" };

				String currentMessage = JsonConvert.ExportToString(rpcCall);
				String msg = String.Format("JRPC {0} {1}", currentMessage.Length.ToString().PadLeft(4, '0'), currentMessage);
				writer.Write(msg);
				writer.Flush();

				// i don't hit break-points in the server until, I do this readline.  I don't understand why I need the reader to
				// get it to send

				// also - it never makes it out of the server to get past 
				//this line, if I put ANYTHING into the send response.
				var line = reader.ReadLine();
			}
		}

Coordinator
Feb 12, 2012 at 4:57 AM

1. Can I use the SendResponse to send back the result object? 

Yes

2. At this point, I don't know how to access the responseObject on the client side?

If you want to get the response by reader.ReadLine(), you should send the response with a "\r\n" as end.

Feb 12, 2012 at 4:38 PM

In my test here, I am on the client side, I send a message by .Write on the StreamWriter and then I flush.  Why is reader.ReadLine necessary to get the message actually going (that's when it hits break points on the server side).  It seems like Writer.Flush would do it and if I wasn't interested in seeing the result and reader wouldn't be necessary at all. ??

Coordinator
Feb 12, 2012 at 5:04 PM
Edited Feb 12, 2012 at 5:05 PM

If you want to get the response by reader.ReadLine(), you should send the response from server with a "\r\n" as end.

Feb 12, 2012 at 6:01 PM

Yes, in the meantime of my last response "that" finally clicked.  Thank you.