public static void WriteStream <T>(PipeStream stream, RpcPipeContext context, T data) { byte[] contextBuffer = ProtoBufSerializer.ToByteArray <RpcPipeContext>(context); byte[] bodyBuffer = null; if (typeof(T) != typeof(RpcNull)) { bodyBuffer = ProtoBufSerializer.ToByteArray <T>(data); } else { bodyBuffer = EmptyBuffer; } RpcPipeHeader header; header.Mark = RpcPipeHeader.MagicMark; header.ContextSize = contextBuffer.Length; header.BodySize = bodyBuffer.Length; byte[] headerBuffer = RpcPipeHeader.ToByteArray(header); stream.Write(headerBuffer, 0, headerBuffer.Length); stream.Write(contextBuffer, 0, contextBuffer.Length); if (header.BodySize > 0) { stream.Write(bodyBuffer, 0, bodyBuffer.Length); } stream.Flush(); }
public static void WriteStreamEx(PipeStream stream, RpcPipeContext context, Exception ex) { byte[] contextBuffer = ProtoBufSerializer.ToByteArray <RpcPipeContext>(context); byte[] bodyBuffer = null; if (ex != null) { bodyBuffer = BinarySerializer.ToByteArray(ex); } else { bodyBuffer = EmptyBuffer; } RpcPipeHeader header; header.Mark = RpcPipeHeader.MagicMark; header.ContextSize = contextBuffer.Length; header.BodySize = bodyBuffer.Length; byte[] headerBuffer = RpcPipeHeader.ToByteArray(header); stream.Write(headerBuffer, 0, headerBuffer.Length); stream.Write(contextBuffer, 0, contextBuffer.Length); if (header.BodySize > 0) { stream.Write(bodyBuffer, 0, bodyBuffer.Length); } stream.Flush(); }
public void SendResponse <T>(RpcResponseHeader header, T results) { HttpListenerResponse response = _httpContext.Response; response.StatusCode = 200; response.ContentType = "multipart/byteranges"; if (!header.HasBody) { response.Headers.Add("Null", "true"); response.ContentLength64 = 0; } else { byte[] buffer = ProtoBufSerializer.ToByteArray <T>(results); if (buffer.Length > 0) { response.ContentLength64 = buffer.Length; response.OutputStream.Write(buffer, 0, buffer.Length); response.OutputStream.Close(); } else { response.ContentLength64 = 0; } } response.Close(); }
void IRpcClientTransaction.SendRequest <T>(RpcRequestHeader request, T args, Action <RpcResponseHeader> callback, int timeout) { _callback = callback; _serviceUrl = string.Format("{0}/{1}.{2}", request.ServerUri, request.Service, request.Method); _webRequest = HttpWebRequest.Create(_serviceUrl); _webRequest.Method = "POST"; _webRequest.Proxy = null; _webRequest.ContentType = "multipart/byteranges"; _webRequest.Headers.Add(HttpRequestHeader.From, request.ServiceAtComputer); _webRequest.Headers.Add(HttpRequestHeader.Pragma, _serviceUrl); _webRequest.Headers.Add(HttpRequestHeader.Cookie, "to=" + ObjectHelper.ToString(request.ToUri)); byte[] buffer = null; if (!request.HasBody) { _webRequest.Headers.Add("Null", "true"); _webRequest.ContentLength = 0; } else { buffer = ProtoBufSerializer.ToByteArray <T>(args); _webRequest.ContentLength = buffer.Length; } timeout = timeout > 0 ? timeout : _channel.Timeout; if (timeout > 0) { _waitHandle = new ManualResetEvent(false); ThreadPool.RegisterWaitForSingleObject(_waitHandle, new WaitOrTimerCallback(TimeoutCallback), this, timeout, true); } if (_webRequest.ContentLength == 0) { _webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), this); } else { _webRequest.BeginGetRequestStream( delegate(IAsyncResult asyncResult) { try { Stream stream = _webRequest.EndGetRequestStream(asyncResult); stream.Write(buffer, 0, buffer.Length); stream.Close(); _webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), this); } catch (Exception ex) { var resp = RpcResponseHeader.CreateError(RpcErrorCode.SendFailed, ex); _callback(resp); } }, this ); } }
public void ProtoBuffer_TestSimple2() { byte[] buffer1 = ProtoBufSerializer.ToByteArray <int>(123); byte[] buffer2 = ProtoBufSerializer.ToByteArray <HybridDictionary <int, int> >(new HybridDictionary <int, int>()); HybridDictionary <int, string> s = new HybridDictionary <int, string>(); s.Add(1, "!23123"); byte[] buffer3 = ProtoBufSerializer.ToByteArray <HybridDictionary <int, string> >(s); var ss = ProtoBufSerializer.FromByteArray <HybridDictionary <int, string> >(buffer3); Assert.AreEqual(ss.Count, 1); Assert.AreEqual(ss.Keys.Count, 1); Assert.AreEqual(ss.Values.Count, 1); }
public static void WriteStream(PipeStream stream, RpcPipeContext context, byte[] bodyBuf) { byte[] contextBuffer = ProtoBufSerializer.ToByteArray <RpcPipeContext>(context); RpcPipeHeader header; header.Mark = RpcPipeHeader.MagicMark; header.ContextSize = contextBuffer.Length; header.BodySize = bodyBuf == null ? 0 : bodyBuf.Length; byte[] headerBuffer = RpcPipeHeader.ToByteArray(header); stream.Write(headerBuffer, 0, headerBuffer.Length); stream.Write(contextBuffer, 0, contextBuffer.Length); if (header.BodySize > 0) { stream.Write(bodyBuf, 0, bodyBuf.Length); } stream.Flush(); }
public void SimpleTypeTest() { // // TODO: Add test logic here // ProtoBufSerializer.ToByteArray(false); ProtoBufSerializer.ToByteArray((int)64); ProtoBufSerializer.ToByteArray((long)640000000000L); ProtoBufSerializer.ToByteArray((float)64.00); ProtoBufSerializer.ToByteArray((double)64.00); ProtoBufSerializer.ToByteArray((decimal)64.00); ProtoBufSerializer.ToByteArray((string)"GFW"); ProtoBufSerializer.ToByteArray(TestEnum.A | TestEnum.B); ProtoBufSerializer.ToByteArray(DateTime.Parse("1989-06-04")); ProtoBufSerializer.ToByteArray(TimeSpan.Parse("6:0:0")); ProtoBufSerializer.ToByteArray(new Uri("emailto:[email protected]")); ProtoBufSerializer.ToByteArray(new int[] { 1, 2, 3, 4, 5 }); ProtoBufSerializer.ToByteArray(new byte[] { 1, 2, 3, 4, 5 }); ProtoBufSerializer.ToByteArray(new byte[] { 1, 2, 3, 4, 5 }); ProtoBufSerializer.ToByteArray(Guid.NewGuid()); ProtoBufSerializer.ToByteArray(new List <string>()); ProtoBufSerializer.ToByteArray(new Dictionary <int, string>()); }