hMailServer.Core.Server.RunAsync C# (CSharp) Méthode

RunAsync() public méthode

public RunAsync ( ) : Task
Résultat Task
        public async Task RunAsync()
        {
            _listener.Start();

            while (true)
            {
                TcpClient tcpClient = null;

                try
                {
                    tcpClient = await _listener.AcceptTcpClientAsync();
                }
                catch (ObjectDisposedException)
                {
                    // When TcpListener is stopped, outstanding calls to AcceptTcpClientAsync
                    // will throw an ObjectDisposedException. When this happens, it's time to 
                    // exit.
                    return;
                }


                var connection = new Connection(tcpClient, _cancellationTokenSource.Token);

                var session = _sessionFactory();

                var sessionTask = session.HandleConnection(connection);

                SessionManager.IncreaseSessionCount(session.Protocol);

                HandleSessionAsynchronously(sessionTask, connection, session);
            }
        }

Usage Example

        static void Main(string[] args)
        {
            var client = new DnsClient();

            var log = new Log();

            var config = ServiceConfigurationReader.Read();
            
            var container = new Container(new DependencyRegistry(config));

            var smtpServerSessionConfiguration = new SmtpServerSessionConfiguration
                {
                    TempDirectory = config.TempDirectory
                };

            Func<ISession> smtpSessionFactory = () => 
                new SmtpServerSession(new SmtpServerCommandHandler(container), log, smtpServerSessionConfiguration);

            var serverConfiguration = new ServerConfiguration()
                {
                    Port = 25
                };

            var smtpServer = new Server(smtpSessionFactory, log, serverConfiguration);
            var smtpRunTask = smtpServer.RunAsync();

            Func<ISession> pop3SessionFactory = () =>
             new Pop3ServerSession(new Pop3ServerCommandHandler(container), log, new Pop3ServerSessionConfiguration());

            var pop3ServerConfiguration = new ServerConfiguration()
                {
                    Port = 110
                };



            var deliverer = new MessageDeliverer(container);
            var delivererTask = deliverer.RunAsync();





            var pop3Server = new Server(pop3SessionFactory, log, pop3ServerConfiguration);
            var pop3RunTask = pop3Server.RunAsync();

            log.LogApplicationInfo("Server running...");


            Console.WriteLine("Press any key to exit");
            Console.ReadLine();

            log.LogApplicationInfo("Shutting down");

            var smtpStopTask = smtpServer.StopAsync();
            var pop3StopTask = pop3Server.StopAsync();

        }
All Usage Examples Of hMailServer.Core.Server::RunAsync