From 0f39add43262cfd701bc9edf162d3c54d2daf22a Mon Sep 17 00:00:00 2001 From: Benau Date: Mon, 27 Aug 2018 13:49:11 +0800 Subject: [PATCH] Convert time-related code in network to 64bit to avoid overflow --- src/main_loop.cpp | 11 ++- src/main_loop.hpp | 4 +- src/network/protocols/client_lobby.cpp | 1 + src/network/protocols/connect_to_peer.cpp | 4 +- src/network/protocols/connect_to_peer.hpp | 2 +- src/network/protocols/connect_to_server.cpp | 14 ++-- src/network/protocols/connect_to_server.hpp | 2 +- src/network/protocols/request_connection.cpp | 4 +- src/network/protocols/server_lobby.cpp | 70 +++++++++++-------- src/network/protocols/server_lobby.hpp | 9 ++- src/network/servers_manager.cpp | 14 ++-- src/network/servers_manager.hpp | 2 +- src/network/stk_host.cpp | 41 ++++++----- src/network/stk_host.hpp | 2 +- src/network/stk_peer.cpp | 6 +- src/network/stk_peer.hpp | 6 +- .../dialogs/player_rankings_dialog.cpp | 6 +- src/states_screens/tracks_screen.cpp | 10 +-- src/states_screens/tracks_screen.hpp | 5 +- 19 files changed, 110 insertions(+), 103 deletions(-) diff --git a/src/main_loop.cpp b/src/main_loop.cpp index ce5220865..73238eaa2 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -42,6 +42,7 @@ #include "race/race_manager.hpp" #include "states_screens/state_manager.hpp" #include "utils/profiler.hpp" +#include "utils/time.hpp" #ifndef WIN32 #include @@ -111,11 +112,9 @@ float MainLoop::getLimitedDt() return 1.0f/60.0f; } - IrrlichtDevice* device = irr_driver->getDevice(); - while( 1 ) { - m_curr_time = device->getTimer()->getRealTime(); + m_curr_time = StkTime::getRealTimeMs(); dt = (float)(m_curr_time - m_prev_time); // On a server (i.e. without graphics) the frame rate can be under // 1 ms, i.e. dt = 0. Additionally, the resolution of a sleep @@ -131,7 +130,7 @@ float MainLoop::getLimitedDt() while (dt <= 0 && !ProfileWorld::isProfileMode()) { StkTime::sleep(1); - m_curr_time = device->getTimer()->getRealTime(); + m_curr_time = StkTime::getRealTimeMs(); dt = (float)(m_curr_time - m_prev_time); } @@ -279,9 +278,7 @@ void MainLoop::updateRace(int ticks) */ void MainLoop::run() { - IrrlichtDevice* device = irr_driver->getDevice(); - - m_curr_time = device->getTimer()->getRealTime(); + m_curr_time = StkTime::getRealTimeMs(); // DT keeps track of the leftover time, since the race update // happens in fixed timesteps float left_over_time = 0; diff --git a/src/main_loop.hpp b/src/main_loop.hpp index f1ab747b7..efa4c332e 100644 --- a/src/main_loop.hpp +++ b/src/main_loop.hpp @@ -39,8 +39,8 @@ private: Synchronised m_ticks_adjustment; - uint32_t m_curr_time; - uint32_t m_prev_time; + uint64_t m_curr_time; + uint64_t m_prev_time; unsigned m_parent_pid; float getLimitedDt(); void updateRace(int ticks); diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index 3bd2ad95d..94d4f7dad 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -771,6 +771,7 @@ void ClientLobby::startGame(Event* event) int sleep_time = (int)(start_time - cur_time); Log::info("ClientLobby", "Start game after %dms", sleep_time); std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time)); + Log::info("ClientLobby", "Started at %lf", StkTime::getRealTime()); m_state.store(RACING); }); } // startGame diff --git a/src/network/protocols/connect_to_peer.cpp b/src/network/protocols/connect_to_peer.cpp index 9e0ccd206..d4dbe72c0 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::getRealTime() > m_timer + 2.0) + if (StkTime::getRealTimeMs() > m_timer + 2000) { - m_timer = StkTime::getRealTime(); + m_timer = StkTime::getRealTimeMs(); // Send a broadcast packet with the string aloha_stk inside, // the client will know our ip address and will connect // The wan remote should already start its ping message to us now diff --git a/src/network/protocols/connect_to_peer.hpp b/src/network/protocols/connect_to_peer.hpp index d9318f681..48b3c514b 100644 --- a/src/network/protocols/connect_to_peer.hpp +++ b/src/network/protocols/connect_to_peer.hpp @@ -33,7 +33,7 @@ protected: TransportAddress m_peer_address; /** Timer use for tracking broadcast. */ - double m_timer = 0.0; + uint64_t m_timer = 0; /** If greater than a certain value, terminate this protocol. */ unsigned m_tried_connection = 0; diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index 50fcfe59b..643ca02ec 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -141,7 +141,7 @@ void ConnectToServer::asynchronousUpdate() request_connection->requestStart(); m_current_protocol = request_connection; // Reset timer for next usage - m_timer = 0.0; + m_timer = 0; break; } case REQUESTING_CONNECTION: @@ -169,7 +169,7 @@ void ConnectToServer::asynchronousUpdate() " aloha, trying to connect anyway."); m_state = CONNECTING; // Reset timer for next usage - m_timer = 0.0; + m_timer = 0; m_tried_connection = 0; } else @@ -190,9 +190,9 @@ void ConnectToServer::asynchronousUpdate() { // Send a 1-byte datagram, the remote host can simply ignore // this datagram, to keep the port open (2 second each) - if (StkTime::getRealTime() > m_timer + 2.0) + if (StkTime::getRealTimeMs() > m_timer + 2000) { - m_timer = StkTime::getRealTime(); + m_timer = StkTime::getRealTimeMs(); BareNetworkString data; data.addUInt8(0); STKHost::get()->sendRawPacket(data, m_server_address); @@ -204,9 +204,9 @@ void ConnectToServer::asynchronousUpdate() case CONNECTING: // waiting the server to answer our connection { // Every 5 seconds - if (StkTime::getRealTime() > m_timer + 5.0) + if (StkTime::getRealTimeMs() > m_timer + 5000) { - m_timer = StkTime::getRealTime(); + m_timer = StkTime::getRealTimeMs(); STKHost::get()->stopListening(); STKHost::get()->connect(m_server_address); STKHost::get()->startListening(); @@ -420,7 +420,7 @@ void ConnectToServer::waitingAloha(bool is_wan) m_server_address = sender; m_state = CONNECTING; // Reset timer for next usage - m_timer = 0.0; + m_timer = 0; m_tried_connection = 0; } } // waitingAloha diff --git a/src/network/protocols/connect_to_server.hpp b/src/network/protocols/connect_to_server.hpp index 05b52d24d..c4b5e63f6 100644 --- a/src/network/protocols/connect_to_server.hpp +++ b/src/network/protocols/connect_to_server.hpp @@ -31,7 +31,7 @@ class Server; class ConnectToServer : public Protocol { private: - double m_timer = 0.0; + uint64_t m_timer = 0; TransportAddress m_server_address; std::shared_ptr m_server; unsigned m_tried_connection = 0; diff --git a/src/network/protocols/request_connection.cpp b/src/network/protocols/request_connection.cpp index 00c88d16d..5d4f96814 100644 --- a/src/network/protocols/request_connection.cpp +++ b/src/network/protocols/request_connection.cpp @@ -77,8 +77,8 @@ void RequestConnection::asynchronousUpdate() { // Allow up to 10 seconds for the separate process to // fully start-up - double timeout = StkTime::getRealTime() + 10.; - while (StkTime::getRealTime() < timeout) + uint64_t timeout = StkTime::getRealTimeMs() + 10000; + while (StkTime::getRealTimeMs() < timeout) { const std::string& sid = NetworkConfig::get() ->getServerIdFile(); diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index bb9de28ad..8c45a8623 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -212,7 +212,7 @@ void ServerLobby::setup() // the server are ready: resetPeersReady(); m_peers_votes.clear(); - m_timeout.store(std::numeric_limits::max()); + m_timeout.store(std::numeric_limits::max()); m_waiting_for_reset = false; Log::info("ServerLobby", "Reset server to initial state."); @@ -397,19 +397,20 @@ void ServerLobby::asynchronousUpdate() (float)NetworkConfig::get()->getMaxPlayers() * UserConfigParams::m_start_game_threshold || m_game_setup->isGrandPrixStarted()) && - m_timeout.load() == std::numeric_limits::max()) + m_timeout.load() == std::numeric_limits::max()) { - m_timeout.store((float)StkTime::getRealTime() + - UserConfigParams::m_start_game_counter); + m_timeout.store((int64_t)StkTime::getRealTimeMs() + + (int64_t) + (UserConfigParams::m_start_game_counter * 1000.0f)); } else if ((float)players.size() < (float)NetworkConfig::get()->getMaxPlayers() * UserConfigParams::m_start_game_threshold && !m_game_setup->isGrandPrixStarted()) { - m_timeout.store(std::numeric_limits::max()); + m_timeout.store(std::numeric_limits::max()); } - if (m_timeout.load() < (float)StkTime::getRealTime()) + if (m_timeout.load() < (int64_t)StkTime::getRealTimeMs()) { startSelection(); return; @@ -444,10 +445,11 @@ void ServerLobby::asynchronousUpdate() case SELECTING: { auto result = handleVote(); - if (m_timeout.load() < (float)StkTime::getRealTime() || + if (m_timeout.load() < (int64_t)StkTime::getRealTimeMs() || (std::get<3>(result) && - m_timeout.load() - (UserConfigParams::m_voting_timeout / 2.0f) < - (float)StkTime::getRealTime())) + m_timeout.load() - + (int64_t)(UserConfigParams::m_voting_timeout / 2.0f * 1000.0f) < + (int64_t)StkTime::getRealTimeMs())) { m_game_setup->setRace(std::get<0>(result), std::get<1>(result), std::get<2>(result)); @@ -509,9 +511,8 @@ void ServerLobby::asynchronousUpdate() void ServerLobby::sendBadConnectionMessageToPeer(std::shared_ptr p) { const unsigned max_ping = UserConfigParams::m_max_ping; - Log::warn("ServerLobby", "Peer %s cannot catch up with max ping %d, it" - " started at %lf.", p->getAddress().toString().c_str(), max_ping, - StkTime::getRealTime()); + Log::warn("ServerLobby", "Peer %s cannot catch up with max ping %d.", + p->getAddress().toString().c_str(), max_ping); NetworkString* msg = getNetworkString(); msg->setSynchronous(true); msg->addUInt8(LE_BAD_CONNECTION); @@ -627,14 +628,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((float)StkTime::getRealTime() + 15.0f); + m_timeout.store((int64_t)StkTime::getRealTimeMs() + 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() || - StkTime::getRealTime() > m_timeout.load()) + (int64_t)StkTime::getRealTimeMs() > m_timeout.load()) { // Send a notification to all clients to exit // the race result screen @@ -866,7 +867,7 @@ void ServerLobby::startSelection(const Event *event) ul.unlock(); // Will be changed after the first vote received - m_timeout.store(std::numeric_limits::max()); + m_timeout.store(std::numeric_limits::max()); } // startSelection //----------------------------------------------------------------------------- @@ -876,9 +877,9 @@ void ServerLobby::startSelection(const Event *event) void ServerLobby::checkIncomingConnectionRequests() { // First poll every 5 seconds. Return if no polling needs to be done. - const float POLL_INTERVAL = 5.0f; - static double last_poll_time = 0; - if (StkTime::getRealTime() < last_poll_time + POLL_INTERVAL) + const uint64_t POLL_INTERVAL = 5000; + static uint64_t last_poll_time = 0; + if (StkTime::getRealTimeMs() < last_poll_time + POLL_INTERVAL) return; // Keep the port open, it can be sent to anywhere as we will send to the @@ -891,7 +892,7 @@ void ServerLobby::checkIncomingConnectionRequests() } // Now poll the stk server - last_poll_time = StkTime::getRealTime(); + last_poll_time = StkTime::getRealTimeMs(); // ======================================================================== class PollServerRequest : public Online::XMLRequest @@ -1509,15 +1510,20 @@ void ServerLobby::handleUnencryptedConnection(std::shared_ptr peer, NetworkString* message_ack = getNetworkString(4); message_ack->setSynchronous(true); // connection success -- return the host id of peer - float auto_start_timer = m_timeout.load(); + float auto_start_timer = 0.0f; + if (m_timeout.load() == std::numeric_limits::max()) + auto_start_timer = std::numeric_limits::max(); + else + { + auto_start_timer = + (m_timeout.load() - (int64_t)StkTime::getRealTimeMs()) / 1000.0f; + } message_ack->addUInt8(LE_CONNECTION_ACCEPTED).addUInt32(peer->getHostId()) - .addUInt32(NetworkConfig::m_server_version) - .addFloat(auto_start_timer == std::numeric_limits::max() ? - auto_start_timer : auto_start_timer - (float)StkTime::getRealTime()); + .addUInt32(NetworkConfig::m_server_version).addFloat(auto_start_timer); peer->sendPacket(message_ack); delete message_ack; - m_peers_ready[peer] = std::make_pair(false, 0.0); + m_peers_ready[peer] = false; for (std::shared_ptr npp : peer->getPlayerProfiles()) { m_game_setup->addPlayer(npp); @@ -1658,13 +1664,14 @@ void ServerLobby::playerVote(Event* event) return; // Check if first vote, if so start counter - if (m_timeout.load() == std::numeric_limits::max()) + if (m_timeout.load() == std::numeric_limits::max()) { - m_timeout.store((float)StkTime::getRealTime() + - UserConfigParams::m_voting_timeout); + m_timeout.store((int64_t)StkTime::getRealTimeMs() + + (int64_t)(UserConfigParams::m_voting_timeout * 1000.0f)); } - float remaining_time = m_timeout.load() - (float)StkTime::getRealTime(); - if (remaining_time < 0.0f) + int64_t remaining_time = + m_timeout.load() - (int64_t)StkTime::getRealTimeMs(); + if (remaining_time < 0) { return; } @@ -1847,7 +1854,7 @@ void ServerLobby::finishedLoadingWorld() void ServerLobby::finishedLoadingWorldClient(Event *event) { std::shared_ptr peer = event->getPeerSP(); - m_peers_ready.at(peer) = std::make_pair(true, StkTime::getRealTime()); + m_peers_ready.at(peer) = true; Log::info("ServerLobby", "Peer %d has finished loading world at %lf", peer->getHostId(), StkTime::getRealTime()); } // finishedLoadingWorldClient @@ -1861,7 +1868,7 @@ void ServerLobby::playerFinishedResult(Event *event) if (m_state.load() != RESULT_DISPLAY) return; std::shared_ptr peer = event->getPeerSP(); - m_peers_ready.at(peer) = std::make_pair(true, StkTime::getRealTime()); + m_peers_ready.at(peer) = true; } // playerFinishedResult //----------------------------------------------------------------------------- @@ -2083,6 +2090,7 @@ void ServerLobby::configPeersStartTime() int sleep_time = (int)(start_time - cur_time); Log::info("ServerLobby", "Start game after %dms", sleep_time); std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time)); + Log::info("ServerLobby", "Started at %lf", StkTime::getRealTime()); m_state.store(RACING); }); } // configPeersStartTime diff --git a/src/network/protocols/server_lobby.hpp b/src/network/protocols/server_lobby.hpp index 8c1827081..7eec95e6a 100644 --- a/src/network/protocols/server_lobby.hpp +++ b/src/network/protocols/server_lobby.hpp @@ -82,7 +82,7 @@ private: std::atomic_bool m_server_has_loaded_world; /** Counts how many peers have finished loading the world. */ - std::map, std::pair, + std::map, bool, std::owner_less > > m_peers_ready; /** Vote from each peer. */ @@ -95,7 +95,7 @@ private: std::weak_ptr m_server_unregistered; /** Timeout counter for various state. */ - std::atomic m_timeout; + std::atomic m_timeout; /** Lock this mutex whenever a client is connect / disconnect or * starting race. */ @@ -160,7 +160,7 @@ private: { if (p.first.expired()) continue; - all_ready = all_ready && p.second.first; + all_ready = all_ready && p.second; if (!all_ready) return false; } @@ -176,8 +176,7 @@ private: } else { - it->second.first = false; - it->second.second = 0.0; + it->second = false; it++; } } diff --git a/src/network/servers_manager.cpp b/src/network/servers_manager.cpp index e4e2ff51e..0078e9a2e 100644 --- a/src/network/servers_manager.cpp +++ b/src/network/servers_manager.cpp @@ -41,7 +41,7 @@ # include #endif -#define SERVER_REFRESH_INTERVAL 5.0f +const uint64_t SERVER_REFRESH_INTERVAL = 5000; static ServersManager* g_manager_singleton(NULL); @@ -64,7 +64,7 @@ void ServersManager::deallocate() // ---------------------------------------------------------------------------- ServersManager::ServersManager() { - m_last_load_time.store(0.0f); + m_last_load_time.store(0); m_list_updated = false; } // ServersManager @@ -158,8 +158,8 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const char buffer[LEN]; // Wait for up to 0.5 seconds to receive an answer from // any local servers. - double start_time = StkTime::getRealTime(); - const double DURATION = 1.0; + uint64_t start_time = StkTime::getRealTimeMs(); + const uint64_t DURATION = 1000; const auto& servers = ServersManager::get()->getServers(); int cur_server_id = (int)servers.size(); assert(cur_server_id == 0); @@ -169,7 +169,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::getRealTime() - start_time < DURATION) + while (StkTime::getRealTimeMs() - start_time < DURATION) { TransportAddress sender; int len = broadcast->receiveRawPacket(buffer, LEN, &sender, 1); @@ -236,7 +236,7 @@ void ServersManager::setLanServers(const std::map(*servers_xml->getNode(i))); } - m_last_load_time.store((float)StkTime::getRealTime()); + m_last_load_time.store(StkTime::getRealTimeMs()); m_list_updated = true; } // refresh diff --git a/src/network/servers_manager.hpp b/src/network/servers_manager.hpp index 40f4a7179..fec48ee9e 100644 --- a/src/network/servers_manager.hpp +++ b/src/network/servers_manager.hpp @@ -45,7 +45,7 @@ private: /** List of broadcast addresses to use. */ std::vector m_broadcast_address; - std::atomic m_last_load_time; + std::atomic m_last_load_time; std::atomic_bool m_list_updated; // ------------------------------------------------------------------------ diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 3a29982ad..29111f7bd 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -307,7 +307,7 @@ void STKHost::init() m_shutdown = false; m_authorised = false; m_network = NULL; - m_exit_timeout.store(std::numeric_limits::max()); + m_exit_timeout.store(std::numeric_limits::max()); m_client_ping.store(0); // Start with initialising ENet @@ -440,7 +440,7 @@ void STKHost::setPublicAddress() } m_network->sendRawPacket(s, m_stun_address); - double ping = StkTime::getRealTime(); + uint64_t ping = StkTime::getRealTimeMs(); freeaddrinfo(res); // Recieve now @@ -448,7 +448,7 @@ void STKHost::setPublicAddress() const int LEN = 2048; char buffer[LEN]; int len = m_network->receiveRawPacket(buffer, LEN, &sender, 2000); - ping = StkTime::getRealTime() - ping; + ping = StkTime::getRealTimeMs() - ping; if (sender.getIP() != m_stun_address.getIP()) { @@ -587,8 +587,7 @@ void STKHost::setPublicAddress() m_public_address = non_xor_addr; } // Succeed, save ping - UserConfigParams::m_stun_list[server_name] = - (uint32_t)(ping * 1000.0); + UserConfigParams::m_stun_list[server_name] = (uint32_t)(ping); untried_server.clear(); } } @@ -620,7 +619,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::getRealTime() + 2.0); + m_exit_timeout.store(StkTime::getRealTimeMs() + 2000); } m_peers.clear(); } // disconnectAllPeers @@ -667,7 +666,7 @@ bool STKHost::connect(const TransportAddress& address) */ void STKHost::startListening() { - m_exit_timeout.store(std::numeric_limits::max()); + m_exit_timeout.store(std::numeric_limits::max()); m_listening_thread = std::thread(std::bind(&STKHost::mainLoop, this)); } // startListening @@ -677,7 +676,7 @@ void STKHost::startListening() */ void STKHost::stopListening() { - if (m_exit_timeout.load() == std::numeric_limits::max()) + if (m_exit_timeout.load() == std::numeric_limits::max()) m_exit_timeout.store(0.0); if (m_listening_thread.joinable()) m_listening_thread.join(); @@ -716,9 +715,9 @@ void STKHost::mainLoop() } } - double last_ping_time = StkTime::getRealTime(); - double last_ping_time_update_for_client = StkTime::getRealTime(); - while (m_exit_timeout.load() > StkTime::getRealTime()) + uint64_t last_ping_time = StkTime::getRealTimeMs(); + uint64_t last_ping_time_update_for_client = StkTime::getRealTimeMs(); + while (m_exit_timeout.load() > StkTime::getRealTimeMs()) { auto sl = LobbyProtocol::get(); if (direct_socket && sl && sl->waitingForPlayers()) @@ -740,12 +739,13 @@ void STKHost::mainLoop() const float timeout = UserConfigParams::m_validation_timeout; bool need_ping = false; if (sl && !sl->isRacing() && - last_ping_time < StkTime::getRealTime()) + last_ping_time < StkTime::getRealTimeMs()) { // If not racing, send an reliable packet at the same rate with // state exchange to keep enet ping accurate - last_ping_time = StkTime::getRealTime() + - 1.0 / double(stk_config->m_network_state_frequeny); + last_ping_time = StkTime::getRealTimeMs() + + (uint64_t)((1.0f / + (float)(stk_config->m_network_state_frequeny)) * 1000.0f); need_ping = true; } @@ -793,8 +793,7 @@ void STKHost::mainLoop() // Remove peer which has not been validated after a specific time // It is validated when the first connection request has finished if (!it->second->isValidated() && - (float)StkTime::getRealTime() > - it->second->getConnectedTime() + timeout) + it->second->getConnectedTime() > timeout) { Log::info("STKHost", "%s has not been validated for more" " than %f seconds, disconnect it by force.", @@ -843,10 +842,10 @@ void STKHost::mainLoop() while (enet_host_service(host, &event, 10) != 0) { if (!is_server && - last_ping_time_update_for_client < StkTime::getRealTime()) + last_ping_time_update_for_client < StkTime::getRealTimeMs()) { last_ping_time_update_for_client = - StkTime::getRealTime() + 2.0; + StkTime::getRealTimeMs() + 2000; auto lp = LobbyProtocol::get(); if (lp && lp->isRacing()) { @@ -886,9 +885,9 @@ void STKHost::mainLoop() // If used a timeout waiting disconnect, exit now if (m_exit_timeout.load() != - std::numeric_limits::max()) + std::numeric_limits::max()) { - m_exit_timeout.store(0.0); + m_exit_timeout.store(0); break; } // Use the previous stk peer so protocol can see the network @@ -981,7 +980,7 @@ void STKHost::mainLoop() else delete stk_event; } // while enet_host_service - } // while m_exit_timeout.load() > StkTime::getRealTime() + } // while m_exit_timeout.load() > StkTime::getRealTimeMs() delete direct_socket; Log::info("STKHost", "Listening has been stopped."); } // mainLoop diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index 5e06da5ee..1237833a1 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -120,7 +120,7 @@ private: std::atomic_bool m_authorised; /** Use as a timeout to waiting a disconnect event when exiting. */ - std::atomic m_exit_timeout; + std::atomic m_exit_timeout; /** An error message, which is set by a protocol to be displayed * in the GUI. */ diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index fbc9b96ac..a38a6a853 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -37,7 +37,7 @@ STKPeer::STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id) { m_enet_peer = enet_peer; m_host_id = host_id; - m_connected_time = (float)StkTime::getRealTime(); + m_connected_time = StkTime::getRealTimeMs(); m_validated.store(false); m_average_ping.store(0); } // STKPeer @@ -114,7 +114,7 @@ void STKPeer::sendPacket(NetworkString *data, bool reliable, bool encrypted) { if (Network::m_connection_debug) { - Log::verbose("STKPeer", "sending packet of size %d to %s at %f", + Log::verbose("STKPeer", "sending packet of size %d to %s at %lf", packet->dataLength, a.toString().c_str(), StkTime::getRealTime()); } @@ -155,7 +155,7 @@ bool STKPeer::isSamePeer(const ENetPeer* peer) const */ uint32_t STKPeer::getPing() { - if ((float)StkTime::getRealTime() - m_connected_time < 3.0f) + if (getConnectedTime() < 3.0f) return 0; if (NetworkConfig::get()->isServer()) { diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index ed327694f..a16c3db23 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -25,6 +25,7 @@ #include "network/transport_address.hpp" #include "utils/no_copy.hpp" +#include "utils/time.hpp" #include "utils/types.hpp" #include @@ -72,7 +73,7 @@ protected: std::vector > m_players; - float m_connected_time; + uint64_t m_connected_time; /** Available karts and tracks from this peer */ std::pair, std::set > m_available_kts; @@ -120,7 +121,8 @@ public: /** Returns the host id of this peer. */ uint32_t getHostId() const { return m_host_id; } // ------------------------------------------------------------------------ - float getConnectedTime() const { return m_connected_time; } + float getConnectedTime() const + { return float(StkTime::getRealTimeMs() - m_connected_time) / 1000.0f; } // ------------------------------------------------------------------------ void setAvailableKartsTracks(std::set& k, std::set& t) diff --git a/src/states_screens/dialogs/player_rankings_dialog.cpp b/src/states_screens/dialogs/player_rankings_dialog.cpp index 3d36d53eb..5774340e4 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 double timer = StkTime::getRealTime(); + static uint64_t timer = StkTime::getRealTimeMs(); // 1 minute per refresh - if (StkTime::getRealTime() < timer + 60.0) + if (StkTime::getRealTimeMs() < timer + 60000) return GUIEngine::EVENT_BLOCK; - timer = StkTime::getRealTime(); + timer = StkTime::getRealTimeMs(); *m_fetched_ranking = false; updatePlayerRanking(m_name, m_online_id, m_ranking_info, m_fetched_ranking); diff --git a/src/states_screens/tracks_screen.cpp b/src/states_screens/tracks_screen.cpp index 6bccd95a9..dcfdb4fcc 100644 --- a/src/states_screens/tracks_screen.cpp +++ b/src/states_screens/tracks_screen.cpp @@ -135,7 +135,7 @@ bool TracksScreen::onEscapePressed() void TracksScreen::tearDown() { m_network_tracks = false; - m_vote_timeout = -1.0f; + m_vote_timeout = std::numeric_limits::max(); m_selected_track = NULL; } // tearDown @@ -418,9 +418,9 @@ void TracksScreen::setFocusOnTrack(const std::string& trackName) // ----------------------------------------------------------------------------- void TracksScreen::setVoteTimeout(float timeout) { - if (m_vote_timeout != -1.0f) + if (m_vote_timeout != std::numeric_limits::max()) return; - m_vote_timeout = (float)StkTime::getRealTime() + timeout; + m_vote_timeout = StkTime::getRealTimeMs() + (uint64_t)(timeout * 1000.0f); } // setVoteTimeout // ----------------------------------------------------------------------------- @@ -451,14 +451,14 @@ void TracksScreen::voteForPlayer() void TracksScreen::onUpdate(float dt) { assert(m_votes); - if (m_vote_timeout == -1.0f) + if (m_vote_timeout == std::numeric_limits::max()) { m_votes->setText(L"", false); return; } m_votes->setVisible(true); - int remaining_time = (int)(m_vote_timeout - StkTime::getRealTime()); + int remaining_time = (m_vote_timeout - StkTime::getRealTimeMs()) / 1000; if (remaining_time < 0) remaining_time = 0; //I18N: In tracks screen, about voting of tracks in network diff --git a/src/states_screens/tracks_screen.hpp b/src/states_screens/tracks_screen.hpp index 9a95716af..293428b77 100644 --- a/src/states_screens/tracks_screen.hpp +++ b/src/states_screens/tracks_screen.hpp @@ -21,6 +21,7 @@ #include "guiengine/screen.hpp" #include "utils/synchronised.hpp" #include +#include #include #include @@ -58,7 +59,7 @@ private: int m_bottom_box_height = -1; - float m_vote_timeout = -1.0f; + uint64_t m_vote_timeout = std::numeric_limits::max(); std::map m_vote_messages; @@ -101,7 +102,7 @@ public: void resetVote() { m_vote_messages.clear(); - m_vote_timeout = -1.0f; + m_vote_timeout = std::numeric_limits::max(); } void setVoteTimeout(float timeout);