SimpleServer.cs :  » Email » SharpWebMail » EricDaugherty » CSES » Net » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Email » SharpWebMail 
SharpWebMail » EricDaugherty » CSES » Net » SimpleServer.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using log4net;
using EricDaugherty.CSES.Common;
using EricDaugherty.CSES.SmtpServer;

namespace EricDaugherty.CSES.Net{
  /// <summary>
  /// The delegate that is called when to process a new connection (Socket).
  /// </summary>
  public delegate void ConnectionProcessor( Socket socket );
  
  /// <summary>
  /// This class provides a bare bones implementation
  /// of a Server to allow the SMTPProcessor or POP3Processor
  /// to handle incoming socket connections.
  /// </summary>
  /// <remarks>
  /// This class provides a very simple server implementation that accepts
  /// incoming Socket connections and passes the call to SMTPProcessor or
  /// POP3Processor for processing.  This code is for example/test use only
  /// and should not be considered a production solution.  
  /// </remarks>
  public class SimpleServer
  {
    #region Variables
    
    private bool isRunning = false;
    private TcpListener listener;
    
    private int port;
    private ConnectionProcessor processor;
    
    private static ILog log = LogManager.GetLogger( typeof( SimpleServer ) );
    
    #endregion
        
    #region Constructors
    
    /// <summary>
    /// Creates a new SimpleServer that listens on a specific
    /// port for connections and passes them to the specified delagat
    /// </summary>
    /// <param name="port">The port to listen on.</param>
    /// <param name="processor">The ConnectionProcessor that will handle the incoming connections.</param>
    public SimpleServer( int port, ConnectionProcessor processor )
    {
      this.port = port;
      this.processor = processor;
    }
    
    #endregion
    
    #region Public Methods
    
    /// <summary>
    /// Listens for new connections and starts a new thread to handle each
    /// new connection.  Loops infinitely.
    /// </summary>
    public void Start()
    {
      IPEndPoint endPoint = new IPEndPoint( IPAddress.Any, port );
      listener = new TcpListener( endPoint );
      listener.Start();

      isRunning = true;

      while( isRunning )
      {
        try
        {
          Socket socket = listener.AcceptSocket();
          ConnectionWrapper handler = new ConnectionWrapper( processor, socket );
          new Thread( new ThreadStart( handler.Start ) ).Start();          
        }
        catch {}
      }
    }

    /// <summary>
    /// Stop the server.  This notifies the listener to stop accepting new connections
    /// and that the loop should exit.
    /// </summary>
    public void Stop()
    {
      isRunning = false;
      if( listener != null )
      {
        listener.Stop();
      }
    }
    
    #endregion
  }
  
  /// <summary>
  /// Wraps the ConnectionProcessor and Socket to allow a new thread to be
  /// started that kicks off the ConnectionProcessor's process( Socket) method.
  /// </summary>
  public class ConnectionWrapper
  {
    private ConnectionProcessor processor;
    private Socket socket;
    
    /// <summary>
    /// Create a ConnectionWrapper to allow for a thread start.
    /// </summary>
    /// <param name="processor"></param>
    /// <param name="socket"></param>
    public ConnectionWrapper( ConnectionProcessor processor, Socket socket )
    {
      this.processor = processor;
      this.socket = socket;
    }
    
    /// <summary>
    /// Entry point for the Thread that will handle this Socket connection.
    /// </summary>
    public void Start()
    {
      processor( socket );
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.