Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        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
                    );
            }
        }
Exemplo n.º 3
0
        private static void ResponseCallback(IAsyncResult asyncResult)
        {
            RpcHttpClientTransaction trans  = (RpcHttpClientTransaction)asyncResult.AsyncState;
            RpcResponseHeader        header = null;

            try {
                var response = trans._webRequest.EndGetResponse(asyncResult);
                trans._webResponse = response;

                string warn = response.Headers.Get("Warning");

                if (!string.IsNullOrEmpty(warn))
                {
                    RpcErrorCode errCode = (RpcErrorCode)Enum.Parse(typeof(RpcErrorCode), warn);
                    if (errCode != RpcErrorCode.OK)
                    {
                        Exception ex = null;
                        if (response.ContentLength > 0)
                        {
                            Stream stream = response.GetResponseStream();
                            ex = BinarySerializer.Deserialize <Exception>(stream);
                        }
                        header = RpcResponseHeader.CreateError(errCode, ex);
                    }
                    else
                    {
                        SystemLog.Error(LogEventID.RpcFailed, "Unexcepted Message");
                        header = RpcResponseHeader.CreateError(RpcErrorCode.Unknown, null);
                    }
                }
                else
                {
                    bool hasBody = (response.Headers["Null"] != "true");
                    header = RpcResponseHeader.CreateSuccess(hasBody);
                }
            } catch (WebException ex) {
                if (ex.Status == WebExceptionStatus.Timeout)
                {
                    header = RpcResponseHeader.CreateError(RpcErrorCode.TransactionTimeout, ex);
                }
                else
                {
                    header = RpcResponseHeader.CreateError(RpcErrorCode.SendFailed, ex);
                }
            } catch (Exception ex) {
                header = RpcResponseHeader.CreateError(RpcErrorCode.SendFailed, ex);
            }
            trans._callback(header);
            trans._waitHandle.Set();
        }
        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);
            }
        }
Exemplo n.º 6
0
        private static void TimeoutCallback(object state, bool setted)
        {
            RpcHttpClientTransaction trans = null;

            try {
                if (setted)                   // Timeout
                {
                    trans = (RpcHttpClientTransaction)state;
                    var resp = RpcResponseHeader.CreateError(RpcErrorCode.TransactionTimeout, null);
                    trans._callback(resp);
                }
            } catch (Exception ex) {
                SystemLog.Error(LogEventID.RpcFailed, ex, "TimeoutCallback");
            } finally {
                if (trans != null)
                {
                    trans.Dispose();
                }
            }
        }
Exemplo n.º 7
0
        public void SendError(RpcResponseHeader header)
        {
            HttpListenerResponse response = _httpContext.Response;

            response.StatusCode  = 200;
            response.ContentType = "multipart/byteranges";

            response.Headers.Add("Warning", header.ErrorCode.ToString());
            if (header.Error == null)
            {
                response.ContentLength64 = 0;
            }
            else
            {
                byte[] buffer = BinarySerializer.ToByteArray(header.Error);
                response.ContentLength64 = buffer.Length;
                response.OutputStream.Write(buffer, 0, buffer.Length);
                response.OutputStream.Close();
            }
            response.Close();
        }
Exemplo n.º 8
0
 public void SendError(RpcResponseHeader header)
 {
     _response = header;
     _callback(header);
 }
Exemplo n.º 9
0
 public void SendResponse <T>(RpcResponseHeader header, T results)
 {
     _response = header;
     _results  = results;
     _callback(header);
 }
        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();
                }
            }
        }