private async Task SetupSessionThenProcessCommands(SmtpConnection connection, SmtpSession session)
        {
            await SendGreetingAsync(connection, Greeting ?? Configuration.DefaultGreeting + " " + System.DateTime.Now.ToString("R"));

            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;
                }
                if (response == SmtpResponse.StartTLS)
                {
                    await SendResponseAsync(connection, response);

                    //session.starttls();
                }
                // ReSharper restore PossibleUnintendedReferenceComparison

                await SendResponseAsync(connection, response);
            };

#pragma warning disable 4014
            rawLineDecoder.ProcessCommandsAsync();
#pragma warning restore 4014
        }
        public void request_disconnect_if_stream_causes_IOException()
        {
            var readLineAsyncSource = new ReadLineAsyncThrowIOException();
            var rawLineDecoder = new RawLineDecoder(readLineAsyncSource);

            var disconnectRequested = false;
            rawLineDecoder.RequestDisconnection += (s, e) => { disconnectRequested = true; rawLineDecoder.Cancel(); };

            rawLineDecoder.ProcessCommandsAsync().Wait(100);
            rawLineDecoder.Cancel();

            Assert.True(disconnectRequested);
        }
        public void detect_activity()
        {
            var readLineAsyncSource = new ReadLineAsyncReturnsText("a");
            var rawLineDecoder = new RawLineDecoder(readLineAsyncSource);

            var cts = new CancellationTokenSource();
            cts.CancelAfter(100);

            var detectedActivity = false;
            rawLineDecoder.DetectedActivity += (s, e) => { detectedActivity = true; rawLineDecoder.Cancel(); };

            rawLineDecoder.ProcessCommandsAsync().Wait(100);
            rawLineDecoder.Cancel();

            Assert.True(detectedActivity);
        }
        public void request_disconnect_if_null_received()
        {
            var readLineAsyncSource = new ReadLineAsyncAlwaysNull();
            var rawLineDecoder = new RawLineDecoder(readLineAsyncSource);

            var disconnectRequested = false;
            rawLineDecoder.RequestDisconnection += (s, e) => { disconnectRequested = true; rawLineDecoder.Cancel(); };
            var processLineCommand = false;
            rawLineDecoder.ProcessLineCommand += (s, l) => processLineCommand = true;

            rawLineDecoder.ProcessCommandsAsync().Wait(100);
            rawLineDecoder.Cancel();

            Assert.True(disconnectRequested);
            Assert.False(processLineCommand);
        }
        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
        }
        public void detect_activity()
        {
            var readLineAsyncSource = new ReadLineAsyncReturnsText("a");
            var rawLineDecoder      = new RawLineDecoder(readLineAsyncSource);

            var cts = new CancellationTokenSource();

            cts.CancelAfter(100);

            var detectedActivity = false;

            rawLineDecoder.DetectedActivity += (s, e) => { detectedActivity = true; rawLineDecoder.Cancel(); };

            rawLineDecoder.ProcessCommandsAsync().Wait(100);
            rawLineDecoder.Cancel();

            Assert.True(detectedActivity);
        }
        public void cancel_reading_single_line_command()
        {
            var readLineAsyncSource = new ReadLineAsyncReturnsText("command");
            var rawLineDecoder      = new RawLineDecoder(readLineAsyncSource);

            byte[] processLineCommand = null;
            rawLineDecoder.ProcessLineCommand += (s, e) => processLineCommand = e.Buffer;

            rawLineDecoder.Cancel();
            rawLineDecoder.ProcessCommandsAsync().Wait(100);

            Assert.Null(processLineCommand);
        }
        public void request_disconnect_if_stream_causes_IOException()
        {
            var readLineAsyncSource = new ReadLineAsyncThrowIOException();
            var rawLineDecoder      = new RawLineDecoder(readLineAsyncSource);

            var disconnectRequested = false;

            rawLineDecoder.RequestDisconnection += (s, e) => { disconnectRequested = true; rawLineDecoder.Cancel(); };

            rawLineDecoder.ProcessCommandsAsync().Wait(100);
            rawLineDecoder.Cancel();

            Assert.True(disconnectRequested);
        }
        public void read_single_line_command()
        {
            var readLineAsyncSource = new ReadLineAsyncReturnsText("command");
            var rawLineDecoder      = new RawLineDecoder(readLineAsyncSource);

            var cts = new CancellationTokenSource();

            cts.CancelAfter(100);

            byte[] processLineCommand = null;
            rawLineDecoder.ProcessLineCommand += (s, e) => { processLineCommand = e.Buffer; rawLineDecoder.Cancel(); };

            rawLineDecoder.ProcessCommandsAsync().Wait(100);
            rawLineDecoder.Cancel();

            Assert.Equal("command", Encoding.Default.GetString(processLineCommand));
        }
        public void request_disconnect_if_null_received()
        {
            var readLineAsyncSource = new ReadLineAsyncAlwaysNull();
            var rawLineDecoder      = new RawLineDecoder(readLineAsyncSource);

            var disconnectRequested = false;

            rawLineDecoder.RequestDisconnection += (s, e) => { disconnectRequested = true; rawLineDecoder.Cancel(); };
            var processLineCommand = false;

            rawLineDecoder.ProcessLineCommand += (s, l) => processLineCommand = true;

            rawLineDecoder.ProcessCommandsAsync().Wait(100);
            rawLineDecoder.Cancel();

            Assert.True(disconnectRequested);
            Assert.False(processLineCommand);
        }
Esempio n. 11
0
        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
        }
        public void read_single_line_command()
        {
            var readLineAsyncSource = new ReadLineAsyncReturnsText("command");
            var rawLineDecoder = new RawLineDecoder(readLineAsyncSource);

            var cts = new CancellationTokenSource();
            cts.CancelAfter(100);

            byte[] processLineCommand = null;
            rawLineDecoder.ProcessLineCommand += (s, e) => { processLineCommand = e.Buffer; rawLineDecoder.Cancel(); };

            rawLineDecoder.ProcessCommandsAsync().Wait(100);
            rawLineDecoder.Cancel();

            Assert.Equal("command", Encoding.Default.GetString(processLineCommand));
        }
        public void cancel_reading_single_line_command()
        {
            var readLineAsyncSource = new ReadLineAsyncReturnsText("command");
            var rawLineDecoder = new RawLineDecoder(readLineAsyncSource);

            byte[] processLineCommand = null;
            rawLineDecoder.ProcessLineCommand += (s, e) => processLineCommand = e.Buffer;

            rawLineDecoder.Cancel();
            rawLineDecoder.ProcessCommandsAsync().Wait(100);

            Assert.Null(processLineCommand);
        }
Esempio n. 14
0
        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
        }