Beispiel #1
0
        private static byte[] ConvertToBytes(ProtocolMessage request)
        {
            var asJson = JsonTableConverter.ObjectToJson(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);
        }
		private static byte[] ConvertToBytes(ProtocolMessage request)
		{
			var asJson = JsonTableConverter.ObjectToJson(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}", JsonTableConverter.ObjectToJson(message)));
			}
			if (TRACE && message.type == "event")
			{
				Event e = (Event)message;
				Console.Error.WriteLine(string.Format("E {0}: {1}", e.@event, JsonTableConverter.ObjectToJson(e.body)));
			}

			var data = ConvertToBytes(message);
			try
			{
				_outputStream.Write(data, 0, data.Length);
				_outputStream.Flush();
			}
			catch (Exception)
			{
				// ignore
			}
		}