using System; using System.Collections; using System.Text; using DOL.GS; using CamelLight.Framework; namespace DOL { /// /// Manages a list of clients and their session ID. /// public class ClientMgr { /// /// The maximum number of clients allowed, this can be changed. /// private static Int32 m_maxClients = GameServer.Instance.Configuration.MaxClientCount; /// /// Used to lock on when resizing the client array /// private static Object m_lock = new Object(); /// /// Holds all currently connected clients /// private static GameClient[] m_clients = new GameClient[0]; /// /// Holds all recycled session IDs /// private static Queue m_recycledSessionIDs = new Queue(); /// /// Number of IDs recycled and waiting to be reused /// public static Int32 RecycledIDCount { get { return m_recycledSessionIDs.Count; } } /// /// List of connected clients /// 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)); } } /// /// List of connected players /// 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)); } } /// /// Number of connected clients /// public static Int32 TotalClientCount { get { Int32 ClientCount = 0; foreach (GameClient client in m_clients) { if (client != null) ClientCount++; } return ClientCount; } } /// /// Number of connected players /// 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; } } /// /// Fetch a GameClient based on it's ID /// /// ID to search /// The found GameClient or null if not found public static GameClient GetClientFromID(Int32 id) { if (id > m_clients.Length - 1) return null; return (GameClient)m_clients[id]; } /// /// Fetch a GameClient based on it's ID /// /// ID to search /// The found GameClient or null if not found public static GameClient GetClientFromID(UInt16 id) { if (id > m_clients.Length - 1) return null; return (GameClient)m_clients[id]; } /// /// Acquires a session ID and stores the client object /// /// Client object to store /// The acquired session ID 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(ref m_clients, m_clients.Length + 1); m_clients[tempSessionID] = client; } } } } return tempSessionID; } /// /// Removes the client object, and releases the session ID /// /// The session ID to release public static void RemoveAndRelease(Int32 sessionID) { GameClient tempClient = m_clients[sessionID]; m_clients[sessionID] = null; tempClient.Player.Delete(); m_recycledSessionIDs.Enqueue(sessionID); } } }