namespace Supercell.Laser.Server.Networking { using Supercell.Laser.Server.Networking.Session; using Supercell.Laser.Server.Logic; public static class Connections { private static readonly object SyncRoot = new object(); public static int Count { get { lock (SyncRoot) { return ActiveConnections.Count; } } } private static List ActiveConnections; private static Thread Thread; public static void Init() { ActiveConnections = new List(); Thread = new Thread(Update); Thread.Start(); } private static void Update() { while (true) { foreach (Connection connection in GetSnapshot()) { if (!connection.MessageManager.IsAlive()) { RemoveConnection(connection, "heartbeat-timeout"); } } Thread.Sleep(1000); } } public static void AddConnection(Connection connection) { lock (SyncRoot) { ActiveConnections.Add(connection); } } public static void RemoveConnection(Connection connection, string reason) { bool removed; lock (SyncRoot) { removed = ActiveConnections.Remove(connection); } if (!removed) { return; } if (connection.MessageManager.HomeMode != null && connection.Avatar != null) { Sessions.Remove(connection.Avatar.AccountId); } connection.Close(); Logger.Network($"Closed {(connection == null ? "conn#?" : connection.Describe())} — {reason}, {Connections.Count} connections, {Sessions.Count} online"); } private static Connection[] GetSnapshot() { lock (SyncRoot) { return ActiveConnections.ToArray(); } } public static Connection[] GetConnectionsSnapshot() { return GetSnapshot(); } } }