Пример #1
0
        public async Task <TcpSocket> OpenSocket(TimeSpan connectTimeout)
        {
            bool connected = false;

            Interlocked.Increment(ref this.activeConnections);

            try
            {
                if (this.activeConnections > this.MaxPoolSize)
                {
                    throw new InvalidOperationException($"Connection pool maxed out at {this.ActiveConnections} connections.");
                }

#pragma warning disable CA2000 // Dispose objects before losing scope
                var client = new TcpSocket(this);
#pragma warning restore CA2000 // Dispose objects before losing scope

                connected = await client.Connect(this.target, connectTimeout).ConfigureAwait(false);

                if (connected)
                {
                    return(client);
                }
            }
            finally
            {
                if (!connected)
                {
                    Interlocked.Decrement(ref this.activeConnections);
                }
            }

            return(null);
        }
Пример #2
0
        public async Task <TcpSocket> OpenSocket(TimeSpan connectTimeout)
        {
            bool connected = false;

            Interlocked.Increment(ref activeConnections);

            try
            {
                if (activeConnections > MaxPoolSize)
                {
                    throw new InvalidOperationException($"Connection pool maxed out at {ActiveConnections} connections.");
                }

                var client = new TcpSocket(this);

                connected = await client.Connect(target, connectTimeout);

                if (connected)
                {
                    return(client);
                }
            }
            finally
            {
                if (!connected)
                {
                    Interlocked.Decrement(ref activeConnections);
                }
            }

            return(null);
        }
Пример #3
0
        public void Release(TcpSocket socket)
        {
            if (!socket.Connected)
            {
                socket.Free();

                Interlocked.Decrement(ref this.activeConnections);

                return;
            }

            this.Queue.Enqueue(socket);
        }