async Task ListenAsync(SmtpSessionContext sessionContext, IEndpointListener endpointListener, 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); // wait for a client connection var stream = await endpointListener.GetStreamAsync(sessionContext, cancellationTokenSource.Token).ConfigureAwait(false); cancellationTokenSource.Token.ThrowIfCancellationRequested(); sessionContext.NetworkClient = new NetworkClient(stream, _options.NetworkBufferSize); if (sessionContext.EndpointDefinition.IsSecure && _options.ServerCertificate != null) { await sessionContext.NetworkClient.Stream.UpgradeAsync(_options.ServerCertificate, _options.SupportedSslProtocols, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); } _sessions.Run(sessionContext, cancellationToken); }
public async Task <Stream> GetStreamAsync(ISessionContext context, CancellationToken cancellationToken) { var stream = await _endpointListener.GetStreamAsync(context, cancellationToken); return(new CustomStream(stream)); }