Exemplo n.º 1
0
        /// <summary>
        /// Listen for SMTP traffic on the given endpoint.
        /// </summary>
        /// <param name="endpointDefinition">The definition of the endpoint to listen on.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A task which performs the operation.</returns>
        async Task ListenAsync(IEndpointDefinition endpointDefinition, CancellationToken cancellationToken)
        {
            // The listener can be stopped either by the caller cancelling the CancellationToken used when starting the server, or when calling
            // the shutdown method. The Shutdown method will stop the listeners and allow any active sessions to finish gracefully.
            var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_shutdownTokenSource.Token, cancellationToken);

            using var endpointListener = _endpointListenerFactory.CreateListener(endpointDefinition);

            while (cancellationTokenSource.Token.IsCancellationRequested == false)
            {
                var sessionContext = new SmtpSessionContext(_serviceProvider, _options, endpointDefinition);

                try
                {
                    // wait for a client connection
                    sessionContext.Pipe = await endpointListener.GetPipeAsync(sessionContext, cancellationTokenSource.Token).ConfigureAwait(false);

                    cancellationTokenSource.Token.ThrowIfCancellationRequested();

                    if (sessionContext.EndpointDefinition.IsSecure && _options.ServerCertificate != null)
                    {
                        await sessionContext.Pipe.UpgradeAsync(_options.ServerCertificate, _options.SupportedSslProtocols, cancellationToken).ConfigureAwait(false);

                        cancellationToken.ThrowIfCancellationRequested();
                    }

                    _sessions.Run(sessionContext, cancellationTokenSource.Token);
                }
                catch (OperationCanceledException) { }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Listen for SMTP traffic on the given endpoint.
        /// </summary>
        /// <param name="endpointDefinition">The definition of the endpoint to listen on.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A task which performs the operation.</returns>
        async Task ListenAsync(IEndpointDefinition endpointDefinition, CancellationToken cancellationToken)
        {
            // The listener can be stopped either by the caller cancelling the CancellationToken used when starting the server, or when calling
            // the shutdown method. The Shutdown method will stop the listeners and allow any active sessions to finish gracefully.
            var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_shutdownTokenSource.Token, cancellationToken);

            using var endpointListener = _endpointListenerFactory.CreateListener(endpointDefinition);

            while (cancellationTokenSource.Token.IsCancellationRequested == false)
            {
                var sessionContext = new SmtpSessionContext(_serviceProvider, _options, endpointDefinition);

                try
                {
                    // wait for a client connection
                    sessionContext.Pipe = await GetPipeAsync(endpointListener, sessionContext, cancellationTokenSource.Token).ConfigureAwait(false);
                }
                catch (OperationCanceledException) { }
                catch (Exception ex)
                {
                    OnSessionFaulted(new SessionFaultedEventArgs(sessionContext, ex));
                    continue;
                }

                _sessions.Run(sessionContext, cancellationTokenSource.Token);
            }
        }