private void ReplayTo(Uri url, DateTime intime, BasicDeliverEventArgs ea, int state, string msg, ContentType type, object obj = null, Type objType = null) { try { var replyTo = ea.BasicProperties.ReplyTo; var replyTold = ea.BasicProperties.CorrelationId; var replyProps = _pubChannel.CreateBasicProperties(); replyProps.CorrelationId = replyTold; var s_url = url.ToString(); _logger.LogInformation($"response uri:{s_url}, data:{obj.ToJson()} state={state.ToString()} type={type.ToString()}"); if (type != ContentType.Json || (type == ContentType.Json && obj != null)) { var msg_size = Encoding.UTF8.GetByteCount(msg); var datas = new byte[55 + msg_size + GetObjSize(type, obj, objType)]; var write = new RPCWrite(datas, Encoding.UTF8); write.WriteString("encoding:utf-8"); write.WriteString("state:" + state.ToString()); write.WriteString("msg:" + msg); write.WriteString("content-type:" + (type == ContentType.YDData ? "yddata" : (type == ContentType.Json ? "json" : "text"))); write.WriteContent(type, obj, objType); _pubChannel.BasicPublish("", routingKey: replyTo, basicProperties: replyProps, body: write.GetDatas().ToArray()); var now = DateTime.Now; var ms = (now - intime).TotalMilliseconds; _qps.Set("c", (int)ms); } else { throw new NotImplementedException(); } } catch (Exception ex) { _logger.LogError("Rpc ReplayTo Error:" + ex.ToString()); } }
public async Task <ResponseBase <TOut> > CallAsync <Tin, TOut>(string uri, Tin data, ContentType type) { var datas = new byte[2000]; var write = new RPCWrite(datas, Encode); var encoding = "encoding:" + Encode.WebName; write.WriteString(encoding); var url = "url:" + "rpc://" + ServerId + uri; write.WriteString(url); var now = "clientTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); write.WriteString(now); var contenttype = "content-type:" + GetContentTypeName(type); write.WriteString(contenttype); write.WriteContent(type, data, typeof(Tin)); var t1 = mqRpc.Request(ServerId, write.GetDatas(), out _); var t2 = Task.Delay(TimeOut * 1000); await Task.WhenAny(new Task[] { t1, t2 }); if (!t1.IsCompletedSuccessfully) { return new ResponseBase <TOut>() { ServerState = -1, ServerMsg = "请求超时" } } ; var rsp = t1.Result; var analysis = new HeadersAnalysis(rsp); var seralize = CreateSeralize(analysis.ContentType, analysis.Encode); var res = seralize.DeserializeObject(analysis.GetBodys(), typeof(TOut)); var state = int.Parse(analysis.Headers["state"]); var msg = analysis.Headers["msg"]; if (state == 200) { return new ResponseBase <TOut> { ServerState = state, ServerMsg = msg, Data = (TOut)res } } ; return(new ResponseBase <TOut> { ServerState = state, ServerMsg = msg, Data = default });