Exemplo n.º 1
0
        public Task <Response> Execute()
        {
            AnyCompletionSource <Response> taskCompletionSource = new AnyCompletionSource <Response>();

            OnExecute(taskCompletionSource);
            return(taskCompletionSource.Task);
        }
Exemplo 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());
                }
            }
        }
Exemplo n.º 3
0
        private async void OnExecute(AnyCompletionSource <Response> taskCompletionSource)
        {
            HttpClient client = null;

            try
            {
                client = HttpHost.Pool.Pop();
                client.RequestCommpletionSource = taskCompletionSource;
                Client = client.Client;
                if (client.Client is AsyncTcpClient)
                {
                    AsyncTcpClient asyncClient = (AsyncTcpClient)client.Client;
                    var            a           = asyncClient.ReceiveMessage <Response>();
                    if (!a.IsCompleted)
                    {
                        asyncClient.Send(this);
                        Status = RequestStatus.SendCompleted;
                    }
                    Response = await a;
                    if (Response.Exception != null)
                    {
                        Status = RequestStatus.Error;
                        throw Response.Exception;
                    }
                    else
                    {
                        Status = RequestStatus.Received;
                    }
                }
                else
                {
                    TcpClient syncClient = (TcpClient)client.Client;
                    syncClient.SendMessage(this);
                    Status   = RequestStatus.SendCompleted;
                    Response = syncClient.ReceiveMessage <Response>();
                    if (Response.Exception != null)
                    {
                        Status = RequestStatus.Error;
                        throw Response.Exception;
                    }
                    else
                    {
                        Status = RequestStatus.Received;
                    }
                }
                int code = int.Parse(Response.Code);
                if (Response.Length > 0)
                {
                    try
                    {
                        if (code < 400)
                        {
                            Response.Body = this.Formater.Deserialization(Response.Stream, this.BodyType, Response.Length);
                        }
                        else
                        {
                            Response.Body = Response.Stream.ReadString(Response.Length);
                        }
                    }
                    finally
                    {
                        Response.Stream.ReadFree(Response.Length);
                        if (Response.Chunked)
                        {
                            Response.Stream.Dispose();
                        }
                        Response.Stream = null;
                    }
                }
                if (!Response.KeepAlive)
                {
                    client.Client.DisConnect();
                }
                if (code >= 400)
                {
                    throw new System.Net.WebException($"{this.Method} {this.Url} {Response.Code} {Response.CodeMsg} {Response.Body}");
                }
                Status = RequestStatus.Completed;
            }
            catch (Exception e_)
            {
                HttpClientException clientException = new HttpClientException(this, HttpHost.Uri, e_.Message, e_);
                if (e_ is System.Net.Sockets.SocketException || e_ is ObjectDisposedException)
                {
                    clientException.SocketError = true;
                }
                Response = new Response {
                    Exception = clientException
                };
                Status = RequestStatus.Error;
            }
            if (Response.Exception != null)
            {
                HttpHost.AddError(Response.Exception.SocketError);
            }
            else
            {
                HttpHost.AddSuccess();
            }
            Response.Current = Response;
            if (client != null)
            {
                HttpHost.Pool.Push(client);
            }
            await Task.Run(() => taskCompletionSource.TrySetResult(Response));
        }
Exemplo n.º 4
0
        private async void OnExecute(AnyCompletionSource <Response> taskCompletionSource)
        {
            HttpClient client = null;
            Response   response;

            try
            {
                client = HttpHost.Pool.Pop();
                client.RequestCommpletionSource = taskCompletionSource;
                Client = client.Client;
                if (client.Client is AsyncTcpClient)
                {
                    AsyncTcpClient asyncClient = (AsyncTcpClient)client.Client;
                    var            a           = asyncClient.ReceiveMessage();
                    if (!a.IsCompleted)
                    {
                        asyncClient.Send(this);
                        Status = RequestStatus.SendCompleted;
                    }
                    var result = await a;
                    if (result is Exception error)
                    {
                        response           = new Response();
                        response.Exception = new HttpClientException(this, HttpHost.Uri, error.Message, error);
                        Status             = RequestStatus.Error;
                    }
                    else
                    {
                        response = (Response)result;
                        Status   = RequestStatus.Received;
                    }
                }
                else
                {
                    TcpClient syncClient = (TcpClient)client.Client;
                    syncClient.SendMessage(this);
                    Status   = RequestStatus.SendCompleted;
                    response = syncClient.ReceiveMessage <Response>();
                    Status   = RequestStatus.Received;
                }
                if (response.Exception == null)
                {
                    int code = int.Parse(response.Code);
                    if (response.Length > 0)
                    {
                        try
                        {
                            if (code < 400)
                            {
                                response.Body = this.Formater.Deserialization(response.Stream, this.BodyType, response.Length);
                            }
                            else
                            {
                                response.Body = response.Stream.ReadString(response.Length);
                            }
                        }
                        finally
                        {
                            response.Stream.ReadFree(response.Length);
                            if (response.Chunked)
                            {
                                response.Stream.Dispose();
                            }
                            response.Stream = null;
                        }
                    }
                    if (!response.KeepAlive)
                    {
                        client.Client.DisConnect();
                    }
                    if (code >= 400)
                    {
                        response.Exception = new HttpClientException(this, HttpHost.Uri, $" [{this.Method} {this.Url} {response.Code} {response.CodeMsg}] Response text:[{response.Body}]");
                    }
                    Status = RequestStatus.Completed;
                }
            }
            catch (Exception e_)
            {
                HttpClientException clientException = new HttpClientException(this, HttpHost.Uri, e_.Message, e_);
                response = new Response {
                    Exception = clientException
                };
                Status = RequestStatus.Error;
            }
            if (response.Exception != null)
            {
                HttpHost.AddError(response.Exception.SocketError);
            }
            else
            {
                HttpHost.AddSuccess();
            }
            Response.Current = response;
            this.Response    = response;
            if (client != null)
            {
                HttpHost.Pool.Push(client);
            }
            await Task.Run(() => taskCompletionSource.TrySetResult(response));
        }