/// <summary> /// Attempt to look up a game server by address, creating the server if /// requested (if it did not exist). /// </summary> /// <param name="ServerAddress">Supplies the server address to look up /// in the active server table.</param> /// <param name="Create">Supplies true if the server instance should be /// created if it did not exist.</param> /// <returns>The active server instance, else null if no server /// satisfied the given criteria.</returns> public NWGameServer LookupServerByAddress(IPEndPoint ServerAddress, bool Create = true) { NWGameServer Server; lock (ActiveServerTable) { if (!ActiveServerTable.TryGetValue(ServerAddress, out Server)) { Server = null; } if ((Create != false) && (Server == null)) { Server = new NWGameServer(MasterServer, ServerAddress); Server.Load(); ActiveServerTable.Add(ServerAddress, Server); } } return(Server); }
/// <summary> /// Prepare the database, creating tables (if they did not already /// exist). /// </summary> private void InitializeDatabase() { uint ServersAdded; if (String.IsNullOrEmpty(MasterServer.ConnectionString)) { return; } try { MasterServer.ExecuteQueryNoReader( @"CREATE TABLE IF NOT EXISTS `game_servers` ( `game_server_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `product_id` int(10) UNSIGNED NOT NULL, `expansions_mask` int(10) UNSIGNED NOT NULL, `build_number` int(10) UNSIGNED NOT NULL, `module_name` varchar(32) NOT NULL, `server_name` varchar(256) NOT NULL, `active_player_count` int(10) UNSIGNED NOT NULL, `maximum_player_count` int(10) UNSIGNED NOT NULL, `local_vault` bool NOT NULL, `last_heartbeat` datetime NOT NULL, `server_address` varchar(128) NOT NULL, `online` bool NOT NULL, `private_server` bool NOT NULL, `module_description` varchar(256) NOT NULL, `module_url` varchar(256) NOT NULL, `game_type` int(10) UNSIGNED NOT NULL, `minimum_level` int(10) UNSIGNED NOT NULL, `maximum_level` int(10) UNSIGNED NOT NULL, `pvp_level` int(10) UNSIGNED NOT NULL, `player_pause` bool NOT NULL, `one_party_only` bool NOT NULL, `elc_enforced` bool NOT NULL, `ilr_enforced` bool NOT NULL, `pwc_url` varchar(256) NOT NULL, `server_description` varchar(256) NOT NULL, `game_server_group_id` int(10) UNSIGNED, PRIMARY KEY (`game_server_id`), UNIQUE KEY (`product_id`, `server_address`), INDEX (`product_id`, `online`), INDEX (`product_id`, `online`, `server_name`), INDEX (`product_id`, `online`, `module_name`), INDEX (`product_id`, `online`, `game_type`), INDEX (`product_id`, `online`, `game_server_group_id`) )"); MasterServer.ExecuteQueryNoReader( @"CREATE TABLE IF NOT EXISTS `game_server_groups` ( `game_server_group_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `product_id` int(10) UNSIGNED NOT NULL, `group_name` varchar(128) NOT NULL, `global_user_counts` bool NOT NULL, PRIMARY KEY (`game_server_group_id`), UNIQUE KEY (`product_id`, `group_name`) )"); MasterServer.ExecuteQueryNoReader( @"CREATE TABLE IF NOT EXISTS `pending_game_servers` ( `pending_game_server_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `product_id` int(10) UNSIGNED NOT NULL, `server_address` varchar(128) NOT NULL, PRIMARY KEY (`pending_game_server_id`), UNIQUE KEY (`product_id`, `server_address`) )"); MasterServer.ExecuteQueryNoReader( @"CREATE TABLE IF NOT EXISTS `client_extension_update` ( `update_id` int(10) UNSIGNED NOT NULL, `product_id` int(10) UNSIGNED NOT NULL, `update_message` varchar(4096) NOT NULL, `update_url` varchar(4096) NOT NULL, `update_info_url` varchar(4096) NOT NULL, `update_version` varchar(128) NOT NULL, `update_motd` varchar(4096) NOT NULL, PRIMARY KEY (`update_id`) )"); MasterServer.ExecuteQueryNoReader( @"CREATE TABLE IF NOT EXISTS `stat_counters` ( `stat_counter_name` varchar(64) NOT NULL, `stat_counter_value` int(10) UNSIGNED NOT NULL, `stat_counter_last_update` datetime NOT NULL, PRIMARY KEY (`stat_counter_name`) )"); MasterServer.ExecuteQueryNoReader( @"CREATE TABLE IF NOT EXISTS `blacklist_entries` ( `blacklist_entry_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `product_id` int(10) UNSIGNED NOT NULL, `blacklist_entry_type` int(10) UNSIGNED NOT NULL, `blacklist_entry_match` varchar(128) NOT NULL, PRIMARY KEY (`blacklist_entry_id`) )"); string Query = String.Format( @"SELECT `game_server_id`, `expansions_mask`, `build_number`, `module_name`, `server_name`, `active_player_count`, `maximum_player_count`, `local_vault`, `last_heartbeat`, `server_address`, `online`, `private_server`, `module_description`, `module_url`, `game_type`, `minimum_level`, `maximum_level`, `pvp_level`, `player_pause`, `one_party_only`, `elc_enforced`, `ilr_enforced`, `pwc_url`, `server_description` FROM `game_servers` WHERE `product_id` = {0} AND `online` = true", MasterServer.ProductID); RefreshBlacklist(); ServersAdded = 0; using (MySqlDataReader Reader = MasterServer.ExecuteQuery(Query)) { while (Reader.Read()) { uint ServerId = Reader.GetUInt32(0); string Hostname = Reader.GetString(9); int i = Hostname.IndexOf(':'); IPEndPoint ServerAddress; if (i == -1) { Logger.Log(LogLevel.Error, "NWServerTracker.InitializeDatabase(): Server {0} has invalid hostname '{1}'.", ServerId, Hostname); continue; } try { ServerAddress = new IPEndPoint( IPAddress.Parse(Hostname.Substring(0, i)), Convert.ToInt32(Hostname.Substring(i + 1))); } catch (Exception e) { Logger.Log(LogLevel.Error, "NWServerTracker.InitializeDatabase(): Error initializing hostname {0} for server {1}: Exception: {2}", Hostname, ServerId, e); continue; } // // Query for whether the server is blacklisted and do // not re-list it if so. // BlacklistLookup Lookup = new BlacklistLookup(); Lookup.ServerAddress = ServerAddress; Lookup.ModuleName = Reader.GetString(3); if (IsBlacklisted(Lookup)) { continue; } NWGameServer Server = new NWGameServer(MasterServer, ServerAddress); Server.DatabaseId = ServerId; Server.ExpansionsMask = (Byte)Reader.GetUInt32(1); Server.BuildNumber = (UInt16)Reader.GetUInt32(2); Server.ModuleName = Reader.GetString(3); Server.ServerName = Reader.GetString(4); Server.ActivePlayerCount = Reader.GetUInt32(5); Server.MaximumPlayerCount = Reader.GetUInt32(6); Server.LocalVault = Reader.GetBoolean(7); Server.LastHeartbeat = Reader.GetDateTime(8); Server.Online = Reader.GetBoolean(10); Server.PrivateServer = Reader.GetBoolean(11); Server.ModuleDescription = Reader.GetString(12); Server.ModuleUrl = Reader.GetString(13); Server.GameType = Reader.GetUInt32(14); Server.MinimumLevel = Reader.GetUInt32(15); Server.MaximumLevel = Reader.GetUInt32(16); Server.PVPLevel = Reader.GetUInt32(17); Server.PlayerPause = Reader.GetBoolean(18); Server.OnePartyOnly = Reader.GetBoolean(19); Server.ELCEnforced = Reader.GetBoolean(20); Server.ILREnforced = Reader.GetBoolean(21); Server.PWCUrl = Reader.GetString(22); Server.ServerDescription = Reader.GetString(23); lock (ActiveServerTable) { ActiveServerTable.Add(ServerAddress, Server); } ServersAdded += 1; } } Logger.Log(LogLevel.Normal, "NWServerTracker.InitializeDatabase(): Read {0} active servers from database.", ServersAdded); } catch (Exception e) { Logger.Log(LogLevel.Error, "NWServerTracker.InitializeDatabase(): Exception: {0}", e); MasterServer.Stop(); } }