void Remove(SmtpSessionHandle handle) { lock (_sessionsLock) { _sessions.Remove(handle); } }
async Task RunAsync(SmtpSessionHandle handle, CancellationToken cancellationToken) { try { _smtpServer.OnSessionCreated(new SessionEventArgs(handle.SessionContext)); await handle.Session.RunAsync(cancellationToken); _smtpServer.OnSessionCompleted(new SessionEventArgs(handle.SessionContext)); } catch (OperationCanceledException) { _smtpServer.OnSessionCancelled(new SessionEventArgs(handle.SessionContext)); } catch (Exception ex) { _smtpServer.OnSessionFaulted(new SessionFaultedEventArgs(handle.SessionContext, ex)); } finally { await handle.SessionContext.Pipe.Input.CompleteAsync(); handle.SessionContext.Pipe.Dispose(); } }
void Add(SmtpSessionHandle handle) { lock (_sessionsLock) { _sessions.Add(handle); } }
internal void Run(SmtpSessionContext sessionContext, CancellationToken cancellationToken) { var handle = new SmtpSessionHandle(new SmtpSession(sessionContext), sessionContext); Add(handle); handle.CompletionTask = RunAsync(handle, cancellationToken); // ReSharper disable once MethodSupportsCancellation handle.CompletionTask.ContinueWith( task => { Remove(handle); }); }