/// <summary> /// Initializes a new instance of the <see cref="Server"/> class. /// </summary> /// <exception cref="NotSupportedException">NetGore does not support systems that are not in Little Endian mode!</exception> public Server() { ThreadAsserts.IsMainThread(); // Check for some system settings if (!BitConverter.IsLittleEndian) { const string errmsg = "NetGore does not support systems that are not in Little Endian mode!"; log.Fatal(errmsg); throw new NotSupportedException(errmsg); } // Check if patching is needed int numMissingPatches = -1; try { numMissingPatches = ServerDbPatcher.GetMissingPatches().Length; } catch (Exception ex) { log.WarnFormat("Failed to find DbPatches directory, so could not check if patching is required. Exception: {0}", ex); } if (numMissingPatches > 0) { log.ErrorFormat("There are `{0}` NetGore db patches not applied. Please backup your database then run /DemoGame.Server/DbPatches/Patch.bat.", numMissingPatches); log.ErrorFormat("Server will auto-close after 10 seconds, and will keep doing this until you patch."); Thread.Sleep(10 * 1000); return; } // Initialize the engine settings EngineSettingsInitializer.Initialize(); // Create the DbController _dbController = CreateDbController(); // Add the query stats tracker var queryStats = new BasicQueryStatsTracker { LogFilePath = ContentPaths.Build.Root.Join("querystats.txt") }; queryStats.LogFileFrequency = 1000 * 5; _dbController.ConnectionPool.QueryStats = queryStats; // Validate the database DbTableValidator.ValidateTables(_dbController); ValidateDbControllerQueryAttributes(); // Clean-up var cleaner = new ServerCleaner(this); cleaner.Run(); // Create some objects _consoleCommands = new ConsoleCommands(this); _groupManager = new GroupManager((gm, x) => new Group(x)); _userAccountManager = new UserAccountManager(DbController); _world = new World(this); _sockets = new ServerSockets(this); WorldStatsTracker.Instance.NetPeerToTrack = _sockets.GetNetServer(); // Check for the password salt if (string.IsNullOrEmpty(ServerSettings.Default.PasswordSalt)) { const string errmsg = "No password salt has been defined in the server settings file. Make sure you define one before releasing."; if (log.IsWarnEnabled) { log.WarnFormat(errmsg); } } // Set the thread priority SetThreadPriority(ServerSettings.Default.ThreadPriority); // Validate the server's settings var ssv = new ServerSettingsValidator(); ssv.Check(this); if (log.IsInfoEnabled) { log.Info("Server loaded."); } }
/// <summary> /// Main game loop for the server. /// </summary> void GameLoop() { ThreadAsserts.IsMainThread(); var updateServerTimeQuery = DbController.GetQuery <UpdateServerTimeQuery>(); var serverTimeUpdater = new ServerTimeUpdater(updateServerTimeQuery); // Set the initial auto-save time _nextServerSaveTime = GetTime() + ServerSettings.Default.RoutineServerSaveRate; var worldStatsTracker = WorldStatsTracker.Instance; while (IsRunning) { // Store the loop start time so we can calculate how long the loop took var loopStartTime = GetTime(); // Update the networking ServerSockets.Heartbeat(); // Update the world _world.Update(); // Update the time serverTimeUpdater.Update(GetTime()); // Handle the queued console commands ProcessConsoleCommands(); // Check if it is time to save the world if (_nextServerSaveTime < loopStartTime) { ServerSave(); } // Update the world stats worldStatsTracker.Update(); // Check if we can afford sleeping the thread var updateElapsedTime = (long)GetTime() - loopStartTime; var sleepTime = ServerSettings.Default.ServerUpdateRate - updateElapsedTime; if (sleepTime > 0) { Thread.Sleep((int)sleepTime); } ++_tick; } // Once the thread reaches this point, it means it is closing since the main loop has stopped // Update the world stats and events one last time before the server closes worldStatsTracker.Update(); EventCounterManager.FlushAll(); // Dispose if (ServerSockets != null) { ServerSockets.Shutdown(); } if (World != null) { World.Dispose(); } if (DbController != null) { DbController.Dispose(); } }
/// <summary> /// Initializes a new instance of the <see cref="Server"/> class. /// </summary> /// <exception cref="NotSupportedException">NetGore does not support systems that are not in Little Endian mode!</exception> public Server() { // Check for some system settings if (!BitConverter.IsLittleEndian) { const string errmsg = "NetGore does not support systems that are not in Little Endian mode!"; log.Fatal(errmsg); throw new NotSupportedException(errmsg); } // Initialize the engine settings EngineSettingsInitializer.Initialize(); // Create the DbController var settings = new DbConnectionSettings(); _dbController = settings.CreateDbControllerPromptEditWhenInvalid(x => new ServerDbController(x.GetMySqlConnectionString()), x => PromptEditDbSettingsFile(settings, x)); if (_dbController == null) return; // Add the query stats tracker var queryStats = new BasicQueryStatsTracker { LogFilePath = ContentPaths.Build.Root.Join("querystats.txt") }; queryStats.LogFileFrequency = 1000 * 5; _dbController.ConnectionPool.QueryStats = queryStats; // Validate the database DbTableValidator.ValidateTables(_dbController); ValidateDbControllerQueryAttributes(); // Clean-up var cleaner = new ServerCleaner(this); cleaner.Run(); // Load the game data and such InitializeScripts(); // Create some objects _consoleCommands = new ConsoleCommands(this); _groupManager = new GroupManager((gm, x) => new Group(x)); _userAccountManager = new UserAccountManager(DbController); _world = new World(this); _sockets = new ServerSockets(this); WorldStatsTracker.Instance.NetPeerToTrack = _sockets.GetNetServer(); // Check for the password salt if (string.IsNullOrEmpty(ServerSettings.Default.PasswordSalt)) { const string errmsg = "No password salt has been defined in the server settings file. Make sure you define one before releasing."; if (log.IsWarnEnabled) log.WarnFormat(errmsg); } // Set the thread priority SetThreadPriority(ServerSettings.Default.ThreadPriority); // Validate the server's settings var ssv = new ServerSettingsValidator(); ssv.Check(this); if (log.IsInfoEnabled) log.Info("Server loaded."); }
/// <summary> /// Initializes a new instance of the <see cref="Server"/> class. /// </summary> /// <exception cref="NotSupportedException">NetGore does not support systems that are not in Little Endian mode!</exception> public Server() { ThreadAsserts.IsMainThread(); // Check for some system settings if (!BitConverter.IsLittleEndian) { const string errmsg = "NetGore does not support systems that are not in Little Endian mode!"; log.Fatal(errmsg); throw new NotSupportedException(errmsg); } // Check if patching is needed int numMissingPatches = -1; try { numMissingPatches = ServerDbPatcher.GetMissingPatches().Length; } catch (Exception ex) { log.WarnFormat("Failed to find DbPatches directory, so could not check if patching is required. Exception: {0}", ex); } if (numMissingPatches > 0) { log.ErrorFormat("There are `{0}` NetGore db patches not applied. Please backup your database then run /DemoGame.Server/DbPatches/Patch.bat.", numMissingPatches); log.ErrorFormat("Server will auto-close after 10 seconds, and will keep doing this until you patch."); Thread.Sleep(10 * 1000); return; } // Initialize the engine settings EngineSettingsInitializer.Initialize(); // Create the DbController _dbController = CreateDbController(); // Add the query stats tracker var queryStats = new BasicQueryStatsTracker { LogFilePath = ContentPaths.Build.Root.Join("querystats.txt") }; queryStats.LogFileFrequency = 1000 * 5; _dbController.ConnectionPool.QueryStats = queryStats; // Validate the database DbTableValidator.ValidateTables(_dbController); ValidateDbControllerQueryAttributes(); // Clean-up var cleaner = new ServerCleaner(this); cleaner.Run(); // Create some objects _consoleCommands = new ConsoleCommands(this); _groupManager = new GroupManager((gm, x) => new Group(x)); _userAccountManager = new UserAccountManager(DbController); _world = new World(this); _sockets = new ServerSockets(this); WorldStatsTracker.Instance.NetPeerToTrack = _sockets.GetNetServer(); // Check for the password salt if (string.IsNullOrEmpty(ServerSettings.Default.PasswordSalt)) { const string errmsg = "No password salt has been defined in the server settings file. Make sure you define one before releasing."; if (log.IsWarnEnabled) log.WarnFormat(errmsg); } // Set the thread priority SetThreadPriority(ServerSettings.Default.ThreadPriority); // Validate the server's settings var ssv = new ServerSettingsValidator(); ssv.Check(this); if (log.IsInfoEnabled) log.Info("Server loaded."); }
/// <summary> /// Initializes a new instance of the <see cref="Server"/> class. /// </summary> /// <exception cref="NotSupportedException">NetGore does not support systems that are not in Little Endian mode!</exception> public Server() { // Check for some system settings if (!BitConverter.IsLittleEndian) { const string errmsg = "NetGore does not support systems that are not in Little Endian mode!"; log.Fatal(errmsg); throw new NotSupportedException(errmsg); } // Initialize the engine settings EngineSettingsInitializer.Initialize(); // Create the DbController var settings = new DbConnectionSettings(); _dbController = settings.CreateDbControllerPromptEditWhenInvalid(x => new ServerDbController(x.GetMySqlConnectionString()), x => PromptEditDbSettingsFile(settings, x)); if (_dbController == null) { return; } // Add the query stats tracker var queryStats = new BasicQueryStatsTracker { LogFilePath = ContentPaths.Build.Root.Join("querystats.txt") }; queryStats.LogFileFrequency = 1000 * 5; _dbController.ConnectionPool.QueryStats = queryStats; // Validate the database DbTableValidator.ValidateTables(_dbController); ValidateDbControllerQueryAttributes(); // Clean-up var cleaner = new ServerCleaner(this); cleaner.Run(); // Load the game data and such InitializeScripts(); // Create some objects _consoleCommands = new ConsoleCommands(this); _groupManager = new GroupManager((gm, x) => new Group(x)); _userAccountManager = new UserAccountManager(DbController); _world = new World(this); _sockets = new ServerSockets(this); WorldStatsTracker.Instance.NetPeerToTrack = _sockets.GetNetServer(); // Check for the password salt if (string.IsNullOrEmpty(ServerSettings.Default.PasswordSalt)) { const string errmsg = "No password salt has been defined in the server settings file. Make sure you define one before releasing."; if (log.IsWarnEnabled) { log.WarnFormat(errmsg); } } // Set the thread priority SetThreadPriority(ServerSettings.Default.ThreadPriority); // Validate the server's settings var ssv = new ServerSettingsValidator(); ssv.Check(this); if (log.IsInfoEnabled) { log.Info("Server loaded."); } }