private OTcpServerHeader SendHeader(ref TcpClient client, int messageSize, string command, string expectType, Guid token) { var header = new OTcpServerHeader(Commands.GetHashOfCommand(command), messageSize, expectType, token); var headerBytes = ObjectBinarySerialization.ObjectToByteArray(header); client.GetStream().Write(headerBytes, 0, headerBytes.Length); return(header); }
private void SendBinary <T>(string command, T messageData) { var data = ObjectBinarySerialization.ObjectToByteArray(messageData); var header = new OTcpServerHeader(Commands.GetHashOfCommand(command), data.LongLength, (messageData).GetType().FullName); int headerSize = OTcpServerHeader.HeaderSize; _currentStream.Write(ObjectBinarySerialization.ObjectToByteArray(header), 0, headerSize); _currentStream.Write(data, 0, data.Length); // Write the bytes }
private void SendJSON <T>(string command, T messageData) { try { var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageData)); var header = new OTcpServerHeader(Commands.GetHashOfCommand(command), data.LongLength, (messageData).GetType().FullName); int headerSize = OTcpServerHeader.HeaderSize; _currentStream.Write(ObjectBinarySerialization.ObjectToByteArray(header), 0, headerSize); _currentStream.Write(data, 0, data.Length); // Write the bytes } catch (Exception e) { Console.WriteLine(e.Message); throw e; } }
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; } }