Exemple #1
0
 public void Stop()
 {
     _isDriving = false;
     HasStopped?.Invoke(this);
 }
Exemple #2
0
        /// <summary>
        /// Main loop of the SMTP server.
        /// </summary>
        internal void Run()
        {
            _stopped = false;

            try
            {
                try
                {
                    // Open a listener to accept client connection
                    TcpListener = new TcpListener(IPAddress.Any, Port);
                    TcpListener.Start();
                }
                catch (Exception e)
                {
                    // If we can't start the listener, we don't start loop
                    _stopped = true;
                    // And store exception that will be thrown back to the thread
                    // that started the server
                    MainThreadException = e;
                }
                finally
                {
                    // Inform calling thread that we can noew receive messages
                    // or that something bad happened.
                    StartedEvent.Set();
                }

                // Server: loop until stopped
                while (!Stopped)
                {
                    Socket socket = null;
                    try
                    {
                        // Accept an incomming client connection
                        socket = TcpListener.AcceptSocket();
                    }
                    catch
                    {
                        socket?.Close();
                        continue; // Non-blocking socket timeout occurred: try accept() again
                    }

                    // Get the input and output streams
                    NetworkStream networkStream = new NetworkStream(socket);
                    StreamReader  input         = new StreamReader(networkStream);
                    StreamWriter  output        = new StreamWriter(networkStream);

                    // Fetch all incomming messages from client, and add them to the queue
                    HandleSmtpTransaction(output, input);

                    // Close client connection, and wait for another one
                    socket.Close();
                }
            }
            catch (Exception e)
            {
                // Send exception back to calling thread
                if (!(e is SocketException socketException && !socketException.Message.Contains("WSACancelBlockingCall")))
                {
                    MainThreadException = e;
                }
            }
            finally
            {
                // The server won't listen anymore
                _stopped = true;

                // Stop the listener if it was started
                if (TcpListener != null)
                {
                    TcpListener.Stop();
                    TcpListener = null;
                }

                HasStopped?.Invoke(this, new EventArgs());
            }
        }
Exemple #3
0
 internal protected virtual void Stopped()
 {
     IsRunning = false;
     HasStopped?.Invoke(this, EventArgs.Empty);
 }