Esempio n. 1
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            try
            {
                if (channel == null || !channel.Open)
                {
                    channel = AsyncInvoke.RunSync(() => bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(Host), Port)));
                }
            }
            catch (Exception ero)
            {
                throw new Exception("连接服务端失败:" + ero);
            }
            var id     = Guid.NewGuid().ToString();
            var method = ServiceType.GetMethod(binder.Name);

            allWaits.Add(id);
            var request = new RequestMessage
            {
                MsgId   = id,
                Service = ServiceName,
                Method  = binder.Name,
                Token   = Token
            };
            var dic     = new Dictionary <string, object>();
            var allArgs = method.GetParameters();
            var outs    = new Dictionary <string, object>();

            for (int i = 0; i < allArgs.Length; i++)
            {
                var p = allArgs[i];
                dic.Add(p.Name, args[i]);
                if (p.Attributes == ParameterAttributes.Out)
                {
                    outs.Add(p.Name, i);
                }
            }
            request.Args = dic;
            channel.WriteAndFlushAsync(request.ToBuffer());
            //等待返回
            var response = allWaits.Wait(id).Response;

            if (response == null)
            {
                throw new Exception("请求超时未响应");
            }
            if (!response.Success)
            {
                throw new Exception($"服务端处理错误:{response.Msg}");
            }
            var returnType = method.ReturnType;

            if (response.Outs != null && response.Outs.Count > 0)
            {
                foreach (var kv in response.Outs)
                {
                    var find = outs[kv.Key];
                    args[(int)find] = kv.Value;
                }
            }
            if (returnType == typeof(void))
            {
                result = null;
                return(true);
            }
            result = response.GetData(returnType);
            return(true);
        }
Esempio n. 2
0
 /// <summary>
 /// POST内容,并返回跳转后的URL
 /// </summary>
 /// <param name="url"></param>
 /// <param name="data"></param>
 /// <param name="now_url"></param>
 /// <returns></returns>
 public string SendData(string url, string method, string data)
 {
     return(AsyncInvoke.RunSync(() => SendDataAsync(url, method, data)));
 }