public IClientPacket Clone()
 {
     if (pipeStream != null)
     {
         pipeStream.Dispose();
         pipeStream = null;
     }
     HttpClientPacket result = new HttpClientPacket();
     result.Client = this.Client;
     this.Client = null;
     return result;
 }
Beispiel #2
0
        private HttpClientHandler Create()
        {
            var            packet = new HttpClientPacket();
            AsyncTcpClient client;

            if (SSL)
            {
                client = SocketFactory.CreateSslClient <AsyncTcpClient>(packet, Host, Port, Host);
                if (CertificateValidationCallback != null)
                {
                    client.CertificateValidationCallback = CertificateValidationCallback;
                }
                else
                {
                    client.CertificateValidationCallback = (o, e, d, f) => true;
                }
            }
            else
            {
                client = SocketFactory.CreateClient <AsyncTcpClient>(packet, Host, Port);
            }
            //if (this.SslProtocols != null)
            //    client.SslProtocols = this.SslProtocols;
            packet.Client    = client;
            client.Connected = c =>
            {
                c.Socket.NoDelay        = true;
                c.Socket.ReceiveTimeout = TimeOut;
                c.Socket.SendTimeout    = TimeOut;
            };
            bool newconn;

            client.Connect(out newconn);
            var result = new HttpClientHandler();

            result.Using  = false;
            result.Client = client;
            lock (Clients)
                Clients.Add(result);
            return(result);
        }
Beispiel #3
0
        public HttpClient Pop(bool recursion = false)
        {
            HttpClient result;

            if (!mPools.TryDequeue(out result))
            {
                int value = System.Threading.Interlocked.Increment(ref mConnections);
                if (value > MaxConnections)
                {
                    System.Threading.Interlocked.Decrement(ref mConnections);
                    if (recursion)
                    {
                        throw new Exception($"Unable to reach {Host}:{Port} HTTP request, exceeding maximum number of connections");
                    }
                    else
                    {
                        for (int i = 0; i < Clients.Count; i++)
                        {
                            HttpClient httpclient = Clients[i];
                            if (httpclient.IsTimeout && httpclient.Using)
                            {
                                Task.Run(() =>
                                {
                                    try
                                    {
                                        httpclient.RequestCommpletionSource.Error(new TimeoutException($"{Host}:{Port} request timeout!"));
                                    }
                                    finally
                                    {
                                        httpclient.Client.DisConnect();
                                    }
                                });
                            }
                        }
                        System.Threading.Thread.Sleep(50);
                        return(Pop(true));
                    }
                }
                var packet = new HttpClientPacket();

                AsyncTcpClient client;
                if (SSL)
                {
                    client = SocketFactory.CreateSslClient <AsyncTcpClient>(packet, Host, Port, Host);
                    client.CertificateValidationCallback = (o, e, d, f) => true;
                }
                else
                {
                    client = SocketFactory.CreateClient <AsyncTcpClient>(packet, Host, Port);
                    client.CertificateValidationCallback = (o, e, d, f) => true;
                }
                if (this.SslProtocols != null)
                {
                    client.SslProtocols = this.SslProtocols;
                }
                packet.Client    = client;
                client.Connected = c =>
                {
                    c.Socket.NoDelay        = true;
                    c.Socket.ReceiveTimeout = TimeOut;
                    c.Socket.SendTimeout    = TimeOut;
                };
                result        = new HttpClient();
                result.Client = client;
                result.Node   = new LinkedListNode <HttpClient>(result);
                Clients.Add(result);
            }
            result.Using   = true;
            result.TimeOut = TimeOut;
            return(result);
        }