public async Task ReturnAsync(
            ISocketConnection connection,
            CancellationToken cancellationToken = default)
        {
            await _semaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                if (_connections.TryGetValue(connection.Name, out ConnectionInfo? connectionInfo))
                {
                    connectionInfo.Rentals--;
                }
                else
                {
                    throw new ArgumentException(
                              "The specified connection does not belong to this pool.",
                              nameof(connection));
                }

                if (connectionInfo.Rentals < 1)
                {
                    try
                    {
                        for (int i = 0; i < _connectionInterceptors.Length; i++)
                        {
                            await _connectionInterceptors[i].OnDisconnectAsync(
                                connectionInfo.Connection)
                            .ConfigureAwait(false);
                        }
                        await TerminateConnectionAsync(connection, cancellationToken)
                        .ConfigureAwait(false);

                        _connections.Remove(connection.Name);
                        await connection.CloseAsync(
                            "All subscriptions closed.",
                            SocketCloseStatus.NormalClosure,
                            cancellationToken)
                        .ConfigureAwait(false);
                    }
                    catch
                    {
                        // we ignore errors here
                    }
                    finally
                    {
                        connection.Dispose();
                    }
                }
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Пример #2
0
 protected virtual void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         if (disposing && _disposeConnection)
         {
             _connection.Dispose();
         }
         _disposed = true;
     }
 }
 public void Dispose()
 {
     _isDisposed             = true;
     _keepAliveTimer.Enabled = false;
     _keepAliveTimer.Stop();
     _packetProtocol.MessageArrived   = null;
     _packetProtocol.KeepAliveArrived = null;
     _socket.Disconnect(false);
     _socket.Dispose();
     _socket.Close();
 }