Merge pull request #9731 from ZehMatt/fix-9666

Fix #9666: pause server if no clients are connected
This commit is contained in:
ζeh Matt 2019-08-08 01:31:43 +02:00 committed by GitHub
commit 12854d8743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 43 deletions

View File

@ -105,25 +105,35 @@ void GameState::Update()
// We use this variable to always advance ticks in normal speed. // We use this variable to always advance ticks in normal speed.
gCurrentRealTimeTicks += realtimeTicksElapsed; gCurrentRealTimeTicks += realtimeTicksElapsed;
// Determine how many times we need to update the game network_update();
if (gGameSpeed > 1)
{
// Update more often if game speed is above normal.
numUpdates = 1 << (gGameSpeed - 1);
}
if (network_get_mode() == NETWORK_MODE_CLIENT && network_get_status() == NETWORK_STATUS_CONNECTED if (network_get_mode() == NETWORK_MODE_CLIENT && network_get_status() == NETWORK_STATUS_CONNECTED
&& network_get_authstatus() == NETWORK_AUTH_OK) && network_get_authstatus() == NETWORK_AUTH_OK)
{ {
if (network_get_server_tick() - gCurrentTicks >= 10) numUpdates = std::clamp<uint32_t>(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 // Update more often if game speed is above normal.
numUpdates += 10; 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; bool didRunSingleFrame = false;
if (game_is_paused()) if (isPaused)
{ {
if (gDoSingleUpdate && network_get_mode() == NETWORK_MODE_NONE) if (gDoSingleUpdate && network_get_mode() == NETWORK_MODE_NONE)
{ {
@ -139,8 +149,6 @@ void GameState::Update()
map_animation_invalidate_all(); map_animation_invalidate_all();
// Special case because we set numUpdates to 0, otherwise in game_logic_update. // Special case because we set numUpdates to 0, otherwise in game_logic_update.
network_update();
network_process_pending(); network_process_pending();
} }
} }
@ -221,20 +229,8 @@ void GameState::UpdateLogic()
if (gScreenAge == 0) if (gScreenAge == 0)
gScreenAge--; gScreenAge--;
network_update();
GetContext()->GetReplayManager()->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_get_mode() == NETWORK_MODE_SERVER)
{ {
if (network_gamestate_snapshots_enabled()) if (network_gamestate_snapshots_enabled())

View File

@ -50,7 +50,6 @@ static constexpr uint32_t CHUNK_SIZE = 1024 * 63;
# include "../ParkImporter.h" # include "../ParkImporter.h"
# include "../Version.h" # include "../Version.h"
# include "../actions/GameAction.h" # include "../actions/GameAction.h"
# include "../actions/PauseToggleAction.hpp"
# include "../config/Config.h" # include "../config/Config.h"
# include "../core/Console.hpp" # include "../core/Console.hpp"
# include "../core/FileStream.hpp" # include "../core/FileStream.hpp"
@ -662,12 +661,6 @@ bool Network::BeginServer(uint16_t port, const std::string& address)
_serverState.gamestateSnapshotsEnabled = gConfigNetwork.desync_debugging; _serverState.gamestateSnapshotsEnabled = gConfigNetwork.desync_debugging;
_advertiser = CreateServerAdvertiser(listening_port); _advertiser = CreateServerAdvertiser(listening_port);
if (gConfigNetwork.pause_server_if_no_clients)
{
auto pauseToggleAction = PauseToggleAction();
GameActions::Execute(&pauseToggleAction);
}
return true; return true;
} }
@ -2043,12 +2036,6 @@ void Network::ProcessDisconnectedClients()
it++; 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() void Network::ProcessGameCommands()
@ -2123,12 +2110,6 @@ void Network::EnqueueGameAction(const GameAction* action)
void Network::AddClient(std::unique_ptr<ITcpSocket>&& socket) void Network::AddClient(std::unique_ptr<ITcpSocket>&& socket)
{ {
if (gConfigNetwork.pause_server_if_no_clients && game_is_paused())
{
auto pauseToggleAction = PauseToggleAction();
GameActions::Execute(&pauseToggleAction);
}
// Log connection info. // Log connection info.
char addr[128]; char addr[128];
snprintf(addr, sizeof(addr), "Client joined from %s", socket->GetHostName()); snprintf(addr, sizeof(addr), "Client joined from %s", socket->GetHostName());