public void SendRequest(Action <long, RpcClientContext, bool> callback, int timeout) { RpcRequest request = _trans.Request; Stopwatch watch = new Stopwatch(); _callback = callback; _observer = RpcObserverManager.GetClientItem(_trans.ServerUri.ToString(), request.Service, request.Method, _trans.ServiceRole); watch.Start(); try { _trans.SendRequest( delegate { var response = _trans.Response; long elapseTicks = watch.ElapsedTicks; bool successed = response.ErrorCode == RpcErrorCode.OK; _observer.Track(successed, response.Error, elapseTicks); _callback(elapseTicks, this, successed); }, timeout ); TracingManager.Info( delegate() { _observer.RequestTracer.InfoFmt2( request.ServiceAtComputer, request.ContextUri, "Args = {0}", ObjectHelper.DumpObject(request.BodyValue) ); } ); } catch (Exception ex) { _observer.RequestTracer.ErrorFmt2( ex, request.FromService, request.ContextUri.ToString(), "Args = {0}", ObjectHelper.DumpObject(request.BodyValue) ); var response = RpcResponse.Create(RpcErrorCode.SendFailed, ex); long elapseTicks = watch.ElapsedTicks; _observer.Track(false, response.Error, elapseTicks); _callback(elapseTicks, this, false); } }
/// <summary> /// 返回错误到客户端 /// </summary> /// <param name="errCode">错误码</param> /// <param name="ex">异常</param> public void ReturnError(RpcErrorCode errCode, Exception ex) { RpcResponse response = RpcResponse.Create(errCode, ex); SendResponse(response); }
/// <summary> /// 带参数成功返回 /// </summary> /// <typeparam name="T">参数类型</typeparam> /// <param name="results">返回参数</param> /// <remarks>返回null等同于无参数返回</remarks> public void Return <T>(T results) { RpcResponse response = RpcResponse.Create <T>(results); SendResponse(response); }
/// <summary> /// 抛出异常到客户端 /// </summary> /// <param name="ex">异常</param> /// <remarks>等同于在方法中直接抛出异常,但可以在方法外异步抛出</remarks> public void ThrowException(Exception ex) { RpcResponse response = RpcResponse.Create(RpcErrorCode.ServerError, ex); SendResponse(response); }
/// <summary> /// 无参数成功返回 /// </summary> public void Return() { RpcResponse response = RpcResponse.Create(RpcErrorCode.OK); SendResponse(response); }