/// <inheritdoc/>
        public void OnAccept(HttpContext context, WebSocket webSocket)
        {
            SecureChannel channel = null;

            // check if the accept socket has been created.
            if (webSocket != null)
            {
                try {
                    channel = new SecureChannel(_listenerId, this,
                                                _bufferManager, _quotas, _controller.Certificate,
                                                _controller.CertificateChain, GetEndpoints());
                    channel.SetRequestReceivedCallback(OnRequestReceived);

                    // Wrap socket in channel to read and write.
                    var socket = new WebSocketMessageSocket(channel, webSocket,
                                                            _bufferManager, _quotas.MaxBufferSize);
                    var channelId = (uint)Interlocked.Increment(ref _lastChannelId);
                    channel.Attach(channelId, socket);

                    _channels.TryAdd(channelId, channel);
                    _logger.Debug("Started channel {channelId} on {socket.Handle}...",
                                  channelId, socket.Handle);
                }
                catch (Exception ex) {
                    _logger.Error(ex, "Unexpected error accepting a new connection.");
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Handles a new connection.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnAccept(object sender, SocketAsyncEventArgs e)
        {
            SecureChannel channel = null;

            while (true)
            {
                var error = e.SocketError;
                var token = e.UserToken;

                // check if the accept socket has been created.
                if (e.AcceptSocket != null && e.SocketError == SocketError.Success)
                {
                    try {
                        // Wrap socket in channel to read and write.
                        channel = new SecureChannel(_listenerId, this,
                                                    _bufferManager, _quotas,
                                                    _serverCertificate ?? _controller.Certificate,
                                                    _serverCertificateChain ?? _controller.CertificateChain,
                                                    GetEndpoints());
                        channel.SetRequestReceivedCallback(new TcpChannelRequestEventHandler(OnRequestReceived));

                        var channelId = (uint)Interlocked.Increment(ref _lastChannelId);
#pragma warning disable IDE0068 // Use recommended dispose pattern
                        var socket = new TcpMessageSocket(channel, e.AcceptSocket,
                                                          _bufferManager, _quotas.MaxBufferSize);
#pragma warning restore IDE0068 // Use recommended dispose pattern
                        channel.Attach(channelId, socket);

                        _channels.TryAdd(channelId, channel);
                        _logger.Debug("Started channel {channelId} on {socket}...",
                                      channelId, socket.Handle);
                    }
                    catch (Exception ex) {
                        _logger.Error(ex, "Unexpected error accepting a new connection.");
                    }
                }

                e.Dispose();
                if (error != SocketError.OperationAborted && token is Socket listeningSocket)
                {
                    // Schedule new accept
                    try {
                        e            = new SocketAsyncEventArgs(); // Should cache it
                        e.Completed += OnAccept;
                        e.UserToken  = listeningSocket;
                        if (!listeningSocket.AcceptAsync(e))
                        {
                            continue; // Handle synchronously
                        }
                    }
                    catch (Exception ex) {
                        _logger.Error(ex, "Unexpected error listening for a connections.");
                        // Stop listening
                    }
                }
                break;
            }
        }
Beispiel #3
0
        /// <inheritdoc/>
        public void OnAccept(HttpContext context, WebSocket webSocket)
        {
            SecureChannel channel = null;

            // check if the accept socket has been created.
            if (webSocket != null)
            {
                try {
                    channel = new SecureChannel(_listenerId, this,
                                                _bufferManager, _quotas, _controller.Certificate,
                                                _controller.CertificateChain, GetEndpoints());
                    channel.SetRequestReceivedCallback(OnRequestReceived);

                    // Wrap socket in channel to read and write.
#pragma warning disable IDE0068 // Use recommended dispose pattern
                    var socket = new WebSocketMessageSocket(channel, webSocket,
                                                            _bufferManager, _quotas.MaxBufferSize, _logger);
#pragma warning restore IDE0068 // Use recommended dispose pattern
                    var channelId = (uint)Interlocked.Increment(ref _lastChannelId);
                    channel.Attach(channelId, socket);
                    if (!_channels.TryAdd(channelId, channel))
                    {
                        throw new InvalidProgramException("Failed to add channel");
                    }
                    channel = null;
                    _logger.Debug("Started channel {channelId} on {socket.Handle}...",
                                  channelId, socket.Handle);
                }
                catch (Exception ex) {
                    _logger.Error(ex, "Unexpected error accepting a new connection.");
                }
                finally {
                    channel?.Dispose();
                }
            }
        }