namespace Holo.GameConnectionSystem { using Holo; using Holo.Source.GameConnectionSystem; using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Sockets; public static class gameSocketServer { private static int _acceptedConnections; private static HashSet _activeConnections; private static int _maxConnections = int.Parse(Config.getTableEntry("server_game_maxconnections")); private static int _Port = int.Parse(Config.getTableEntry("server_game_port")); private static bool AcceptConnections = true; private static Hashtable connections = new Hashtable(2); private static Socket socketHandler; internal static void AddConnection(string IP) { int num = 0; if (connections.ContainsKey(IP)) { num = (int) connections[IP]; connections.Remove(IP); } else { num = 0; } int num2 = num + 1; connections.Add(IP, num2); } internal static void Destroy() { AcceptConnections = false; foreach (int num in _activeConnections) { freeConnection(num); } } internal static void freeConnection(int connectionID) { if (_activeConnections.Contains(connectionID)) { _activeConnections.Remove(connectionID); _acceptedConnections--; } } internal static int GetConnectionAmount(string IP) { if (connections.ContainsKey(IP)) { return (int) connections[IP]; } return 0; } internal static void InncommingDataRequest(IAsyncResult iAr) { try { Socket socket = ((Socket) iAr.AsyncState).EndAccept(iAr); string iP = socket.RemoteEndPoint.ToString().Split(new char[] { ':' })[0]; GetConnectionAmount(iP); if ((acceptedConnections >= maxConnections) || !AcceptConnections) { socket.Shutdown(SocketShutdown.Both); socket.Close(); } else if (connectionHelper.IpIsBanned(iP)) { socket.Shutdown(SocketShutdown.Both); socket.Close(); } else { int num = NewConnectionRequestID(); if (num > 0) { AddConnection(iP); new gameConnection(socket, num); } } } catch { } socketHandler.BeginAccept(new AsyncCallback(gameSocketServer.InncommingDataRequest), socketHandler); } internal static int NewConnectionRequestID() { _acceptedConnections++; _activeConnections.Add(_acceptedConnections); return _acceptedConnections; } internal static void RemoveConnection(string IP) { int num = 0; if (connections.ContainsKey(IP)) { num = (int) connections[IP]; } else { connections.Remove(IP); } if (num > 1) { int num2 = num - 1; connections.Remove(IP); connections.Add(IP, num2); } else { connections.Remove(IP); } } internal static void SetupSocket() { Out.WriteLine("Setting up socket on port " + _Port); connectionHelper.Init(); AcceptConnections = true; if (_activeConnections != null) { _activeConnections.Clear(); } _activeConnections = new HashSet(); StartListening(); } internal static void StartListening() { bool flag = false; try { if ((_Port == 0) || (_maxConnections == 0)) { throw new NullReferenceException(); } socketHandler = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint localEP = new IPEndPoint(IPAddress.Any, _Port); socketHandler.Bind(localEP); socketHandler.Listen(0x3e8); while (!AcceptConnections) { socketHandler.BeginAccept(new AsyncCallback(gameSocketServer.InncommingDataRequest), socketHandler); } } catch (Exception exception) { Out.WriteError("Fatal error during socket setup:\r" + exception.ToString()); flag = true; Eucalypt.Shutdown(); } finally { if (!flag) { socketHandler.BeginAccept(new AsyncCallback(gameSocketServer.InncommingDataRequest), socketHandler); Out.WriteLine("Socket is ready"); } } } internal static int acceptedConnections { get { return _acceptedConnections; } } internal static int maxConnections { get { return _maxConnections; } set { _maxConnections = value; } } } }