public async Task CloseAsync(
            string message,
            SocketCloseStatus closeStatus,
            CancellationToken cancellationToken)
        {
            try
            {
                WebSocket?webSocket = _webSocket;

                if (_disposed || Closed || webSocket is null)
                {
                    return;
                }

                await webSocket.CloseOutputAsync(
                    MapCloseStatus(closeStatus),
                    message,
                    cancellationToken);

                Dispose();
            }
            catch
            {
                // we do not throw here ...
            }
        }
        public async Task CloseAsync(
            string message,
            SocketCloseStatus closeStatus,
            CancellationToken cancellationToken = default)
        {
            try
            {
                if (IsClosed)
                {
                    return;
                }

                await _socketClient.Socket.CloseOutputAsync(
                    MapCloseStatus(closeStatus),
                    message,
                    cancellationToken)
                .ConfigureAwait(false);

                Dispose();
            }
            catch
            {
                // we do not throw here ...
            }
        }
        private static WebSocketCloseStatus MapCloseStatus(SocketCloseStatus closeStatus)
        {
            switch (closeStatus)
            {
            case SocketCloseStatus.EndpointUnavailable:
                return(WebSocketCloseStatus.EndpointUnavailable);

            case SocketCloseStatus.InternalServerError:
                return(WebSocketCloseStatus.InternalServerError);

            case SocketCloseStatus.InvalidMessageType:
                return(WebSocketCloseStatus.InvalidMessageType);

            case SocketCloseStatus.InvalidPayloadData:
                return(WebSocketCloseStatus.InvalidPayloadData);

            case SocketCloseStatus.MandatoryExtension:
                return(WebSocketCloseStatus.MandatoryExtension);

            case SocketCloseStatus.MessageTooBig:
                return(WebSocketCloseStatus.MessageTooBig);

            case SocketCloseStatus.NormalClosure:
                return(WebSocketCloseStatus.NormalClosure);

            case SocketCloseStatus.PolicyViolation:
                return(WebSocketCloseStatus.PolicyViolation);

            case SocketCloseStatus.ProtocolError:
                return(WebSocketCloseStatus.ProtocolError);

            default:
                return(WebSocketCloseStatus.Empty);
            }
        }
 public Task CloseAsync(
     string message,
     SocketCloseStatus closeStatus,
     CancellationToken cancellationToken)
 {
     Closed = true;
     return(Task.CompletedTask);
 }
Example #5
0
        protected virtual async Task Close(SocketCloseStatus closeStatus)
        {
            // this will close the socket even if there are messages
            // in flight waiting on the down stream.
            await m_CloseAsync((int)closeStatus, closeStatus.ToString(), this.m_root_factory_working_token);

            m_Scheduler.RemoveQueue(m_SessionId);
            m_factory.SockedClosed(m_SessionId);
        }
        /// <inheritdoc />
        public async Task CloseAsync(
            string message,
            SocketCloseStatus closeStatus,
            CancellationToken cancellationToken = default)
        {
            try
            {
                if (IsClosed)
                {
                    return;
                }

                if (_activeProtocol is not null)
                {
                    try
                    {
                        await _activeProtocol.TerminateAsync(cancellationToken)
                        .ConfigureAwait(false);
                    }
                    catch
                    {
                        // In case termination of the protocol failed we still want to close the
                        // socket
                    }
                }

                await _socket.CloseOutputAsync(
                    MapCloseStatus(closeStatus),
                    message,
                    cancellationToken)
                .ConfigureAwait(false);

                await DisposeAsync();
            }
            catch
            {
                // we do not throw here ...
            }
        }