Exemplo n.º 1
0
        /// <summary>
        /// Save the server to the database.  The server is assumed to be
        /// locked.
        /// </summary>
        /// <returns>True if the save was successful.</returns>
        public bool Save()
        {
            if (MasterServer.ConnectionString == null)
            {
                return(false);
            }

            try
            {
                if (ModuleName == null)
                {
                    ModuleName = "";
                }

                if (ServerName == null)
                {
                    ServerName = "";
                }

                if (ModuleDescription == null)
                {
                    ModuleDescription = "";
                }

                if (ModuleUrl == null)
                {
                    ModuleUrl = "";
                }

                if (PWCUrl == null)
                {
                    PWCUrl = "";
                }

                if (ServerDescription == null)
                {
                    ServerDescription = "";
                }

                string Query = String.Format(
                    @"INSERT INTO `game_servers` (
    `game_server_id`,
    `product_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`)
VALUES (
    {0},
    {1},
    {2},
    {3},
    '{4}',
    '{5}',
    {6},
    {7},
    {8},
    '{9}',
    '{10}',
    {11},
    {12},
    '{13}',
    '{14}',
    {15},
    {16},
    {17},
    {18},
    {19},
    {20},
    {21},
    {22},
    '{23}',
    '{24}')
ON DUPLICATE KEY UPDATE 
    `expansions_mask` = {2},
    `build_number` = {3},
    `module_name` = '{4}',
    `server_name` = '{5}',
    `active_player_count` = {6},
    `maximum_player_count` = {7},
    `local_vault` = {8},
    `last_heartbeat` = '{9}',
    `server_address` = '{10}',
    `online` = {11},
    `private_server` = {12},
    `module_description` = '{13}',
    `module_url` = '{14}',
    `game_type` = {15},
    `minimum_level` = {16},
    `maximum_level` = {17},
    `pvp_level` = {18},
    `player_pause` = {19},
    `one_party_only` = {20},
    `elc_enforced` = {21},
    `ilr_enforced` = {22},
    `pwc_url` = '{23}',
    `server_description` = '{24}'",
                    DatabaseId,
                    MasterServer.ProductID,
                    ExpansionsMask,
                    BuildNumber,
                    MySqlHelper.EscapeString(ModuleName.Length > 32 ? ModuleName.Substring(0, 32) : ModuleName),
                    MySqlHelper.EscapeString(ServerName.Length > 256 ? ServerName.Substring(0, 256) : ServerName),
                    ActivePlayerCount,
                    MaximumPlayerCount,
                    LocalVault,
                    MasterServer.DateToSQLDate(LastHeartbeat),
                    MySqlHelper.EscapeString(ServerAddress.ToString()),
                    Online,
                    PrivateServer,
                    MySqlHelper.EscapeString(ModuleDescription.Length > 256 ? ModuleDescription.Substring(0, 256) : ModuleDescription),
                    MySqlHelper.EscapeString(ModuleUrl.Length > 256 ? ModuleUrl.Substring(0, 256) : ModuleUrl),
                    GameType,
                    MinimumLevel,
                    MaximumLevel,
                    PVPLevel,
                    PlayerPause,
                    OnePartyOnly,
                    ELCEnforced,
                    ILREnforced,
                    MySqlHelper.EscapeString(PWCUrl.Length > 256 ? PWCUrl.Substring(0, 256) : PWCUrl),
                    MySqlHelper.EscapeString(ServerDescription.Length > 256 ? ServerDescription.Substring(0, 256) : ServerDescription)
                    );

                MasterServer.ExecuteQueryNoReaderCombine(Query);

                LastSaveTick = (uint)Environment.TickCount;
            }
            catch (Exception e)
            {
                Logger.Log(LogLevel.Error, "NWGameServer.Save(): Failed to save server {0}: Exception: {1}",
                           ServerAddress,
                           e);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This timer callback runs when the scavenge sweep timer elapses.
        /// Its purpose is to cycle through offline game_servers records in the
        /// database, which have had a heartbeat more recent than
        /// ScavengerTimeSpan since the current time.  Such servers are then
        /// re-pinged in a low frequency interval so as to allow servers that
        /// had gone offline for a long period of time (but had been returned
        /// to service afterwards) to be automatically re-listed eventually.
        /// </summary>
        /// <param name="sender">Unused.</param>
        /// <param name="e">Unused.</param>
        private void ScavengerSweepTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                bool     Processed = false;
                DateTime Now       = DateTime.UtcNow;
                string   Query     = String.Format(
                    @"SELECT `game_server_id`,
    `server_address` 
FROM `game_servers` 
WHERE `product_id` = {0} 
AND `game_server_id` > {1}
AND `online` = false 
AND `last_heartbeat` >= '{2}' 
GROUP BY `game_server_id` 
ORDER BY `game_server_id` 
LIMIT 50",
                    MasterServer.ProductID,
                    ScavengeServerId,
                    MasterServer.DateToSQLDate(Now.Subtract(ScavengerTimeSpan)));

                //
                // Examine each server record returned and, if the server is
                // still offline, enqueue a ping to it.  Advance the iterator
                // to the next server as each server is processed.
                //

                using (MySqlDataReader Reader = MasterServer.ExecuteQuery(Query))
                {
                    while (Reader.Read())
                    {
                        uint       ServerId      = Reader.GetUInt32(0);
                        IPEndPoint ServerAddress = ConvertServerHostnameToIPEndPoint(Reader.GetString(1));

                        if (ServerId > ScavengeServerId)
                        {
                            ScavengeServerId = ServerId;
                        }

                        Processed = true;

                        if (ServerAddress == null)
                        {
                            continue;
                        }

                        NWGameServer Server = LookupServerByAddress(ServerAddress, false);

                        if (Server == null || Server.Online == false)
                        {
                            MasterServer.SendServerInfoRequest(ServerAddress);
                        }
                    }
                }

                //
                // If no servers were processed, then the end of the iterator
                // list must have been reached.  Re-start the sweep cycle at
                // the beginning next time.
                //

                if (Processed == false)
                {
                    ScavengeServerId = 0;
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Error, "NWServerTracker.ScavengeSweepTimer_Elapsed(): Excepton processing scavenge server list: {0}", ex);
            }

            lock (HeartbeatLock)
            {
                if (!HeartbeatsEnabled)
                {
                    return;
                }

                ScavengerSweepTimer.Start();
            }
        }