Example #1
0
        private void ProcessEvent(BasicDeliverEventArgs ea)
        {
            _qps.Add("r");
            var header     = new HeadersAnalysis(ea.Body);
            var clienttime = header.ClientDate;
            var span       = Math.Abs((DateTime.Now - clienttime).TotalMilliseconds);

            if (Configs.Delay != 0 && span > Configs.Delay * 1000)
            {
                ReplayTo(header.Url, DateTime.Now, ea, 402, $"请求已超时 请求 {ea.RoutingKey} 耗时 {span.ToString()}ms", ContentType.Text);
                return;
            }
            var action = _routing.Route(header.Url.AbsolutePath);

            if (action == null)
            {
                ReplayTo(header.Url, DateTime.Now, ea, 401, "未找到匹配请求的控制器或方法", ContentType.Text);
                return;
            }
            var reques = new Request()
            {
                Headers       = header.Headers,
                Action        = action,
                Url           = header.Url,
                Seralize      = RPCWrite.CreateSeralize(header.ContentType, header.Encode),
                ContentLength = header.ContentLength,
                Body          = header.GetBodys()
            };

            Excute(reques, ea);
        }
Example #2
0
 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());
     }
 }
Example #3
0
        public uint GetObjSize(ContentType type, object data, Type dataType)
        {
            if (data == null)
            {
                return(0);
            }
            var ser = RPCWrite.CreateSeralize(type, Encoding.UTF8);

            return(ser.GetSize(data, dataType));
        }
Example #4
0
        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
            });