protected override async Task HandleAsync(
            ISocketConnection connection,
            InitializeConnectionMessage message,
            CancellationToken cancellationToken)
        {
            ConnectionStatus connectionStatus =
                await _socketSessionInterceptor.OnConnectAsync(
                    connection, message, cancellationToken);

            if (connectionStatus.Accepted)
            {
                await connection.SendAsync(AcceptConnectionMessage.Default, cancellationToken);

                await connection.SendAsync(KeepConnectionAliveMessage.Default, cancellationToken);
            }
            else
            {
                var rejectMessage = connectionStatus.Extensions == null
                    ? new RejectConnectionMessage(connectionStatus.Message)
                    : new RejectConnectionMessage(
                    connectionStatus.Message,
                    connectionStatus.Extensions);

                await connection.SendAsync(
                    rejectMessage.Serialize(),
                    cancellationToken);

                await connection.CloseAsync(
                    connectionStatus.Message,
                    SocketCloseStatus.PolicyViolation,
                    cancellationToken);
            }
        }
Пример #2
0
 public async Task HandleAsync(CancellationToken cancellationToken)
 {
     using (var cts = CancellationTokenSource
                      .CreateLinkedTokenSource(cancellationToken))
     {
         if (await _connection.TryOpenAsync().ConfigureAwait(false))
         {
             try
             {
                 _keepAlive.Begin(cts.Token);
                 _messageProcessor.Begin(cts.Token);
                 await _messageReciver.ReceiveAsync(cts.Token)
                 .ConfigureAwait(false);
             }
             finally
             {
                 cts.Cancel();
                 await _connection.CloseAsync(
                     "Session ended.",
                     SocketCloseStatus.NormalClosure,
                     CancellationToken.None)
                 .ConfigureAwait(false);
             }
         }
     }
 }
Пример #3
0
        public async Task HandleAsync(CancellationToken cancellationToken)
        {
            using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            if (await _connection.TryOpenAsync())
            {
                try
                {
                    _keepAlive.Begin(cts.Token);
                    _messageProcessor.Begin(cts.Token);
                    await _messageReceiver.ReceiveAsync(cts.Token);
                }
                catch (OperationCanceledException)
                {
                    // OperationCanceledException are catched and will not
                    // bubble further. We will just close the current subscription
                    // context.
                }
                finally
                {
                    cts.Cancel();
                    await _connection.CloseAsync(
                        "Session ended.",
                        SocketCloseStatus.NormalClosure,
                        CancellationToken.None);
                }
            }
        }
 protected override Task HandleAsync(
     ISocketConnection connection,
     TerminateConnectionMessage message,
     CancellationToken cancellationToken)
 {
     return(connection.CloseAsync(
                "Connection terminated by user.",
                SocketCloseStatus.NormalClosure,
                cancellationToken));
 }
        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();
            }
        }
    protected override async Task HandleAsync(
        ISocketConnection connection,
        TerminateConnectionMessage message,
        CancellationToken cancellationToken)
    {
        await connection.CloseAsync(
            "Connection terminated by user.",
            SocketCloseStatus.NormalClosure,
            cancellationToken);

        await _socketSessionInterceptor.OnCloseAsync(connection, cancellationToken);
    }
        protected override async Task HandleAsync(
            ISocketConnection connection,
            InitializeConnectionMessage message,
            CancellationToken cancellationToken)
        {
            ConnectionStatus connectionStatus =
                _connectMessageInterceptor == null
                    ? ConnectionStatus.Accept()
                    : await _connectMessageInterceptor.OnReceiveAsync(
                    connection, message, cancellationToken)
                .ConfigureAwait(false);

            if (connectionStatus.Accepted)
            {
                await connection.SendAsync(
                    AcceptConnectionMessage.Default.Serialize(),
                    cancellationToken)
                .ConfigureAwait(false);

                await connection.SendAsync(
                    KeepConnectionAliveMessage.Default.Serialize(),
                    cancellationToken)
                .ConfigureAwait(false);
            }
            else
            {
                var rejectMessage = connectionStatus.Extensions == null
                    ? new RejectConnectionMessage(
                    connectionStatus.Message)
                    : new RejectConnectionMessage(
                    connectionStatus.Message,
                    connectionStatus.Extensions);

                await connection.SendAsync(
                    rejectMessage.Serialize(),
                    cancellationToken)
                .ConfigureAwait(false);

                // TODO : resources
                await connection.CloseAsync(
                    connectionStatus.Message,
                    SocketCloseStatus.PolicyViolation,
                    cancellationToken)
                .ConfigureAwait(false);
            }
        }