Skip to content

Commit

Permalink
minor cleanup and fixed missing Add from session manager
Browse files Browse the repository at this point in the history
  • Loading branch information
cosullivan committed Sep 23, 2024
1 parent 8cd4ca3 commit 9424a3f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Src/SmtpServer.Tests/SmtpServer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

<ItemGroup>
<PackageReference Include="MailKit" Version="3.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
8 changes: 4 additions & 4 deletions Src/SmtpServer.Tests/SmtpServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions Src/SmtpServer/IO/SecurableDuplexPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
9 changes: 2 additions & 7 deletions Src/SmtpServer/Net/EndpointListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ public sealed class EndpointListener : IEndpointListener
/// </summary>
public const string RemoteEndPointKey = "EndpointListener:RemoteEndPoint";

readonly IEndpointDefinition _endpointDefinition;
readonly TcpListener _tcpListener;
readonly Action _disposeAction;

/// <summary>
/// Constructor.
/// </summary>
/// <param name="endpointDefinition">The endpoint definition to create the listener for.</param>
/// <param name="tcpListener">The TCP listener for the endpoint.</param>
/// <param name="disposeAction">The action to execute when the listener has been disposed.</param>
internal EndpointListener(IEndpointDefinition endpointDefinition, TcpListener tcpListener, Action disposeAction)
internal EndpointListener(TcpListener tcpListener, Action disposeAction)
{
_endpointDefinition = endpointDefinition;
_tcpListener = tcpListener;
_disposeAction = disposeAction;
}
Expand Down Expand Up @@ -61,9 +58,7 @@ public async Task<ISecurableDuplexPipe> GetPipeAsync(ISessionContext context, Ca
tcpClient.Close();
tcpClient.Dispose();
}
catch (Exception)
{
}
catch { }
});
}

Expand Down
2 changes: 1 addition & 1 deletion Src/SmtpServer/Net/EndpointListenerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions Src/SmtpServer/SmtpSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 9424a3f

Please sign in to comment.