private OTcpServerMessage ReceiveBinary(ref TcpClient client) { var header = ReceiveHeader(ref client); var receivedMessage = new OTcpServerMessage(); if (header == null) { // LOG return(null); } receivedMessage.Header = header; byte[] messageBytes = new byte[header.DataSize]; // Clear the message // Receive the stream of bytes client.GetStream().Read(messageBytes, 0, messageBytes.Length); receivedMessage.Message = ObjectBinarySerialization.ByteArrayToObject(messageBytes); return(receivedMessage); }
private OTcpServerHeader ReceiveHeader(ref TcpClient client) { int headerSize = OTcpServerHeader.HeaderSize; byte[] headerBytes = new byte[headerSize]; // Clear the message // Receive the stream of bytes try { _currentStream.Read(headerBytes, 0, headerSize); OTcpServerHeader header = (OTcpServerHeader)ObjectBinarySerialization.ByteArrayToObject(headerBytes); header.ExpectType = header.ExpectType.Split(' ')[0]; return(header); } catch (Exception e) { Console.WriteLine(e.Message); return(null); } }
private OTcpServerHeader ReceiveHeader(ref TcpClient client) { // Get Header var headerByteSize = OTcpServerHeader.HeaderSize; byte[] headerbuffer = new byte[headerByteSize]; //client.Client.RemoteEndPoint try { client.GetStream().Read(headerbuffer, 0, headerbuffer.Length); OTcpServerHeader receivedHeader = (OTcpServerHeader)ObjectBinarySerialization.ByteArrayToObject(headerbuffer); receivedHeader.ExpectType = receivedHeader.ExpectType.Split(' ')[0]; return(receivedHeader); } catch (Exception e) { Console.WriteLine("Header Receive Error" + e.Message); throw e; } }
//public void AddCommand(string commandName, FunctionHandler.ObjInObjOutHandlerDelegate responseFunction) //{ // if (commandName != null && responseFunction != null) // { // var fh = new FunctionHandler(commandCount,commandName,FunctionHandlerType.ObjInObjOut,responseFunction); // CommandList.Add(commandCount, fh); // commandCount++; // } //} //public void AddCommand(string commandName, FunctionHandler.ObjInVoidOutHandlerDelegate responseFunction) //{ // if (commandName != null && responseFunction != null) // { // var fh = new FunctionHandler(commandCount, commandName, FunctionHandlerType.ObjInVoidOut, responseFunction); // CommandList.Add(commandCount, fh); // commandCount++; // } //} //public void AddCommand(string commandName, FunctionHandler.VoidInObjOutHandlerDelegate responseFunction) //{ // if (commandName != null && responseFunction != null) // { // var fh = new FunctionHandler(commandCount, commandName, FunctionHandlerType.VoidInObjOut, responseFunction); // CommandList.Add(commandCount, fh); // commandCount++; // } //} //public void AddCommand(string commandName, FunctionHandler.VoidInVoidOutHandlerDelegate responseFunction) //{ // if (commandName != null && responseFunction != null) // { // var fh = new FunctionHandler(commandCount, commandName, FunctionHandlerType.VoidInVoidOut, responseFunction); // CommandList.Add(commandCount, fh); // commandCount++; // } //} private OTcpServerMessage ReceiveBinary(ref TcpClient client) { // Get Header var receivedHeader = ReceiveHeader(ref client); var convertedType = StringTypeToTypeConverter.GetTypeFrom(receivedHeader.ExpectType); // Once Header Received + Receive Data byte[] databuffer = new byte[receivedHeader.DataSize]; try { client.GetStream().Read(databuffer, 0, databuffer.Length); object dataobject = Convert.ChangeType(ObjectBinarySerialization.ByteArrayToObject(databuffer), convertedType); return(new OTcpServerMessage(receivedHeader, dataobject)); } catch (Exception e) { Console.WriteLine(e.Message); throw e; } }