using System;
using System.Collections;
using System.Text;
using DOL.GS;
namespace DOL
{
///
/// Manages a list of clients and their session ID.
///
public class ClientMgr
{
///
/// Locking object for client array resizing
///
private static readonly Object m_resizeLock = 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
{
lock (m_resizeLock)
{
tempSessionID = m_clients.Length;
Array.Resize(ref m_clients, m_clients.Length + 1);
m_clients[tempSessionID] = client;
}
}
return tempSessionID;
}
///
/// Removes the client object and releases its session ID
///
/// The client to remove
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();
}
}
}