diff --git a/src/graphics/irr_driver.cpp b/src/graphics/irr_driver.cpp index 65ce8ffc5..dbc6f6392 100644 --- a/src/graphics/irr_driver.cpp +++ b/src/graphics/irr_driver.cpp @@ -62,6 +62,7 @@ #include "physics/physics.hpp" #include "scriptengine/property_animator.hpp" #include "states_screens/dialogs/confirm_resolution_dialog.hpp" +#include "states_screens/networking_lobby.hpp" #include "states_screens/state_manager.hpp" #include "tracks/track_manager.hpp" #include "tracks/track.hpp" @@ -1715,9 +1716,9 @@ void IrrDriver::displayFPS() no_trust--; static video::SColor fpsColor = video::SColor(255, 0, 0, 0); - font->draw( L"FPS: ...", core::rect< s32 >(100,0,400,50), fpsColor, - false ); - + font->draw(StringUtils::insertValues (L"FPS: ... Ping: %dms", + NetworkingLobby::getInstance()->getServerPing()), + core::rect< s32 >(100,0,400,50), fpsColor, false); return; } @@ -1738,22 +1739,26 @@ void IrrDriver::displayFPS() { fps_string = StringUtils::insertValues (L"FPS: %d/%d/%d - PolyCount: %d Solid, " - "%d Shadows - LightDist : %d, Total skinning joints: %d", + "%d Shadows - LightDist : %d, Total skinning joints: %d, " + "Ping: %dms", min, fps, max, SP::sp_solid_poly_count, SP::sp_shadow_poly_count, m_last_light_bucket_distance, - m_skinning_joint); + m_skinning_joint, + NetworkingLobby::getInstance()->getServerPing()); } else { if (CVS->isGLSL()) { - fps_string = _("FPS: %d/%d/%d - %d KTris", min, fps, max, - SP::sp_solid_poly_count / 1000); + fps_string = _("FPS: %d/%d/%d - %d KTris, Ping: %dms", min, fps, + max, SP::sp_solid_poly_count / 1000, + NetworkingLobby::getInstance()->getServerPing()); } else { - fps_string = _("FPS: %d/%d/%d - %d KTris", min, fps, max, - (int)roundf(kilotris)); + fps_string = _("FPS: %d/%d/%d - %d KTris, Ping: %dms", min, fps, + max, (int)roundf(kilotris), + NetworkingLobby::getInstance()->getServerPing()); } } diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index cf09793de..7a9e10666 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -1022,6 +1022,10 @@ void ServerLobby::connectionRequested(Event* event) server_info->addUInt8(LE_SERVER_INFO); m_game_setup->addServerInfo(server_info); peer->sendPacket(server_info); + // Make sure it will always ping at least the frequency of state exchange + // so enet will not ping when we exchange state but keep ping elsewhere + // then in lobby the ping seen will be correct + peer->setPingInterval(110); delete server_info; m_peers_ready[peer] = std::make_pair(false, 0.0); diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 380a4cea6..70321003a 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -716,6 +716,14 @@ void STKHost::mainLoop() while (m_exit_timeout.load() > StkTime::getRealTime()) { + auto server_peer = getServerPeerForClient(); + if (!is_server && server_peer && + StkTime::getRealTime() - server_peer->getConnectedTime() > 3.0) + { + // Back to default ping interval for client + server_peer->setPingInterval(0); + } + auto sl = LobbyProtocol::get(); if (direct_socket && sl && sl->waitingForPlayers()) { @@ -785,6 +793,12 @@ void STKHost::mainLoop() { auto stk_peer = std::make_shared (event.peer, this, m_next_unique_host_id++); + if (!is_server) + { + // This allow client to get the correct ping as fast as + // possible + stk_peer->setPingInterval(10); + } std::unique_lock lock(m_peers_mutex); m_peers[event.peer] = stk_peer; lock.unlock(); diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index 400a0caec..37d5055d4 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -37,6 +37,7 @@ STKPeer::STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id) m_host_id = host_id; m_connected_time = (float)StkTime::getRealTime(); m_token_set.store(false); + setPingInterval(10); m_client_server_token.store(0); } // STKPeer @@ -124,12 +125,12 @@ bool STKPeer::isSamePeer(const ENetPeer* peer) const } // isSamePeer //----------------------------------------------------------------------------- -/** Returns the ping to this peer from host, it waits for 15 seconds for a +/** Returns the ping to this peer from host, it waits for 3 seconds for a * stable ping returned by enet measured in ms. */ uint32_t STKPeer::getPing() const { - if ((float)StkTime::getRealTime() - m_connected_time < 15.0f) + if ((float)StkTime::getRealTime() - m_connected_time < 3.0f) return 0; - return m_enet_peer->lastRoundTripTime; + return m_enet_peer->roundTripTime; } // getPing diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index 9c6793372..1a96d3f8e 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -164,6 +164,9 @@ public: } } // ------------------------------------------------------------------------ + void setPingInterval(uint32_t interval) + { enet_peer_ping_interval(m_enet_peer, interval); } + // ------------------------------------------------------------------------ uint32_t getPing() const; }; // STKPeer diff --git a/src/states_screens/networking_lobby.cpp b/src/states_screens/networking_lobby.cpp index e553dd213..d37bdd3e3 100644 --- a/src/states_screens/networking_lobby.cpp +++ b/src/states_screens/networking_lobby.cpp @@ -240,16 +240,23 @@ void NetworkingLobby::onUpdate(float delta) { m_start_button->setVisible(true); } - if (auto p = m_server_peer.lock()) - { - //I18N: In the networking lobby, display ping when connected - const uint32_t ping = p->getPing(); - if (ping != 0) - m_header->setText(_("Lobby (ping: %dms)", ping), false); - } + //I18N: In the networking lobby, display ping when connected + const uint32_t ping = getServerPing(); + if (ping != 0) + m_header->setText(_("Lobby (ping: %dms)", ping), false); } } // onUpdate +// ---------------------------------------------------------------------------- +uint32_t NetworkingLobby::getServerPing() const +{ + if (auto p = m_server_peer.lock()) + { + return p->getPing(); + } + return 0; +} // getServerPing + // ---------------------------------------------------------------------------- void NetworkingLobby::sendChat(irr::core::stringw text) { diff --git a/src/states_screens/networking_lobby.hpp b/src/states_screens/networking_lobby.hpp index 541e76857..db7b5088d 100644 --- a/src/states_screens/networking_lobby.hpp +++ b/src/states_screens/networking_lobby.hpp @@ -125,7 +125,7 @@ public: int/*icon id*/> >& p); void addSplitscreenPlayer(irr::core::stringw name); void cleanAddedPlayers(); - + uint32_t getServerPing() const; }; // class NetworkingLobby #endif