From 01de097ef0cb331594c3a2b3dbbae2a6679f4e38 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 6 Aug 2019 00:28:45 +0200 Subject: [PATCH 1/2] Fix #9666: pause server if no clients are connected --- src/openrct2/GameState.cpp | 44 +++++++++++++++----------------- src/openrct2/network/Network.cpp | 19 -------------- 2 files changed, 20 insertions(+), 43 deletions(-) diff --git a/src/openrct2/GameState.cpp b/src/openrct2/GameState.cpp index 2bd343b31e..a96f3eee41 100644 --- a/src/openrct2/GameState.cpp +++ b/src/openrct2/GameState.cpp @@ -105,25 +105,35 @@ void GameState::Update() // We use this variable to always advance ticks in normal speed. gCurrentRealTimeTicks += realtimeTicksElapsed; - // Determine how many times we need to update the game - if (gGameSpeed > 1) - { - // Update more often if game speed is above normal. - numUpdates = 1 << (gGameSpeed - 1); - } + network_update(); if (network_get_mode() == NETWORK_MODE_CLIENT && network_get_status() == NETWORK_STATUS_CONNECTED && network_get_authstatus() == NETWORK_AUTH_OK) { - if (network_get_server_tick() - gCurrentTicks >= 10) + numUpdates = std::clamp(network_get_server_tick() - gCurrentTicks, 0, 10); + } + else + { + // Determine how many times we need to update the game + if (gGameSpeed > 1) { - // Make sure client doesn't fall behind the server too much - numUpdates += 10; + // Update more often if game speed is above normal. + numUpdates = 1 << (gGameSpeed - 1); + } + } + + bool isPaused = game_is_paused(); + if (network_get_mode() == NETWORK_MODE_SERVER && gConfigNetwork.pause_server_if_no_clients) + { + // If we are headless we always have 1 player (host), pause if no one else is around. + if (gOpenRCT2Headless && network_get_num_players() == 1) + { + isPaused |= true; } } bool didRunSingleFrame = false; - if (game_is_paused()) + if (isPaused) { if (gDoSingleUpdate && network_get_mode() == NETWORK_MODE_NONE) { @@ -139,8 +149,6 @@ void GameState::Update() map_animation_invalidate_all(); // Special case because we set numUpdates to 0, otherwise in game_logic_update. - network_update(); - network_process_pending(); } } @@ -221,20 +229,8 @@ void GameState::UpdateLogic() if (gScreenAge == 0) gScreenAge--; - network_update(); - GetContext()->GetReplayManager()->Update(); - if (network_get_mode() == NETWORK_MODE_CLIENT && network_get_status() == NETWORK_STATUS_CONNECTED - && network_get_authstatus() == NETWORK_AUTH_OK) - { - // Don't run ahead of the server but can be on same tick. - if (gCurrentTicks > network_get_server_tick()) - { - return; - } - } - if (network_get_mode() == NETWORK_MODE_SERVER) { if (network_gamestate_snapshots_enabled()) diff --git a/src/openrct2/network/Network.cpp b/src/openrct2/network/Network.cpp index 1f6dfa359f..a8abd27ea2 100644 --- a/src/openrct2/network/Network.cpp +++ b/src/openrct2/network/Network.cpp @@ -50,7 +50,6 @@ static constexpr uint32_t CHUNK_SIZE = 1024 * 63; # include "../ParkImporter.h" # include "../Version.h" # include "../actions/GameAction.h" -# include "../actions/PauseToggleAction.hpp" # include "../config/Config.h" # include "../core/Console.hpp" # include "../core/FileStream.hpp" @@ -662,12 +661,6 @@ bool Network::BeginServer(uint16_t port, const std::string& address) _serverState.gamestateSnapshotsEnabled = gConfigNetwork.desync_debugging; _advertiser = CreateServerAdvertiser(listening_port); - if (gConfigNetwork.pause_server_if_no_clients) - { - auto pauseToggleAction = PauseToggleAction(); - GameActions::Execute(&pauseToggleAction); - } - return true; } @@ -2043,12 +2036,6 @@ void Network::ProcessDisconnectedClients() it++; } } - - if (gConfigNetwork.pause_server_if_no_clients && game_is_not_paused() && client_connection_list.empty()) - { - auto pauseToggleAction = PauseToggleAction(); - GameActions::Execute(&pauseToggleAction); - } } void Network::ProcessGameCommands() @@ -2123,12 +2110,6 @@ void Network::EnqueueGameAction(const GameAction* action) void Network::AddClient(std::unique_ptr&& socket) { - if (gConfigNetwork.pause_server_if_no_clients && game_is_paused()) - { - auto pauseToggleAction = PauseToggleAction(); - GameActions::Execute(&pauseToggleAction); - } - // Log connection info. char addr[128]; snprintf(addr, sizeof(addr), "Client joined from %s", socket->GetHostName()); From 8670385505ce127993f663ff95d7213e71c134a4 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 6 Aug 2019 00:29:15 +0200 Subject: [PATCH 2/2] Bump up network version --- src/openrct2/network/Network.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2/network/Network.cpp b/src/openrct2/network/Network.cpp index a8abd27ea2..55e6edeaba 100644 --- a/src/openrct2/network/Network.cpp +++ b/src/openrct2/network/Network.cpp @@ -34,7 +34,7 @@ // This string specifies which version of network stream current build uses. // It is used for making sure only compatible builds get connected, even within // single OpenRCT2 version. -#define NETWORK_STREAM_VERSION "2" +#define NETWORK_STREAM_VERSION "3" #define NETWORK_STREAM_ID OPENRCT2_VERSION "-" NETWORK_STREAM_VERSION static Peep* _pickup_peep = nullptr;