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 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(); } } }