Click here to Skip to main content
12,695,447 members (27,101 online)
Click here to Skip to main content

Stats

27.8K views
187 downloads
24 bookmarked
Posted

Protocol implementation

, 31 Jan 2005
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

Share

About the Author

engine252
Belgium Belgium
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 31 Jan 2005
Article Copyright 2005 by engine252
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid