Codechange: [Network] replace _realtime_tick with std::chrono

This commit is contained in:
Patric Stout
2021-02-25 20:30:16 +01:00
committed by Patric Stout
parent dc7ba33b51
commit 53c28a8ec9
10 changed files with 39 additions and 49 deletions

View File

@@ -877,7 +877,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
bool load_success = SafeLoad({}, SLO_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, lf);
/* Long savegame loads shouldn't affect the lag calculation! */
this->last_packet = _realtime_tick;
this->last_packet = std::chrono::steady_clock::now();
if (!load_success) {
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
@@ -1178,27 +1178,23 @@ void ClientNetworkGameSocketHandler::CheckConnection()
/* Only once we're authorized we can expect a steady stream of packets. */
if (this->status < STATUS_AUTHORIZED) return;
/* It might... sometimes occur that the realtime ticker overflows. */
if (_realtime_tick < this->last_packet) this->last_packet = _realtime_tick;
/* Lag is in milliseconds; 5 seconds are roughly twice the
* server's "you're slow" threshold (1 game day). */
uint lag = (_realtime_tick - this->last_packet) / 1000;
if (lag < 5) return;
/* 5 seconds are roughly twice the server's "you're slow" threshold (1 game day). */
std::chrono::steady_clock::duration lag = std::chrono::steady_clock::now() - this->last_packet;
if (lag < std::chrono::seconds(5)) return;
/* 20 seconds are (way) more than 4 game days after which
* the server will forcefully disconnect you. */
if (lag > 20) {
if (lag > std::chrono::seconds(20)) {
this->NetworkGameSocketHandler::CloseConnection();
return;
}
/* Prevent showing the lag message every tick; just update it when needed. */
static uint last_lag = 0;
if (last_lag == lag) return;
static std::chrono::steady_clock::duration last_lag = {};
if (std::chrono::duration_cast<std::chrono::seconds>(last_lag) == std::chrono::duration_cast<std::chrono::seconds>(lag)) return;
last_lag = lag;
SetDParam(0, lag);
SetDParam(0, std::chrono::duration_cast<std::chrono::seconds>(lag).count());
ShowErrorMessage(STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION_CAPTION, STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION, WL_INFO);
}