示例#1
0
        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();
        }
示例#2
0
        public RpcPipeServerTransaction(RpcPipeServerChannel channel, NamedPipeServerStream stream)
            : base(channel, null, null)
        {
            _stream  = stream;
            _channel = channel;

            _context = RpcPipeStreamHelper.ReadStream(_stream, out _buffer);
            var req = new RpcRequest()
            {
                ServiceAtComputer = _context.From,
                Service           = _context.ServiceName,
                Method            = _context.MethodName,
                ContextUri        = _context.To,
            };

            if (_context.HasBody)
            {
                req.BodyBuffer = new RpcBodyBuffer(_buffer);
            }
            else
            {
                req.BodyBuffer = null;
            }

            SetRequest(req);
        }
示例#3
0
        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 RpcRequestHeader ReceiveRequestHeader()
        {
            _context = RpcPipeStreamHelper.ReadStream(_stream, out _buffer);

            RpcRequestHeader header = new RpcRequestHeader();

            header.ServiceAtComputer = _context.From;
            header.Service           = _context.ServiceName;
            header.Method            = _context.MethodName;
            header.HasBody           = _context.HasBody;
            header.ToUri             = _context.To;
            return(header);
        }
示例#5
0
        public static RpcPipeContext ReadStream(PipeStream stream, out byte[] buffer)
        {
            byte[] headerBuffer = new byte[RpcPipeHeader.Size];
            stream.Read(headerBuffer, 0, RpcPipeHeader.Size);
            RpcPipeHeader header = RpcPipeHeader.FromByteArray(headerBuffer);

            if (header.Mark != RpcPipeHeader.MagicMark)
            {
                // throw new RpcException("RpcPipeHeader Crashed", "", RpcErrorCode.SendFailed, null);
                buffer = null;
                return(null);
            }

            if (header.ContextSize > 1024 || header.BodySize > 64000000)
            {
                // throw new RpcException("RpcPipeHeader Length To Long", "", RpcErrorCode.SendFailed, null);
                buffer = null;
                return(null);
            }

            byte[] contextBuffer = new byte[header.ContextSize];
            stream.Read(contextBuffer, 0, contextBuffer.Length);
            RpcPipeContext context = ProtoBufSerializer.FromByteArray <RpcPipeContext>(contextBuffer);

            if (context.HasBody)
            {
                if (header.BodySize == 0)
                {
                    buffer = EmptyBuffer;
                }
                else
                {
                    buffer = new byte[header.BodySize];
                    stream.Read(buffer, 0, buffer.Length);
                }
            }
            else
            {
                buffer = null;
            }
            return(context);
        }
示例#6
0
        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 override void SendRequest(Action callback, int timeout)
        {
            _callback = callback;

            _context = new RpcPipeContext();

            _context.From        = Request.ServiceAtComputer;
            _context.To          = Request.ContextUri;
            _context.ServiceName = Request.Service;
            _context.MethodName  = Request.Method;
            _context.HasBody     = Request.BodyBuffer != null;

            var pipeUri = (NamedPipeUri)ServerUri;

            _stream = new NamedPipeClientStream(pipeUri.Computer, pipeUri.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

            try {
                timeout = timeout > 0 ? timeout : _channel.Timeout;
                if (timeout > 0)
                {
                    _stream.Connect(timeout);
                }
                else
                {
                    _stream.Connect();
                }

                if (Request.BodyBuffer != null)
                {
                    byte[] buffer = Request.BodyBuffer.GetByteArray();
                    RpcPipeStreamHelper.WriteStream(_stream, _context, buffer);
                }
                else
                {
                    RpcPipeStreamHelper.WriteStream(_stream, _context, null);
                }

                _context = RpcPipeStreamHelper.ReadStream(_stream, out _bodyBuffer);

                if (_context.RetCode == RpcErrorCode.OK)
                {
                    if (_context.HasBody)
                    {
                        Response = RpcResponse.Create(_bodyBuffer);
                    }
                    else
                    {
                        Response = RpcResponse.Create();
                    }
                }
                else
                {
                    if (_bodyBuffer != null)
                    {
                        Exception ex = BinarySerializer.FromByteArray <Exception>(_bodyBuffer);
                        Response = RpcResponse.Create(_context.RetCode, ex);
                    }
                    else
                    {
                        Response = RpcResponse.Create(_context.RetCode, null);
                    }
                }
                _callback();
            } catch (Exception ex) {
                Response = RpcResponse.Create(RpcErrorCode.SendFailed, ex);
                _callback();
            } finally {
                if (_stream != null)
                {
                    _stream.Close();
                }
            }
        }
        public void SendRequest <T>(RpcRequestHeader request, T args, Action <RpcResponseHeader> callback, int timeout)
        {
            _callback = callback;

            _context = new RpcPipeContext();

            _context.From        = request.ServiceAtComputer;
            _context.To          = request.ToUri;
            _context.ServiceName = request.Service;
            _context.MethodName  = request.Method;
            _context.HasBody     = request.HasBody;

            _stream = new NamedPipeClientStream(_serverUri.Computer, _serverUri.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

            try {
                timeout = timeout > 0 ? timeout : _channel.Timeout;
                if (timeout > 0)
                {
                    _stream.Connect(timeout);
                }
                else
                {
                    _stream.Connect();
                }

                if (request.HasBody)
                {
                    RpcPipeStreamHelper.WriteStream <T>(_stream, _context, args);
                }
                else
                {
                    RpcPipeStreamHelper.WriteStream <RpcNull>(_stream, _context, null);
                }

                RpcResponseHeader header;
                _context = RpcPipeStreamHelper.ReadStream(_stream, out _bodyBuffer);

                if (_context.RetCode == RpcErrorCode.OK)
                {
                    header = RpcResponseHeader.CreateSuccess(_context.HasBody);
                }
                else
                {
                    if (_bodyBuffer != null)
                    {
                        Exception ex = BinarySerializer.FromByteArray <Exception>(_bodyBuffer);
                        header = RpcResponseHeader.CreateError(_context.RetCode, ex);
                    }
                    else
                    {
                        header = RpcResponseHeader.CreateError(_context.RetCode, null);
                    }
                }
                _callback(header);
            } catch (Exception ex) {
                var header = RpcResponseHeader.CreateError(RpcErrorCode.SendFailed, ex);
                _callback(header);
            } finally {
                if (_stream != null)
                {
                    _stream.Close();
                }
            }
        }