/// <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.");
                }
            }
        }
            /// <summary>
            /// Complete event
            /// </summary>
            /// <param name="webMessageSocket"></param>
            /// <param name="task"></param>
            internal void CompleteSend(WebSocketMessageSocket webMessageSocket, Task <int> task)
            {
                if (!task.IsCompleted)
                {
                    task.GetAwaiter().OnCompleted(() => CompleteSend(webMessageSocket, task));
                    return;
                }

                SocketErrorString = null;
                IsSocketError     = true;
                BytesTransferred  = 0;

                if (task.IsFaulted)
                {
                    SocketErrorString = task.Exception.ToString();
                }
                else if (task.IsCanceled)
                {
                    SocketErrorString = "Operation aborted";
                }
                else
                {
                    IsSocketError    = false;
                    BytesTransferred = task.Result;
                }

                Completed?.Invoke(webMessageSocket, this);
            }
Example #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();
                }
            }
        }