/// <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> /// 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(); }