All pastes #37024 Raw Edit

Mine

public text v1 · immutable
#37024 ·published 2006-01-17 01:31 UTC
rendered paste body
using System;
using System.Collections;
using System.Text;
using DOL.GS;

namespace DOL
{
	/// <summary>
	/// Manages a list of clients and their session ID.
	/// </summary>
	public class ClientMgr
	{
		/// <summary>
		/// Locking object for client array resizing
		/// </summary>
		private static readonly Object m_resizeLock = new Object();

		/// <summary>
		/// Holds all currently connected clients
		/// </summary>
		private static GameClient[] m_clients = new GameClient[0];

		/// <summary>
		/// Holds all recycled session IDs
		/// </summary>
		private static Queue m_recycledSessionIDs = new Queue();

		/// <summary>
		/// Number of IDs recycled and waiting to be reused
		/// </summary>
		public static Int32 RecycledIDCount
		{
			get
			{
				return m_recycledSessionIDs.Count;
			}
		}

		/// <summary>
		/// List of connected clients
		/// </summary>
		public static GameClient[] AllClients
		{
			get
			{
				ArrayList tempClients = new ArrayList();

				foreach (GameClient client in m_clients)
				{
					if (client != null) tempClients.Add(client);
				}

				tempClients.Sort(new PlayerNameComparer());

				return (GameClient[])tempClients.ToArray(typeof(GameClient));
			}
		}

		/// <summary>
		/// List of connected players
		/// </summary>
		public static GameClient[] PlayingClients
		{
			get
			{
				ArrayList tempClients = new ArrayList();

				foreach (GameClient client in m_clients)
				{
					if (client != null &&
						client.IsPlaying &&
						client.Player != null &&
						client.Player.ObjectState == GameObject.eObjectState.Active)
					{
						tempClients.Add(client);
					}
				}

				return (GameClient[])tempClients.ToArray(typeof(GameClient));
			}
		}

		/// <summary>
		/// Number of connected clients
		/// </summary>
		public static Int32 TotalClientCount
		{
			get
			{
				Int32 ClientCount = 0;

				foreach (GameClient client in m_clients)
				{
					if (client != null) ClientCount++;
				}

				return ClientCount;
			}
		}

		/// <summary>
		/// Number of connected players
		/// </summary>
		public static Int32 PlayingClientCount
		{
			get
			{
				Int32 ClientCount = 0;

				foreach (GameClient client in m_clients)
				{
					if (client != null &&
						client.IsPlaying &&
						client.Player != null &&
						client.Player.ObjectState == GameObject.eObjectState.Active)
					{
						ClientCount++;
					}
				}

				return ClientCount;
			}
		}

		/// <summary>
		/// Fetch a GameClient based on it's ID
		/// </summary>
		/// <param name="id">ID to search</param>
		/// <returns>The found GameClient or null if not found</returns>
		public static GameClient GetClientFromID(Int32 id)
		{
			if (id > m_clients.Length - 1) return null;

			return (GameClient)m_clients[id];
		}

		/// <summary>
		/// Fetch a GameClient based on it's ID
		/// </summary>
		/// <param name="id">ID to search</param>
		/// <returns>The found GameClient or null if not found</returns>
		public static GameClient GetClientFromID(UInt16 id)
		{
			if (id > m_clients.Length - 1) return null;

			return (GameClient)m_clients[id];
		}

		/// <summary>
		/// Acquires a session ID and stores the client object
		/// </summary>
		/// <param name="client">Client object to store</param>
		/// <returns>The acquired session ID</returns>
		public static Int32 GetSessionID(GameClient client)
		{
			Int32 tempSessionID = -1;

			if (m_recycledSessionIDs.Count > 0)
			{
				Object sID = m_recycledSessionIDs.Dequeue();

				if (sID != null)
				{
					tempSessionID = (Int32)sID;

					m_clients[tempSessionID] = client;
				}
			}
			else
			{
				lock (m_resizeLock)
				{
					tempSessionID = m_clients.Length;

					Array.Resize<GameClient>(ref m_clients, m_clients.Length + 1);

					m_clients[tempSessionID] = client;
				}
			}

			return tempSessionID;
		}

		/// <summary>
		/// Removes the client object and releases its session ID
		/// </summary>
		/// <param name="client">The client to remove</param>
		public static void RemoveClient(GameClient client)
		{
			if (entry == null) return;

			GameClient tempClient = m_clients[client.SessionID];

			m_clients[client.SessionID] = null;

			if (tempClient == null || tempClient.Player == null)
				return;

			m_recycledSessionIDs.Enqueue(tempClient.SessionID);

			tempClient.Player.Delete();
		}
	}
}