Ejemplo n.º 1
0
        public static ClientActionHanler GetHandler(MethodInfo method)
        {
            ClientActionHanler result;

            if (!mHandlers.TryGetValue(method, out result))
            {
                result            = new ClientActionHanler(method);
                mHandlers[method] = result;
            }
            return(result);
        }
Ejemplo n.º 2
0
        protected override object Invoke(MethodInfo targetMethod, object[] args)
        {
            ClientActionHanler handler = ClientActionFactory.GetHandler((MethodInfo)targetMethod);
            var rinfo = handler.GetRequestInfo(args);

            if (handler.Node == null)
            {
                handler.Node = Cluster[rinfo.Url];
            }
            HttpHost host = handler.Node.GetClient();

            if (host == null)
            {
                throw new HttpClientException(null, null, "no http nodes are available");
            }
            if (!handler.Async)
            {
                var request = rinfo.GetRequest(host);
                var task    = request.Execute();
                int timeout = host.Pool.TimeOut;
                if (!task.Wait(timeout))
                {
                    throw new HttpClientException(request, host.Uri, $"{rinfo.Method} {rinfo.Url} request time out {timeout}!");
                }
                if (task.Result.Exception != null)
                {
                    throw task.Result.Exception;
                }
                return(task.Result.Body);
            }
            else
            {
                var request = rinfo.GetRequest(host);
                var task    = request.Execute();
                if (handler.MethodType == typeof(ValueTask))
                {
                    AnyCompletionSource <object> source = new AnyCompletionSource <object>();
                    source.WaitResponse(task);
                    return(new ValueTask(source.Task));
                }
                else
                {
                    Type gtype = typeof(AnyCompletionSource <>);
                    Type type  = gtype.MakeGenericType(handler.ReturnType);
                    IAnyCompletionSource source = (IAnyCompletionSource)Activator.CreateInstance(type);
                    source.WaitResponse(task);
                    return(source.GetTask());
                }
            }
        }