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); } }
private static void TimeoutCallback(object state, bool timedOut) { RpcHttpClientTransaction trans = null; try { trans = (RpcHttpClientTransaction)state; if (trans._registeredHandle != null) { trans._registeredHandle.Unregister(null); } if (timedOut) // Timeout { var response = RpcResponse.Create(RpcErrorCode.TransactionTimeout, null); trans.OnCallback(response); } } catch (Exception ex) { SystemLog.Error(LogEventID.RpcFailed, ex, "TimeoutCallback"); } }
private static void ResponseCallback(IAsyncResult asyncResult) { RpcHttpClientTransaction trans = (RpcHttpClientTransaction)asyncResult.AsyncState; RpcResponse response = null; try { var webResponse = trans._webRequest.EndGetResponse(asyncResult); trans._webResponse = webResponse; string warn = webResponse.Headers.Get("Warning"); if (!string.IsNullOrEmpty(warn)) { RpcErrorCode errCode = (RpcErrorCode)Enum.Parse(typeof(RpcErrorCode), warn); if (errCode != RpcErrorCode.OK) { Exception ex = null; if (webResponse.ContentLength > 0) { Stream stream = webResponse.GetResponseStream(); ex = BinarySerializer.Deserialize <Exception>(stream); } response = RpcResponse.Create(errCode, ex); } else { SystemLog.Error(LogEventID.RpcFailed, "Unexcepted Message"); response = RpcResponse.Create(RpcErrorCode.Unknown, null); } } else { bool hasBody = (webResponse.Headers["Null"] != "true"); if (hasBody) { if (webResponse.ContentLength == 0) { response = RpcResponse.Create(RpcNull.EmptyStream, 0); } else { Stream stream = webResponse.GetResponseStream(); response = RpcResponse.Create(stream, (int)webResponse.ContentLength); } } else { response = RpcResponse.Create(); } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.Timeout) { response = RpcResponse.Create(RpcErrorCode.TransactionTimeout, ex); } else { response = RpcResponse.Create(RpcErrorCode.SendFailed, ex); } } catch (Exception ex) { response = RpcResponse.Create(RpcErrorCode.SendFailed, ex); } trans.OnCallback(response); }
public void Callback(RpcResponse response) { Response = response; _callback(); }
public void SendFailed(RpcErrorCode code, Exception ex) { Response = RpcResponse.Create(code, ex); RpcTcpTransactionManager.EndTransaction(Sequence, Response); }
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 abstract void SendResponse(RpcResponse response);
public override void SendResponse(RpcResponse response) { _peerTx.Response = response; _peerTx.Callback(); }