From 9424a3f50dd2cee6b35cd675ae24ed2946f07acf Mon Sep 17 00:00:00 2001 From: cosullivan Date: Mon, 23 Sep 2024 21:41:43 +0800 Subject: [PATCH] minor cleanup and fixed missing Add from session manager --- Src/SmtpServer.Tests/SmtpServer.Tests.csproj | 4 ++-- Src/SmtpServer.Tests/SmtpServerTests.cs | 8 ++++---- Src/SmtpServer/IO/SecurableDuplexPipe.cs | 18 +++++++++--------- Src/SmtpServer/Net/EndpointListener.cs | 9 ++------- Src/SmtpServer/Net/EndpointListenerFactory.cs | 2 +- Src/SmtpServer/SmtpSessionManager.cs | 10 +++++----- 6 files changed, 23 insertions(+), 28 deletions(-) diff --git a/Src/SmtpServer.Tests/SmtpServer.Tests.csproj b/Src/SmtpServer.Tests/SmtpServer.Tests.csproj index 38bb095..f8732be 100644 --- a/Src/SmtpServer.Tests/SmtpServer.Tests.csproj +++ b/Src/SmtpServer.Tests/SmtpServer.Tests.csproj @@ -6,8 +6,8 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Src/SmtpServer.Tests/SmtpServerTests.cs b/Src/SmtpServer.Tests/SmtpServerTests.cs index 9c69b88..300bf03 100644 --- a/Src/SmtpServer.Tests/SmtpServerTests.cs +++ b/Src/SmtpServer.Tests/SmtpServerTests.cs @@ -382,10 +382,10 @@ public async Task SessionTimeoutIsExceeded_DelayedAuthenticate() var port = 9025; using var disposable = CreateServer(endpoint => endpoint - .SessionTimeout(sessionTimeout) - .IsSecure(true) - .Certificate(CreateCertificate()) - ); + .SessionTimeout(sessionTimeout) + .IsSecure(true) + .Certificate(CreateCertificate()) + ); using var tcpClient = new TcpClient(server, port); using var sslStream = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); diff --git a/Src/SmtpServer/IO/SecurableDuplexPipe.cs b/Src/SmtpServer/IO/SecurableDuplexPipe.cs index 9031c71..70ede62 100644 --- a/Src/SmtpServer/IO/SecurableDuplexPipe.cs +++ b/Src/SmtpServer/IO/SecurableDuplexPipe.cs @@ -42,15 +42,15 @@ public async Task UpgradeAsync(X509Certificate certificate, SslProtocols protoco try { - var sslServerAuthenticationOptions = new SslServerAuthenticationOptions - { - ServerCertificate = certificate, - ClientCertificateRequired = false, - EnabledSslProtocols = protocols, - CertificateRevocationCheckMode = X509RevocationMode.Online - }; - - await sslStream.AuthenticateAsServerAsync(sslServerAuthenticationOptions, cancellationToken); + await sslStream.AuthenticateAsServerAsync( + new SslServerAuthenticationOptions + { + ServerCertificate = certificate, + ClientCertificateRequired = false, + EnabledSslProtocols = protocols, + CertificateRevocationCheckMode = X509RevocationMode.Online + }, + cancellationToken); } catch { diff --git a/Src/SmtpServer/Net/EndpointListener.cs b/Src/SmtpServer/Net/EndpointListener.cs index 160b021..c7d2946 100644 --- a/Src/SmtpServer/Net/EndpointListener.cs +++ b/Src/SmtpServer/Net/EndpointListener.cs @@ -21,19 +21,16 @@ public sealed class EndpointListener : IEndpointListener /// public const string RemoteEndPointKey = "EndpointListener:RemoteEndPoint"; - readonly IEndpointDefinition _endpointDefinition; readonly TcpListener _tcpListener; readonly Action _disposeAction; /// /// Constructor. /// - /// The endpoint definition to create the listener for. /// The TCP listener for the endpoint. /// The action to execute when the listener has been disposed. - internal EndpointListener(IEndpointDefinition endpointDefinition, TcpListener tcpListener, Action disposeAction) + internal EndpointListener(TcpListener tcpListener, Action disposeAction) { - _endpointDefinition = endpointDefinition; _tcpListener = tcpListener; _disposeAction = disposeAction; } @@ -61,9 +58,7 @@ public async Task GetPipeAsync(ISessionContext context, Ca tcpClient.Close(); tcpClient.Dispose(); } - catch (Exception) - { - } + catch { } }); } diff --git a/Src/SmtpServer/Net/EndpointListenerFactory.cs b/Src/SmtpServer/Net/EndpointListenerFactory.cs index 097efca..48f5386 100644 --- a/Src/SmtpServer/Net/EndpointListenerFactory.cs +++ b/Src/SmtpServer/Net/EndpointListenerFactory.cs @@ -32,7 +32,7 @@ public virtual IEndpointListener CreateListener(IEndpointDefinition endpointDefi var endpointEventArgs = new EndpointEventArgs(endpointDefinition, tcpListener.LocalEndpoint); OnEndpointStarted(endpointEventArgs); - return new EndpointListener(endpointDefinition, tcpListener, () => OnEndpointStopped(endpointEventArgs)); + return new EndpointListener(tcpListener, () => OnEndpointStopped(endpointEventArgs)); } /// diff --git a/Src/SmtpServer/SmtpSessionManager.cs b/Src/SmtpServer/SmtpSessionManager.cs index 910a514..e02ab8d 100644 --- a/Src/SmtpServer/SmtpSessionManager.cs +++ b/Src/SmtpServer/SmtpSessionManager.cs @@ -19,19 +19,19 @@ internal SmtpSessionManager(SmtpServer smtpServer) internal void Run(SmtpSessionContext sessionContext, CancellationToken cancellationToken) { var handle = new SmtpSessionHandle(new SmtpSession(sessionContext), sessionContext); + Add(handle); - var smtpSessionTask = RunAsync(handle, cancellationToken).ContinueWith(task => + handle.CompletionTask = RunAsync(handle, cancellationToken).ContinueWith(task => { Remove(handle); }); - - handle.CompletionTask = smtpSessionTask; } async Task RunAsync(SmtpSessionHandle handle, CancellationToken cancellationToken) { - using var sessionReadTimeoutCancellationTokenSource = new CancellationTokenSource(handle.SessionContext.EndpointDefinition.SessionTimeout); - using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, sessionReadTimeoutCancellationTokenSource.Token); + using var sessionTimeoutCancellationTokenSource = new CancellationTokenSource(handle.SessionContext.EndpointDefinition.SessionTimeout); + + using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, sessionTimeoutCancellationTokenSource.Token); try {