Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Protocol implementation

Rate me:
Please Sign up or sign in to vote.
2.96/5 (9 votes)
31 Jan 20053 min read 34.9K   254   24  
Add some protocol support to your server.
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading;

namespace Infinity.Networking
{
	/// <summary>
	/// Summary description for MultiThreadedServerBase.
	/// </summary>
	public class MultiThreadedServerBase
	{
		public const int BACKLOG = 10;
	
		private Thread _mainThread;

		private int _port;
		private string _localIP;
		private Socket _mainSoc;
		private ArrayList _connections;
		private MessageCollection _messageHandlers;

		// a collection of (messageHandlers) functions
		internal MessageCollection MessageHandlers
		{
			get
			{
				return _messageHandlers;
			}
		}

		public MultiThreadedServerBase(string localIP,int port)
		{
			_messageHandlers = new MessageCollection();
			_connections = new ArrayList();
			_port = port;
			_localIP = localIP;
		}

		public void Start()
		{
			// check to see if the thread is alraidy running.
			if (_mainThread != null)
			{
				new Exception("close the server before starting it again.");
			}
			else
			{
				_mainThread = new Thread(new ThreadStart(Listen));
				_mainSoc = new Socket(
					AddressFamily.InterNetwork,
					SocketType.Stream,
					ProtocolType.Tcp);

				_mainSoc.Bind(new IPEndPoint(IPAddress.Parse(_localIP),_port));
				_mainThread.Start();
			}
		}

		// this makes it possible to add other ClientConnection based classes
		// implement the protocol.
		protected virtual ClientConnection OnCreateNewConnection(Socket socket)
		{
			return new ClientConnection(_mainSoc.Accept(),this);	
		}

		private void Listen()
		{
			_mainSoc.Listen(BACKLOG);

			try
			{
				while(true)
				{
					ClientConnection client = OnCreateNewConnection(_mainSoc.Accept());
					_connections.Add(client);
				}
			}
			catch(SocketException e)
			{
				// if the client closes the connection we will get an exception.
				if (e.ErrorCode != (int)SocketErrorCodes.InterruptedFunctionCall)
					System.Diagnostics.Debug.WriteLine(
						e.Message.ToString());
			}
		}

		public void Stop()
		{
			if (_mainSoc != null)
			{
				_mainSoc.Close();
				_mainSoc = null;

				_mainThread.Abort();
				_mainThread = null;

				foreach(ClientConnection con in _connections)
				{
					con.Dispose();
				}
			}
		}

		public void Suspend()
		{
			_mainThread.Suspend();
		}

		public void Resume()
		{
			_mainThread.Resume();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions