From ebf95ee95882836bcfd76a4966e403f6ce16171d Mon Sep 17 00:00:00 2001 From: Benau Date: Mon, 6 May 2019 15:17:19 +0800 Subject: [PATCH] Use monotonic clock to avoid system time changes --- src/audio/sfx_manager.cpp | 8 ++--- src/input/multitouch_device.cpp | 2 +- src/main_loop.cpp | 14 ++++---- src/network/network_timer_synchronizer.hpp | 2 +- src/network/protocols/client_lobby.cpp | 4 +-- src/network/protocols/connect_to_peer.cpp | 4 +-- src/network/protocols/connect_to_server.cpp | 4 +-- src/network/protocols/lobby_protocol.cpp | 8 ++--- src/network/protocols/server_lobby.cpp | 24 ++++++------- src/network/protocols/server_lobby.hpp | 4 +-- src/network/servers_manager.cpp | 10 +++--- src/network/stk_host.cpp | 34 +++++++++---------- src/network/stk_host.hpp | 4 +-- src/network/stk_peer.cpp | 4 +-- src/network/stk_peer.hpp | 6 ++-- .../dialogs/player_rankings_dialog.cpp | 6 ++-- .../online/network_kart_selection.cpp | 4 +-- .../online/networking_lobby.cpp | 10 +++--- src/states_screens/online/tracks_screen.cpp | 2 +- src/tracks/check_trigger.cpp | 6 ++-- src/tracks/track_object_presentation.cpp | 2 +- src/tracks/track_object_presentation.hpp | 8 ++--- src/utils/time.cpp | 6 ++-- src/utils/time.hpp | 13 ++++--- 24 files changed, 95 insertions(+), 94 deletions(-) diff --git a/src/audio/sfx_manager.cpp b/src/audio/sfx_manager.cpp index 045941d2c..339aebfa8 100644 --- a/src/audio/sfx_manager.cpp +++ b/src/audio/sfx_manager.cpp @@ -478,9 +478,9 @@ void* SFXManager::mainLoop(void *obj) { // Wait some time to let other threads run, then queue an // update event to keep music playing. - uint64_t t = StkTime::getRealTimeMs(); + uint64_t t = StkTime::getMonoTimeMs(); StkTime::sleep(1); - t = StkTime::getRealTimeMs() - t; + t = StkTime::getMonoTimeMs() - t; me->queue(SFX_UPDATE, (SFXBase*)NULL, float(t / 1000.0)); } me->m_sfx_commands.lock(); @@ -838,11 +838,11 @@ void SFXManager::reallyUpdateNow(SFXCommand *current) if (m_last_update_time == std::numeric_limits::max()) { // first time - m_last_update_time = StkTime::getRealTimeMs(); + m_last_update_time = StkTime::getMonoTimeMs(); } uint64_t previous_update_time = m_last_update_time; - m_last_update_time = StkTime::getRealTimeMs(); + m_last_update_time = StkTime::getMonoTimeMs(); float dt = float(m_last_update_time - previous_update_time) / 1000.0f; assert(current->m_command==SFX_UPDATE); diff --git a/src/input/multitouch_device.cpp b/src/input/multitouch_device.cpp index 6152e2007..5bb38f881 100644 --- a/src/input/multitouch_device.cpp +++ b/src/input/multitouch_device.cpp @@ -502,7 +502,7 @@ void MultitouchDevice::updateOrientationFromGyroscope(float z) { const float GYRO_SPEED_THRESHOLD = 0.005f; - uint64_t now = StkTime::getRealTimeMs(); + uint64_t now = StkTime::getMonoTimeMs(); uint64_t delta = now - m_gyro_time; m_gyro_time = now; float timedelta = (float)delta / 1000.f; diff --git a/src/main_loop.cpp b/src/main_loop.cpp index 5b8d55bd6..bf3931078 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -118,7 +118,7 @@ float MainLoop::getLimitedDt() while( 1 ) { - m_curr_time = StkTime::getRealTimeMs(); + m_curr_time = StkTime::getMonoTimeMs(); if (m_prev_time > m_curr_time) { m_prev_time = m_curr_time; @@ -157,7 +157,7 @@ float MainLoop::getLimitedDt() while (dt == 0) { StkTime::sleep(1); - m_curr_time = StkTime::getRealTimeMs(); + m_curr_time = StkTime::getMonoTimeMs(); if (m_prev_time > m_curr_time) { Log::error("MainLopp", "System clock keeps backwards!"); @@ -313,7 +313,7 @@ void MainLoop::updateRace(int ticks, bool fast_forward) */ void MainLoop::run() { - m_curr_time = StkTime::getRealTimeMs(); + m_curr_time = StkTime::getMonoTimeMs(); // DT keeps track of the leftover time, since the race update // happens in fixed timesteps float left_over_time = 0; @@ -525,7 +525,7 @@ void MainLoop::run() // in CutsceneWorld::enterRaceOverState // Reset the timer for correct time for cutscene m_frame_before_loading_world = false; - m_curr_time = StkTime::getRealTimeMs(); + m_curr_time = StkTime::getMonoTimeMs(); left_over_time = 0.0f; break; } @@ -553,7 +553,7 @@ void MainLoop::run() { // irr_driver->getDevice()->run() loads the world m_frame_before_loading_world = false; - m_curr_time = StkTime::getRealTimeMs(); + m_curr_time = StkTime::getMonoTimeMs(); left_over_time = 0.0f; } @@ -608,7 +608,7 @@ void MainLoop::renderGUI(int phase, int loop_index, int loop_size) return; } - uint64_t now = StkTime::getRealTimeMs(); + uint64_t now = StkTime::getMonoTimeMs(); float dt = (now - m_curr_time)/1000.0f; if (dt < 1.0 / 30.0f) return; @@ -624,7 +624,7 @@ void MainLoop::renderGUI(int phase, int loop_index, int loop_size) m_request_abort = !irr_driver->getDevice()->run(); //TODO: remove debug output - // uint64_t now2 = StkTime::getRealTimeMs(); + // uint64_t now2 = StkTime::getMonoTimeMs(); // Log::verbose("mainloop", " duration t %llu dt %llu", now, now2-now); #endif } // renderGUI diff --git a/src/network/network_timer_synchronizer.hpp b/src/network/network_timer_synchronizer.hpp index a8b3874d2..1a41153b5 100644 --- a/src/network/network_timer_synchronizer.hpp +++ b/src/network/network_timer_synchronizer.hpp @@ -68,7 +68,7 @@ public: return; } - const uint64_t cur_time = StkTime::getRealTimeMs(); + const uint64_t cur_time = StkTime::getMonoTimeMs(); // Discard too close time compared to last ping // (due to resend when packet loss) // 10 packets per second as seen in STKHost diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index 4ac2079e8..f447f8494 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -422,7 +422,7 @@ void ClientLobby::update(int ticks) if (!m_received_server_result) { m_received_server_result = true; - m_auto_back_to_lobby_time = StkTime::getRealTimeMs() + 5000; + m_auto_back_to_lobby_time = StkTime::getMonoTimeMs() + 5000; // In case someone opened paused race dialog or menu in network game GUIEngine::ModalDialog::dismiss(); GUIEngine::ScreenKeyboard::dismiss(); @@ -431,7 +431,7 @@ void ClientLobby::update(int ticks) World::getWorld()->enterRaceOverState(); } if (NetworkConfig::get()->isAutoConnect() && - StkTime::getRealTimeMs() > m_auto_back_to_lobby_time) + StkTime::getMonoTimeMs() > m_auto_back_to_lobby_time) { m_auto_back_to_lobby_time = std::numeric_limits::max(); doneWithResults(); diff --git a/src/network/protocols/connect_to_peer.cpp b/src/network/protocols/connect_to_peer.cpp index 6bf27207b..d26be1fbc 100644 --- a/src/network/protocols/connect_to_peer.cpp +++ b/src/network/protocols/connect_to_peer.cpp @@ -55,9 +55,9 @@ void ConnectToPeer::asynchronousUpdate() break; } // Each 2 second for a ping or broadcast - if (StkTime::getRealTimeMs() > m_timer + 2000) + if (StkTime::getMonoTimeMs() > m_timer + 2000) { - m_timer = StkTime::getRealTimeMs(); + m_timer = StkTime::getMonoTimeMs(); // Send a broadcast packet with the string aloha_stk inside, // the client will use enet intercept to discover if server // address or port is different from stk addons database. diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index cddfdd045..a857d040c 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -90,14 +90,14 @@ void ConnectToServer::getClientServerInfo() assert(m_server); // Allow up to 10 seconds for the separate process to fully start-up bool started = false; - uint64_t timeout = StkTime::getRealTimeMs() + 10000; + uint64_t timeout = StkTime::getMonoTimeMs() + 10000; const std::string& sid = NetworkConfig::get()->getServerIdFile(); assert(!sid.empty()); const std::string dir = StringUtils::getPath(sid); const std::string server_id_file = StringUtils::getBasename(sid); uint16_t port = 0; unsigned server_id = 0; - while (StkTime::getRealTimeMs() < timeout) + while (StkTime::getMonoTimeMs() < timeout) { std::set files; file_manager->listFiles(files, dir); diff --git a/src/network/protocols/lobby_protocol.cpp b/src/network/protocols/lobby_protocol.cpp index 62b2ff231..55fd0ec3d 100644 --- a/src/network/protocols/lobby_protocol.cpp +++ b/src/network/protocols/lobby_protocol.cpp @@ -163,7 +163,7 @@ void LobbyProtocol::setup() void LobbyProtocol::startVotingPeriod(float max_time) { m_max_voting_time = uint64_t(max_time*1000); - m_end_voting_period.store(StkTime::getRealTimeMs() + m_max_voting_time); + m_end_voting_period.store(StkTime::getMonoTimeMs() + m_max_voting_time); } // startVotingPeriod //----------------------------------------------------------------------------- @@ -171,9 +171,9 @@ void LobbyProtocol::startVotingPeriod(float max_time) float LobbyProtocol::getRemainingVotingTime() { if (m_end_voting_period.load() == 0 || - StkTime::getRealTimeMs() >= m_end_voting_period.load()) + StkTime::getMonoTimeMs() >= m_end_voting_period.load()) return 0.0f; - uint64_t t = m_end_voting_period.load() - StkTime::getRealTimeMs(); + uint64_t t = m_end_voting_period.load() - StkTime::getMonoTimeMs(); return t / 1000.0f; } // getRemainingVotingTime @@ -182,7 +182,7 @@ float LobbyProtocol::getRemainingVotingTime() bool LobbyProtocol::isVotingOver() { return m_end_voting_period.load() != 0 && - m_end_voting_period.load() < StkTime::getRealTimeMs(); + m_end_voting_period.load() < StkTime::getMonoTimeMs(); } // isVotingOver //----------------------------------------------------------------------------- diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index f598d878d..6ab0aa329 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -124,7 +124,7 @@ ServerLobby::ServerLobby() : LobbyProtocol(NULL) } m_rs_state.store(RS_NONE); - m_last_success_poll_time.store(StkTime::getRealTimeMs() + 30000); + m_last_success_poll_time.store(StkTime::getMonoTimeMs() + 30000); m_server_owner_id.store(-1); m_registered_for_once_only = false; m_has_created_server_id_file = false; @@ -809,7 +809,7 @@ void ServerLobby::asynchronousUpdate() if (NetworkConfig::get()->isWAN() && allowJoinedPlayersWaiting() && m_server_recovering.expired() && - StkTime::getRealTimeMs() > m_last_success_poll_time.load() + 30000) + StkTime::getMonoTimeMs() > m_last_success_poll_time.load() + 30000) { Log::warn("ServerLobby", "Trying auto server recovery."); registerServer(false/*now*/); @@ -874,7 +874,7 @@ void ServerLobby::asynchronousUpdate() m_game_setup->isGrandPrixStarted()) && m_timeout.load() == std::numeric_limits::max()) { - m_timeout.store((int64_t)StkTime::getRealTimeMs() + + m_timeout.store((int64_t)StkTime::getMonoTimeMs() + (int64_t) (ServerConfig::m_start_game_counter * 1000.0f)); } @@ -886,7 +886,7 @@ void ServerLobby::asynchronousUpdate() updatePlayerList(); m_timeout.store(std::numeric_limits::max()); } - if (m_timeout.load() < (int64_t)StkTime::getRealTimeMs() || + if (m_timeout.load() < (int64_t)StkTime::getMonoTimeMs() || (checkPeersReady() && (int)players >= ServerConfig::m_min_start_game_players)) { @@ -1563,14 +1563,14 @@ void ServerLobby::update(int ticks) resetPeersReady(); // Set the delay before the server forces all clients to exit the race // result screen and go back to the lobby - m_timeout.store((int64_t)StkTime::getRealTimeMs() + 15000); + m_timeout.store((int64_t)StkTime::getMonoTimeMs() + 15000); m_state = RESULT_DISPLAY; sendMessageToPeers(m_result_ns, /*reliable*/ true); Log::info("ServerLobby", "End of game message sent"); break; case RESULT_DISPLAY: if (checkPeersReady() || - (int64_t)StkTime::getRealTimeMs() > m_timeout.load()) + (int64_t)StkTime::getMonoTimeMs() > m_timeout.load()) { // Send a notification to all clients to exit // the race result screen @@ -1635,7 +1635,7 @@ bool ServerLobby::registerServer(bool now) Log::info("ServerLobby", "Server %d is now online.", server_id_online); sl->m_server_id_online.store(server_id_online); - sl->m_last_success_poll_time.store(StkTime::getRealTimeMs()); + sl->m_last_success_poll_time.store(StkTime::getMonoTimeMs()); return; } Log::error("ServerLobby", "%s", @@ -1933,8 +1933,8 @@ void ServerLobby::checkIncomingConnectionRequests() // First poll every 5 seconds. Return if no polling needs to be done. const uint64_t POLL_INTERVAL = 5000; static uint64_t last_poll_time = 0; - if (StkTime::getRealTimeMs() < last_poll_time + POLL_INTERVAL || - StkTime::getRealTimeMs() > m_last_success_poll_time.load() + 30000 || + if (StkTime::getMonoTimeMs() < last_poll_time + POLL_INTERVAL || + StkTime::getMonoTimeMs() > m_last_success_poll_time.load() + 30000 || m_server_id_online.load() == 0) return; @@ -1948,7 +1948,7 @@ void ServerLobby::checkIncomingConnectionRequests() } // Now poll the stk server - last_poll_time = StkTime::getRealTimeMs(); + last_poll_time = StkTime::getMonoTimeMs(); // ======================================================================== class PollServerRequest : public Online::XMLRequest @@ -1975,7 +1975,7 @@ void ServerLobby::checkIncomingConnectionRequests() auto sl = m_server_lobby.lock(); if (!sl) return; - sl->m_last_success_poll_time.store(StkTime::getRealTimeMs()); + sl->m_last_success_poll_time.store(StkTime::getMonoTimeMs()); if (sl->m_state.load() != WAITING_FOR_START_GAME && !sl->allowJoinedPlayersWaiting()) { @@ -2758,7 +2758,7 @@ void ServerLobby::handleUnencryptedConnection(std::shared_ptr peer, else { auto_start_timer = - (m_timeout.load() - (int64_t)StkTime::getRealTimeMs()) / 1000.0f; + (m_timeout.load() - (int64_t)StkTime::getMonoTimeMs()) / 1000.0f; } message_ack->addUInt8(LE_CONNECTION_ACCEPTED).addUInt32(peer->getHostId()) .addUInt32(ServerConfig::m_server_version); diff --git a/src/network/protocols/server_lobby.hpp b/src/network/protocols/server_lobby.hpp index 345ed3b18..c99c8cd02 100644 --- a/src/network/protocols/server_lobby.hpp +++ b/src/network/protocols/server_lobby.hpp @@ -246,7 +246,7 @@ private: } void addPeerConnection(const std::string& addr_str) { - m_pending_peer_connection[addr_str] = StkTime::getRealTimeMs(); + m_pending_peer_connection[addr_str] = StkTime::getMonoTimeMs(); } void removeExpiredPeerConnection() { @@ -255,7 +255,7 @@ private: for (auto it = m_pending_peer_connection.begin(); it != m_pending_peer_connection.end();) { - if (StkTime::getRealTimeMs() - it->second > 45000) + if (StkTime::getMonoTimeMs() - it->second > 45000) it = m_pending_peer_connection.erase(it); else it++; diff --git a/src/network/servers_manager.cpp b/src/network/servers_manager.cpp index a87b5eb60..2e21e8648 100644 --- a/src/network/servers_manager.cpp +++ b/src/network/servers_manager.cpp @@ -157,7 +157,7 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const char buffer[LEN]; // Wait for up to 0.5 seconds to receive an answer from // any local servers. - uint64_t start_time = StkTime::getRealTimeMs(); + uint64_t start_time = StkTime::getMonoTimeMs(); const uint64_t DURATION = 1000; const auto& servers = ServersManager::get()->getServers(); int cur_server_id = (int)servers.size(); @@ -168,7 +168,7 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const // because e.g. a local client would answer as 127.0.0.1 and // 192.168.**. std::map > servers_now; - while (StkTime::getRealTimeMs() - start_time < DURATION) + while (StkTime::getMonoTimeMs() - start_time < DURATION) { TransportAddress sender; int len = broadcast->receiveRawPacket(buffer, LEN, &sender, 1); @@ -236,7 +236,7 @@ void ServersManager::setLanServers(const std::map(*s)); } - m_last_load_time.store(StkTime::getRealTimeMs()); + m_last_load_time.store(StkTime::getMonoTimeMs()); m_list_updated = true; } // refresh diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index cf30ab36b..e3055bd8c 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -297,7 +297,7 @@ void STKHost::init() m_players_in_game.store(0); m_players_waiting.store(0); m_total_players.store(0); - m_network_timer.store(StkTime::getRealTimeMs()); + m_network_timer.store(StkTime::getMonoTimeMs()); m_shutdown = false; m_authorised = false; m_network = NULL; @@ -435,7 +435,7 @@ void STKHost::setPublicAddress() } m_network->sendRawPacket(s, m_stun_address); - uint64_t ping = StkTime::getRealTimeMs(); + uint64_t ping = StkTime::getMonoTimeMs(); freeaddrinfo(res); // Recieve now @@ -443,7 +443,7 @@ void STKHost::setPublicAddress() const int LEN = 2048; char buffer[LEN]; int len = m_network->receiveRawPacket(buffer, LEN, &sender, 2000); - ping = StkTime::getRealTimeMs() - ping; + ping = StkTime::getMonoTimeMs() - ping; if (sender.getIP() != m_stun_address.getIP()) { @@ -614,7 +614,7 @@ void STKHost::disconnectAllPeers(bool timeout_waiting) for (auto peer : m_peers) peer.second->disconnect(); // Wait for at most 2 seconds for disconnect event to be generated - m_exit_timeout.store(StkTime::getRealTimeMs() + 2000); + m_exit_timeout.store(StkTime::getMonoTimeMs() + 2000); } m_peers.clear(); } // disconnectAllPeers @@ -708,25 +708,25 @@ void STKHost::mainLoop() } } - uint64_t last_ping_time = StkTime::getRealTimeMs(); - uint64_t last_update_speed_time = StkTime::getRealTimeMs(); - uint64_t last_ping_time_update_for_client = StkTime::getRealTimeMs(); + uint64_t last_ping_time = StkTime::getMonoTimeMs(); + uint64_t last_update_speed_time = StkTime::getMonoTimeMs(); + uint64_t last_ping_time_update_for_client = StkTime::getMonoTimeMs(); std::map ctp; - while (m_exit_timeout.load() > StkTime::getRealTimeMs()) + while (m_exit_timeout.load() > StkTime::getMonoTimeMs()) { // Clear outdated connect to peer list every 15 seconds for (auto it = ctp.begin(); it != ctp.end();) { - if (it->second + 15000 < StkTime::getRealTimeMs()) + if (it->second + 15000 < StkTime::getMonoTimeMs()) it = ctp.erase(it); else it++; } - if (last_update_speed_time < StkTime::getRealTimeMs()) + if (last_update_speed_time < StkTime::getMonoTimeMs()) { // Update upload / download speed per second - last_update_speed_time = StkTime::getRealTimeMs() + 1000; + last_update_speed_time = StkTime::getMonoTimeMs() + 1000; m_upload_speed.store(getNetwork()->getENetHost()->totalSentData); m_download_speed.store( getNetwork()->getENetHost()->totalReceivedData); @@ -754,11 +754,11 @@ void STKHost::mainLoop() const float timeout = ServerConfig::m_validation_timeout; bool need_ping = false; if (sl && (!sl->isRacing() || sl->allowJoinedPlayersWaiting()) && - last_ping_time < StkTime::getRealTimeMs()) + last_ping_time < StkTime::getMonoTimeMs()) { // If not racing, send an reliable packet at the 10 packets // per second, which is for accurate ping calculation by enet - last_ping_time = StkTime::getRealTimeMs() + + last_ping_time = StkTime::getMonoTimeMs() + (uint64_t)((1.0f / 10.0f) * 1000.0f); need_ping = true; } @@ -909,10 +909,10 @@ void STKHost::mainLoop() { auto lp = LobbyProtocol::get(); if (!is_server && - last_ping_time_update_for_client < StkTime::getRealTimeMs()) + last_ping_time_update_for_client < StkTime::getMonoTimeMs()) { last_ping_time_update_for_client = - StkTime::getRealTimeMs() + 2000; + StkTime::getMonoTimeMs() + 2000; if (lp && lp->isRacing()) { auto p = getServerPeerForClient(); @@ -1070,7 +1070,7 @@ void STKHost::mainLoop() else delete stk_event; } // while enet_host_service - } // while m_exit_timeout.load() > StkTime::getRealTimeMs() + } // while m_exit_timeout.load() > StkTime::getMonoTimeMs() delete direct_socket; Log::info("STKHost", "Listening has been stopped."); } // mainLoop @@ -1140,7 +1140,7 @@ void STKHost::handleDirectSocketRequest(Network* direct_socket, } if (ctp.find(peer_addr) == ctp.end()) { - ctp[peer_addr] = StkTime::getRealTimeMs(); + ctp[peer_addr] = StkTime::getMonoTimeMs(); std::make_shared(sender)->requestStart(); } } diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index 5afd044f3..7d5df62ac 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -348,10 +348,10 @@ public: { return m_nts.get(); } // ------------------------------------------------------------------------ uint64_t getNetworkTimer() const - { return StkTime::getRealTimeMs() - m_network_timer.load(); } + { return StkTime::getMonoTimeMs() - m_network_timer.load(); } // ------------------------------------------------------------------------ void setNetworkTimer(uint64_t ticks) - { m_network_timer.store(StkTime::getRealTimeMs() - ticks); } + { m_network_timer.store(StkTime::getMonoTimeMs() - ticks); } // ------------------------------------------------------------------------ std::pair getAllPlayersTeamInfo() const; // ------------------------------------------------------------------------ diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index 405622fd8..37f4845fa 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -37,14 +37,14 @@ STKPeer::STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id) { m_enet_peer = enet_peer; m_host_id = host_id; - m_connected_time = StkTime::getRealTimeMs(); + m_connected_time = StkTime::getMonoTimeMs(); m_validated.store(false); m_average_ping.store(0); m_waiting_for_game.store(true); m_spectator.store(false); m_disconnected.store(false); m_warned_for_high_ping.store(false); - m_last_activity.store((int64_t)StkTime::getRealTimeMs()); + m_last_activity.store((int64_t)StkTime::getMonoTimeMs()); } // STKPeer //----------------------------------------------------------------------------- diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index d587d13e3..eb9cb37c6 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -142,7 +142,7 @@ public: uint32_t getHostId() const { return m_host_id; } // ------------------------------------------------------------------------ float getConnectedTime() const - { return float(StkTime::getRealTimeMs() - m_connected_time) / 1000.0f; } + { return float(StkTime::getMonoTimeMs() - m_connected_time) / 1000.0f; } // ------------------------------------------------------------------------ void setAvailableKartsTracks(std::set& k, std::set& t) @@ -225,12 +225,12 @@ public: const std::string& getUserVersion() const { return m_user_version; } // ------------------------------------------------------------------------ void updateLastActivity() - { m_last_activity.store((int64_t)StkTime::getRealTimeMs()); } + { m_last_activity.store((int64_t)StkTime::getMonoTimeMs()); } // ------------------------------------------------------------------------ int idleForSeconds() const { int64_t diff = - (int64_t)StkTime::getRealTimeMs() - m_last_activity.load(); + (int64_t)StkTime::getMonoTimeMs() - m_last_activity.load(); if (diff < 0) return 0; return (int)(diff / 1000); diff --git a/src/states_screens/dialogs/player_rankings_dialog.cpp b/src/states_screens/dialogs/player_rankings_dialog.cpp index 5774340e4..3d9fece36 100644 --- a/src/states_screens/dialogs/player_rankings_dialog.cpp +++ b/src/states_screens/dialogs/player_rankings_dialog.cpp @@ -165,12 +165,12 @@ GUIEngine::EventPropagation } else if (selection == m_refresh_widget->m_properties[PROP_ID]) { - static uint64_t timer = StkTime::getRealTimeMs(); + static uint64_t timer = StkTime::getMonoTimeMs(); // 1 minute per refresh - if (StkTime::getRealTimeMs() < timer + 60000) + if (StkTime::getMonoTimeMs() < timer + 60000) return GUIEngine::EVENT_BLOCK; - timer = StkTime::getRealTimeMs(); + timer = StkTime::getMonoTimeMs(); *m_fetched_ranking = false; updatePlayerRanking(m_name, m_online_id, m_ranking_info, m_fetched_ranking); diff --git a/src/states_screens/online/network_kart_selection.cpp b/src/states_screens/online/network_kart_selection.cpp index 98465a6f7..d30809159 100644 --- a/src/states_screens/online/network_kart_selection.cpp +++ b/src/states_screens/online/network_kart_selection.cpp @@ -72,7 +72,7 @@ void NetworkKartSelectionScreen::init() */ void NetworkKartSelectionScreen::onUpdate(float dt) { - if (StkTime::getRealTimeMs() > m_exit_timeout) + if (StkTime::getMonoTimeMs() > m_exit_timeout) { // Reset the screen to networking menu if failed to back to lobby STKHost::get()->shutdown(); @@ -154,7 +154,7 @@ bool NetworkKartSelectionScreen::onEscapePressed() { // Send go back lobby event to server with an exit timeout, so if // server doesn't react in time we exit the server - m_exit_timeout = StkTime::getRealTimeMs() + 5000; + m_exit_timeout = StkTime::getMonoTimeMs() + 5000; NetworkString back(PROTOCOL_LOBBY_ROOM); back.addUInt8(LobbyProtocol::LE_CLIENT_BACK_LOBBY); STKHost::get()->sendToServer(&back, true); diff --git a/src/states_screens/online/networking_lobby.cpp b/src/states_screens/online/networking_lobby.cpp index 77b1223c2..7d6df1547 100644 --- a/src/states_screens/online/networking_lobby.cpp +++ b/src/states_screens/online/networking_lobby.cpp @@ -296,9 +296,9 @@ void NetworkingLobby::onUpdate(float delta) m_config_button->setImage(m_config_texture); m_client_live_joinable = false; - if (m_player_list && StkTime::getRealTimeMs() > m_ping_update_timer) + if (m_player_list && StkTime::getMonoTimeMs() > m_ping_update_timer) { - m_ping_update_timer = StkTime::getRealTimeMs() + 2000; + m_ping_update_timer = StkTime::getMonoTimeMs() + 2000; updatePlayerPings(); } @@ -415,7 +415,7 @@ void NetworkingLobby::onUpdate(float delta) if (cur_player >= m_min_start_game_players && m_cur_starting_timer == std::numeric_limits::max()) { - m_cur_starting_timer = (int64_t)StkTime::getRealTimeMs() + + m_cur_starting_timer = (int64_t)StkTime::getMonoTimeMs() + (int64_t)(m_start_timeout * 1000.0); } else if (cur_player < m_min_start_game_players) @@ -433,7 +433,7 @@ void NetworkingLobby::onUpdate(float delta) if (m_cur_starting_timer != std::numeric_limits::max()) { int64_t remain = (m_cur_starting_timer - - (int64_t)StkTime::getRealTimeMs()) / 1000; + (int64_t)StkTime::getMonoTimeMs()) / 1000; if (remain < 0) remain = 0; //I18N: In the networking lobby, display the starting timeout @@ -752,5 +752,5 @@ void NetworkingLobby::initAutoStartTimer(bool grand_prix_started, void NetworkingLobby::setStartingTimerTo(float t) { m_cur_starting_timer = - (int64_t)StkTime::getRealTimeMs() + (int64_t)(t * 1000.0f); + (int64_t)StkTime::getMonoTimeMs() + (int64_t)(t * 1000.0f); } // setStartingTimerTo diff --git a/src/states_screens/online/tracks_screen.cpp b/src/states_screens/online/tracks_screen.cpp index 075099785..0391a88ab 100644 --- a/src/states_screens/online/tracks_screen.cpp +++ b/src/states_screens/online/tracks_screen.cpp @@ -674,7 +674,7 @@ void TracksScreen::onUpdate(float dt) { int list_id = m_vote_list->getItemID(StringUtils::toString(m_winning_index)); - //if (StkTime::getRealTimeMs() / 1000 % 2 == 0) + //if (StkTime::getMonoTimeMs() / 1000 % 2 == 0) m_vote_list->setSelectionID(list_id); //else // m_vote_list->unfocused(PLAYER_ID_GAME_MASTER, NULL); diff --git a/src/tracks/check_trigger.cpp b/src/tracks/check_trigger.cpp index d3b267f32..e23477844 100644 --- a/src/tracks/check_trigger.cpp +++ b/src/tracks/check_trigger.cpp @@ -32,7 +32,7 @@ CheckTrigger::CheckTrigger(const Vec3& center, float distance, m_center(center), m_distance2(distance * distance), m_triggering_function(triggering_function) { - m_last_triggered_time = StkTime::getRealTimeMs(); + m_last_triggered_time = StkTime::getMonoTimeMs(); } // CheckSphere // ---------------------------------------------------------------------------- @@ -44,12 +44,12 @@ bool CheckTrigger::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos, // kart_id will be -1 if called by CheckManager::getChecklineTriggering if (kart_id < 0 || kart_id >= (int)World::getWorld()->getNumKarts()) return false; - if (m_last_triggered_time + 2000 > StkTime::getRealTimeMs()) + if (m_last_triggered_time + 2000 > StkTime::getMonoTimeMs()) return false; AbstractKart* k = World::getWorld()->getKart(kart_id); if ((k->getXYZ() - m_center).length2() < m_distance2) { - m_last_triggered_time = StkTime::getRealTimeMs(); + m_last_triggered_time = StkTime::getMonoTimeMs(); return true; } return false; diff --git a/src/tracks/track_object_presentation.cpp b/src/tracks/track_object_presentation.cpp index 36d3d4317..865f5eb83 100644 --- a/src/tracks/track_object_presentation.cpp +++ b/src/tracks/track_object_presentation.cpp @@ -1142,7 +1142,7 @@ TrackObjectPresentationActionTrigger::TrackObjectPresentationActionTrigger( // ---------------------------------------------------------------------------- void TrackObjectPresentationActionTrigger::onTriggerItemApproached() { - if (m_reenable_timeout > StkTime::getRealTimeMs()) + if (m_reenable_timeout > StkTime::getMonoTimeMs()) { return; } diff --git a/src/tracks/track_object_presentation.hpp b/src/tracks/track_object_presentation.hpp index f84dbc386..358ae7ad4 100644 --- a/src/tracks/track_object_presentation.hpp +++ b/src/tracks/track_object_presentation.hpp @@ -412,20 +412,20 @@ public: // ------------------------------------------------------------------------ /** Reset the trigger (i.e. sets it to active again). */ virtual void reset() OVERRIDE - { m_reenable_timeout = StkTime::getRealTimeMs(); } + { m_reenable_timeout = StkTime::getMonoTimeMs(); } // ------------------------------------------------------------------------ - /** Sets the trigger to be enabled or disabled. getRealTimeMs is used to + /** Sets the trigger to be enabled or disabled. getMonoTimeMs is used to * to avoid called update which duplicated in network rewinding. */ virtual void setEnable(bool status) OVERRIDE { - m_reenable_timeout = status ? StkTime::getRealTimeMs() : + m_reenable_timeout = status ? StkTime::getMonoTimeMs() : std::numeric_limits::max(); } // ------------------------------------------------------------------------ void setReenableTimeout(float time) { m_reenable_timeout = - StkTime::getRealTimeMs() + (uint64_t)(time * 1000.0f); + StkTime::getMonoTimeMs() + (uint64_t)(time * 1000.0f); } }; // class TrackObjectPresentationActionTrigger diff --git a/src/utils/time.cpp b/src/utils/time.cpp index 982625a8a..1ad3bf0b1 100644 --- a/src/utils/time.cpp +++ b/src/utils/time.cpp @@ -24,6 +24,8 @@ #include irr::ITimer *StkTime::m_timer = NULL; +std::chrono::steady_clock::time_point + StkTime::m_mono_start = std::chrono::steady_clock::now(); /** Init function for the timer. It grabs a copy of the timer of the * current irrlicht device (which is the NULL device). This way the @@ -94,14 +96,14 @@ void StkTime::getDate(int *day, int *month, int *year) StkTime::ScopeProfiler::ScopeProfiler(const char* name) { Log::info("ScopeProfiler", "%s {\n", name); - m_time = getRealTimeMs(); + m_time = getMonoTimeMs(); m_name = name; } // StkTime::ScopeProfiler::ScopeProfiler // ---------------------------------------------------------------------------- StkTime::ScopeProfiler::~ScopeProfiler() { - uint64_t difference = getRealTimeMs() - m_time; + uint64_t difference = getMonoTimeMs() - m_time; Log::info("ScopeProfiler", "} // took %d ms (%s)\n", (int)difference, m_name.c_str()); } // StkTime::ScopeProfiler::ScopeProfiler diff --git a/src/utils/time.hpp b/src/utils/time.hpp index 23340eee8..0ab1cd2f2 100644 --- a/src/utils/time.hpp +++ b/src/utils/time.hpp @@ -47,6 +47,8 @@ private: * (and the sfx threads needs real time at that time). */ static irr::ITimer *m_timer; + /** Initalized when STK starts. */ + static std::chrono::steady_clock::time_point m_mono_start; public: typedef time_t TimeType; @@ -94,17 +96,14 @@ public: */ static double getRealTime(long startAt=0); // ------------------------------------------------------------------------ - /** Returns a time based since epoch. + /** Returns a time based since the starting of stk (monotonic clock). * The value is a 64bit unsigned integer in milliseconds. */ - static uint64_t getRealTimeMs() + static uint64_t getMonoTimeMs() { - auto now = std::chrono::system_clock::now(); - auto now_ms = - std::chrono::time_point_cast(now); - auto epoch = now_ms.time_since_epoch(); + auto duration = std::chrono::steady_clock::now() - m_mono_start; auto value = - std::chrono::duration_cast(epoch); + std::chrono::duration_cast(duration); return value.count(); } // ------------------------------------------------------------------------