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.
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<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
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())

View File

@ -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<ITcpSocket>&& 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());