/// <summary>do io loop with tcpclient /// </summary> /// <param name="log"></param> /// <param name="server"></param> /// <param name="tcpClient"></param> public static void IOWorker(ILog log, TcpServerChannel server, TcpClient tcpClient) { //remoting core protocol //Preamble 4 //MajorVersion 1 //MinorVersion 1 //ReadOperation 2 //ReadContentDelimiter 2 //ReadContentLength 4 var headerLength = 4 + 1 + 1 + 2 + 2 + 4; var header = new byte[headerLength]; NetworkStream stream = tcpClient.GetStream(); stream.BeginRead(header, 0, header.Length, o => { RemotingTcpProtocolHandle handle = null; try { var length = stream.EndRead(o); handle = length == headerLength ? Parse(header, stream) : null; } catch (Exception e) { handle = null; log.Error(e); } finally { Process(handle, log, server, tcpClient); } }, null); }
private static void Process(RemotingTcpProtocolHandle handle, ILog log, TcpServerChannel server, TcpClient tcpClient) { try { if (handle == null) return; //TODO:process methodCall MethodCall methodCall = Serializer.DeserializeMethodCall(handle.ReadContent()); } catch (Exception e) { log.Error(e); //TODO:return error to client } finally { IOWorker(log, server, tcpClient); } }