private async Task SetupSessionThenProcessCommands(SmtpConnection connection, SmtpSession session) { await SendGreetingAsync(connection, Greeting ?? Configuration.DefaultGreeting); var sessionInfoParseResponder = new SmtpSessionInfoResponder(session.ResponderFactory, session.SessionInfo); var rawLineDecoder = new RawLineDecoder(connection); rawLineDecoder.RequestDisconnection += (s, e) => { if (!e.DisconnectionExpected) { MailServerLogger.Instance.Warn(String.Format("Connection unexpectedly lost {0}", connection.RemoteEndPoint)); } rawLineDecoder.Cancel(); session.Disconnect(); }; rawLineDecoder.DetectedActivity += (s, e) => session.UpdateActivity(); rawLineDecoder.ProcessLineCommand += async(s, e) => { // ReSharper disable PossibleUnintendedReferenceComparison var response = sessionInfoParseResponder.ProcessLineCommand(e.Buffer); if (response == SmtpResponse.None) { return; } if (response == SmtpResponse.Disconnect) { await SendResponseAsync(connection, response); MailServerLogger.Instance.Debug(String.Format("Remote connection disconnected {0}", connection.RemoteEndPoint)); rawLineDecoder.Cancel(); session.Disconnect(); return; } // ReSharper restore PossibleUnintendedReferenceComparison await SendResponseAsync(connection, response); }; #pragma warning disable 4014 rawLineDecoder.ProcessCommandsAsync(); #pragma warning restore 4014 }
private async Task SetupSessionThenProcessCommands(SmtpConnection connection, SmtpSession session) { await SendGreetingAsync(connection, Greeting ?? Configuration.DefaultGreeting); var sessionInfoParseResponder = new SmtpSessionInfoResponder(session.ResponderFactory, session.SessionInfo); var rawLineDecoder = new RawLineDecoder(connection); rawLineDecoder.RequestDisconnection += (s, e) => { if (!e.DisconnectionExpected) { MailServerLogger.Instance.Warn(String.Format("Connection unexpectedly lost {0}", connection.RemoteEndPoint)); } rawLineDecoder.Cancel(); session.Disconnect(); }; rawLineDecoder.DetectedActivity += (s, e) => session.UpdateActivity(); rawLineDecoder.ProcessLineCommand += async(s, e) => { var response = sessionInfoParseResponder.ProcessLineCommand(e.Buffer); if (response == null || !response.HasValue) { return; } if (response.ResponseCode == SmtpResponses.DisconnectResponseCode) { await SendResponseAsync(connection, response); MailServerLogger.Instance.Debug(String.Format("Remote connection disconnected {0}", connection.RemoteEndPoint)); rawLineDecoder.Cancel(); await Task.Delay(100).ContinueWith(t => session.Disconnect()); return; } await SendResponseAsync(connection, response); }; #pragma warning disable 4014 rawLineDecoder.ProcessCommandsAsync(); #pragma warning restore 4014 }
private async Task SetupSessionThenProcessCommands(SmtpConnection connection, SmtpSession session) { await SendGreetingAsync(connection, Greeting ?? Configuration.DefaultGreeting); var sessionInfoParseResponder = new SmtpSessionInfoResponder(session.ResponderFactory, session.SessionInfo); var rawLineDecoder = new RawLineDecoder(connection); rawLineDecoder.RequestDisconnection += (s, e) => { if (!e.DisconnectionExpected) { MailServerLogger.Instance.Warn(String.Format("Connection unexpectedly lost {0}", connection.RemoteEndPoint)); } rawLineDecoder.Cancel(); session.Disconnect(); }; rawLineDecoder.DetectedActivity += (s, e) => session.UpdateActivity(); rawLineDecoder.ProcessLineCommand += async (s, e) => { // ReSharper disable PossibleUnintendedReferenceComparison var response = sessionInfoParseResponder.ProcessLineCommand(e.Buffer); if (response == SmtpResponse.None) return; if (response == SmtpResponse.Disconnect) { await SendResponseAsync(connection, response); MailServerLogger.Instance.Debug(String.Format("Remote connection disconnected {0}", connection.RemoteEndPoint)); rawLineDecoder.Cancel(); session.Disconnect(); return; } // ReSharper restore PossibleUnintendedReferenceComparison await SendResponseAsync(connection, response); }; #pragma warning disable 4014 rawLineDecoder.ProcessCommandsAsync(); #pragma warning restore 4014 }
private async Task SetupSessionThenProcessCommands(SmtpConnection connection, SmtpSession session) { await SendGreetingAsync(connection, Greeting ?? Configuration.DefaultGreeting); var sessionInfoParseResponder = new SmtpSessionInfoResponder(session.ResponderFactory, session.SessionInfo); var rawLineDecoder = new RawLineDecoder(connection); rawLineDecoder.RequestDisconnection += (s, e) => { if (!e.DisconnectionExpected) { MailServerLogger.Instance.Warn($"Connection unexpectedly lost {connection.RemoteEndPoint}"); } rawLineDecoder.Cancel(); session.Disconnect(); }; rawLineDecoder.DetectedActivity += (s, e) => session.UpdateActivity(); rawLineDecoder.ProcessLineCommand += async (s, e) => { var response = sessionInfoParseResponder.ProcessLineCommand(e.Buffer); if (response == null || !response.HasValue) return; if (response.ResponseCode == SmtpResponses.DisconnectResponseCode) { await SendResponseAsync(connection, response); MailServerLogger.Instance.Debug($"Remote connection disconnected {connection.RemoteEndPoint}"); rawLineDecoder.Cancel(); await Task.Delay(100).ContinueWith(t => session.Disconnect()); return; } await SendResponseAsync(connection, response); }; #pragma warning disable 4014 rawLineDecoder.ProcessCommandsAsync(); #pragma warning restore 4014 }
private static SmtpResponse SendCommand(SmtpSessionInfoResponder parseResponder, string rawCommand) { var command = Encoding.Default.GetBytes(rawCommand); var response = parseResponder.ProcessLineCommand(command); return response; }