Ejemplo n.º 1
0
        // The core server task
        public void Start()
        {
            _listenerTask = Task.Run(async() =>
            {
                _config.Load();

                _tcpListener = TcpListener.Create(_port);
                _tcpListener.Start();
                while (!_stop)
                {
                    TcpClient tcpClient;

                    try
                    {
                        tcpClient = await _tcpListener.AcceptTcpClientAsync();
                    }
                    catch (ObjectDisposedException) when(_stop)
                    {
                        return;
                    }

                    Msg?.Invoke($"Client has connected {tcpClient.Client.RemoteEndPoint}");
                    var task = StartHandleConnectionAsync(tcpClient);
                    // if already faulted, re-throw any error on the calling context
                    if (task.IsFaulted)
                    {
                        task.Wait();
                    }
                }
            });
        }