Example #1
0
        static void Main(string[] args)
        {
            _Activity = new ScopedActivity("Application");
            try
            {
                Program p = new Program();
                p.Run();

                Console.ReadLine();
            }
            finally
            {
                if (_Listener != null)
                {
                    _Listener.Dispose();
                }
                _Activity.Dispose();
            }
        }
 public LoggingStreamReader(Stream stream, ScopedActivity activity)
     : base(stream)
 {
     _Activity = activity;
 }
Example #3
0
 private bool ValidateClient(TcpClient client, ScopedActivity activity)
 {
     bool retVal = false;
     activity.Log("Checking if client IP Address is black listed");
     IPEndPoint clientEndpoint = (IPEndPoint)client.Client.RemoteEndPoint;
     if (!BlackListedIpAddresses.Contains(clientEndpoint.Address))
     {
         //address is not black listed
         activity.Log("Address {0} is allowed, client passes validation checks", clientEndpoint.Address);
         retVal = true;
     }
     else
     {
         //address is black listed
         activity.Log("Not Processing Client, Client IP Address is black listed: {0}", clientEndpoint.Address);
     }
     return retVal;
 }
Example #4
0
        private void ProcessClient(TcpClient client, TcpListener listener, ScopedActivity activity)
        {
            ListenerInfo info = _ListenerInfos[listener];
            try
            {
                info.Clients.Add(client);

                //gather some metrics...
                activity.Log("Client: {0}", client.Client.RemoteEndPoint);
                activity.Log("Active Connections for Listener: {0}", info.Clients.Count);

                //run validation checks...
                if (ValidateClient(client, activity))
                {
                    //process client
                    activity.Log("Exchanging Commands");
                    using (SmtpStreamProcessor clientProcessor = new SmtpStreamProcessor(client.GetStream(), info.SecurityMode, ServerCertificate))
                    {
                        clientProcessor.Process();
                    }
                }
            }
            finally
            {
                info.Clients.Remove(client);
                client.Close();
            }
        }
Example #5
0
        private void AcceptClient(IAsyncResult result)
        {
            using(ScopedActivity localActivity = new ScopedActivity("Accepting Client Connection"))
            {
                TcpClient client = null;
                TcpListener listener = (TcpListener)result.AsyncState;

                //this section is critical for the robustness of the application.
                //it should only accept the client and begin accepting new connections
                //in the finally block
                try
                {
                    localActivity.Log("Listener: {0}", listener.LocalEndpoint);
                    client = listener.EndAcceptTcpClient(result);
                }
                catch (Exception ex)
                {
                    localActivity.LogException(ex, "An exception occurred while accepting client connection");
                }
                finally
                {
                    listener.BeginAcceptTcpClient(AcceptClient, listener);
                }

                //if client was successfully accepted, continue the processing chain
                if (client != null)
                {
                    ProcessClient(client, listener, localActivity);
                }
            }
        }
Example #6
0
        public void Start()
        {
            try
            {
                _HasStarted = true;
                _Activity = new ScopedActivity("SmtpListener");

                InitializeListeners();
            }
            catch (Exception ex)
            {
                if (_Activity != null)
                {
                    _Activity.LogException(ex, "Exception Occurred While Starting Smtp Listener");
                }
            }
        }