All pastes #37023 Raw Edit

tobz

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

namespace DOL
{
	/// <summary>
	/// Manages a list of clients and their session ID.
	/// </summary>
	public class ClientMgr
	{
		/// <summary>
		/// The maximum number of clients allowed, this can be changed.
		/// </summary>
		private static Int32 m_maxClients = GameServer.Instance.Configuration.MaxClientCount;

		/// <summary>
		/// Used to lock on when resizing the client array
		/// </summary>
		private static Object m_lock = 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
			{
				if (m_clients.Length < m_maxClients - 1)
				{
					lock(m_lock)
					{
						if (m_clients.Length < m_maxClients - 1)
						{
							tempSessionID = (Int32)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 the session ID
		/// </summary>
		/// <param name="sessionID">The session ID to release</param>
		public static void RemoveAndRelease(Int32 sessionID)
		{
			GameClient tempClient = m_clients[sessionID];

			m_clients[sessionID] = null;

			tempClient.Player.Delete();

			m_recycledSessionIDs.Enqueue(sessionID);
		}
	}
}