Beispiel #1
0
        /// <summary>
        /// Starts listening to remote connections in a loop that only ends when <paramref name="cancellationToken"/> is marked cancelled.
        /// </summary>
        public async void StartAsync(CancellationToken cancellationToken)
        {
            try
            {
                listener.Start();
            }
            catch (Exception ex)
            {
                logger.Log(App.LogLevel.Error, "Cannot listen: " + ex.Message);
                throw;
            }

            logger.Log(App.LogLevel.Info, "Listening at port " + port);

            while (!cancellationToken.IsCancellationRequested)
            {
                var client = await listener.AcceptTcpClientAsync();

                var task = processClientAsync(client, cancellationToken);

                if (task.IsFaulted)
                {
                    await task;
                }
            }

            listener.Stop();
        }
Beispiel #2
0
        private static void ensureLogToConsole(App.LogLevel level, string log)
        {
            _logger.Log(level, log);

            if (_options.LogMode == LogMode.File)
            {
                App.ConsoleLogger.Log(level, log);
            }
        }
Beispiel #3
0
        public bool Connect()
        {
            try
            {
                _client.Connect(remoteHost, remotePort);
            }
            catch (SocketException ex)
            {
                logger.Log(App.LogLevel.Error, "Cannot connect to peer: " + ex.Message);
                return(false);
            }

            return(true);
        }