private OTcpServerMessage ReceiveJSON(ref TcpClient client) { // Once Header Received + Receive Data var receivedHeader = ReceiveHeader(ref client); var convertedType = StringTypeToTypeConverter.GetTypeFrom(receivedHeader.ExpectType); byte[] databuffer = new byte[receivedHeader.DataSize]; try { client.GetStream().Read(databuffer, 0, databuffer.Length); object dataobject = Convert.ChangeType(JsonConvert.DeserializeObject(Encoding.UTF8.GetString(databuffer), convertedType), convertedType); return(new OTcpServerMessage(receivedHeader, dataobject)); } catch (Exception e) { Console.WriteLine(e.Message); throw e; } }
private OTcpServerMessage ReceiveJSON(ref TcpClient client) { var header = ReceiveHeader(ref client); var dtype = StringTypeToTypeConverter.Convert(header.ExpectType); 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); var convertedType = StringTypeToTypeConverter.GetTypeFrom(header.ExpectType); receivedMessage.Message = Convert.ChangeType(JsonConvert.DeserializeObject(Encoding.UTF8.GetString(messageBytes), convertedType), convertedType); return(receivedMessage); }
//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; } }