public void SendError(RpcResponseHeader header)
        {
            _context.RetCode = header.ErrorCode;

            if (header.Error != null)
            {
                _context.HasBody = true;
                RpcPipeStreamHelper.WriteStreamEx(_stream, _context, header.Error);
            }
            else
            {
                _context.HasBody = false;
                RpcPipeStreamHelper.WriteStream <RpcNull>(_stream, _context, null);
            }
            _stream.Disconnect();
            _channel.RecycleServerStream(_stream);
        }
        public void SendResponse <T>(RpcResponseHeader header, T results)
        {
            _context.RetCode = RpcErrorCode.OK;
            _context.HasBody = header.HasBody;

            if (header.HasBody)
            {
                RpcPipeStreamHelper.WriteStream <T>(_stream, _context, results);
                _stream.Disconnect();
                _channel.RecycleServerStream(_stream);
            }
            else
            {
                RpcPipeStreamHelper.WriteStream <RpcNull>(_stream, _context, null);
                _stream.Disconnect();
                _channel.RecycleServerStream(_stream);
            }
        }
示例#3
0
        public override void SendResponse(RpcResponse response)
        {
            try {
                _context.RetCode = response.ErrorCode;
                _context.HasBody = response.BodyBuffer != null;

                if (_context.HasBody)
                {
                    byte[] buffer = response.BodyBuffer.GetByteArray();
                    RpcPipeStreamHelper.WriteStream(_stream, _context, buffer);
                }
                else
                {
                    RpcPipeStreamHelper.WriteStream(_stream, _context, null);
                }
            } finally {
                _stream.Disconnect();
                _channel.RecycleServerStream(_stream);
            }
        }
        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();
                }
            }
        }