Exemplo n.º 1
0
        public TcpClient Pop()
        {
            TcpClient result;

            if (!mPools.TryPop(out result))
            {
                int value = System.Threading.Interlocked.Increment(ref mConnections);
                if (value > mMaxConnections)
                {
                    System.Threading.Interlocked.Decrement(ref mConnections);
                    throw new Exception("Unable to reach HTTP request, exceeding maximum number of connections");
                }
                var packet = new HttpClientPacket();
                result           = SocketFactory.CreateClient <TcpClient>(packet, Host, Port);
                packet.Client    = result;
                result.Connected = c => { c.Socket.NoDelay = true; };
                result.Connect();
                result.Socket.SendTimeout    = TimeOut;
                result.Socket.ReceiveTimeout = TimeOut;
            }
            else
            {
                var value = System.Threading.Interlocked.Decrement(ref mPoolConnectins);
            }
            return(result);
        }
Exemplo n.º 2
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 client = Clients[i];
                            if (client.IsTimeout && client.Using)
                            {
                                Task.Run(() =>
                                {
                                    try
                                    {
                                        client.RequestCommpletionSource.Error(new TimeoutException($"{Host}:{Port} request timeout!"));
                                    }
                                    finally
                                    {
                                        client.Client.DisConnect();
                                    }
                                });
                            }
                        }
                        System.Threading.Thread.Sleep(50);
                        return(Pop(true));
                    }
                }
                var packet = new HttpClientPacket();
                if (Async)
                {
                    AsyncTcpClient client;
                    if (SSL)
                    {
                        client = SocketFactory.CreateSslClient <AsyncTcpClient>(packet, Host, Port, Host);
                    }
                    else
                    {
                        client = SocketFactory.CreateClient <AsyncTcpClient>(packet, Host, Port);
                    }
                    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);
                }
                else
                {
                    TcpClient client;
                    if (SSL)
                    {
                        client = SocketFactory.CreateSslClient <TcpClient>(packet, Host, Port, Host);
                    }
                    else
                    {
                        client = SocketFactory.CreateClient <TcpClient>(packet, Host, Port);
                    }
                    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 = BeetleX.TimeWatch.GetElapsedMilliseconds() + TimeOut * 1000;
            return(result);
        }