Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private void ReceiveClients()
        {
            while (!_token.IsCancellationRequested)
            {
                TcpClient client = null;
                CancellationTokenSource tokenSource = new CancellationTokenSource();

                try
                {
                    client = _listener.AcceptTcpClient();

                    try
                    {
                        Task task = new Task(() =>
                        {
                            var ctx = new TcpListenerWebSocketContext(client, null, _secure, _sslConfigInUse, _log);
                            ctx.ReadHeaderRequest();
                            ProcessRequest(ctx);
                        }, tokenSource.Token, TaskCreationOptions.LongRunning);
                        task.ConfigureAwait(false);
                        task.Start();
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex.Message);
                        _log.Debug(ex.ToString());
                        client?.Close();
                        tokenSource?.Cancel();
                    }
                }
                catch (SocketException ex)
                {
                    if (_state == ServerState.ShuttingDown)
                    {
                        _log.Info("The underlying listener is stopped.");
                        break;
                    }

                    _log.Fatal(ex.Message);
                    _log.Debug(ex.ToString());
                    tokenSource?.Cancel();
                    break;
                }
                catch (Exception ex)
                {
                    _log.Fatal(ex.Message);
                    _log.Debug(ex.ToString());
                    client?.Close();
                    tokenSource?.Cancel();
                    break;
                }
            }

            if (_state != ServerState.ShuttingDown)
            {
                Abort();
            }
        }