private static byte[] ConvertToBytes(ProtocolMessage request) { var asJson = JsonConvert.SerializeObject(request); byte[] jsonBytes = Encoding.GetBytes(asJson); string header = string.Format("Content-Length: {0}{1}", jsonBytes.Length, TWO_CRLF); byte[] headerBytes = Encoding.GetBytes(header); byte[] data = new byte[headerBytes.Length + jsonBytes.Length]; System.Buffer.BlockCopy(headerBytes, 0, data, 0, headerBytes.Length); System.Buffer.BlockCopy(jsonBytes, 0, data, headerBytes.Length, jsonBytes.Length); return(data); }
protected void SendMessage(ProtocolMessage message) { message.seq = _sequenceNumber++; if (TRACE_RESPONSE && message.type == "response") { Console.Error.WriteLine(string.Format(" R: {0}", JsonConvert.SerializeObject(message))); } if (TRACE && message.type == "event") { Event e = (Event)message; Console.Error.WriteLine(string.Format("E {0}: {1}", e.eventType, JsonConvert.SerializeObject(e.body))); } var data = ConvertToBytes(message); try { _outputStream.Write(data, 0, data.Length); _outputStream.Flush(); } catch (Exception) { // ignore } }
private static byte[] ConvertToBytes(ProtocolMessage request) { var asJson = JsonConvert.SerializeObject(request); byte[] jsonBytes = Encoding.GetBytes(asJson); string header = string.Format("Content-Length: {0}{1}", jsonBytes.Length, TWO_CRLF); byte[] headerBytes = Encoding.GetBytes(header); byte[] data = new byte[headerBytes.Length + jsonBytes.Length]; System.Buffer.BlockCopy(headerBytes, 0, data, 0, headerBytes.Length); System.Buffer.BlockCopy(jsonBytes, 0, data, headerBytes.Length, jsonBytes.Length); return data; }