Offer a way to know accurate ping to server within 3 seconds

This commit is contained in:
Benau 2018-05-22 01:31:08 +08:00
parent 34b8a07d80
commit 1a975f9ef4
7 changed files with 54 additions and 20 deletions

View File

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

View File

@ -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);

View File

@ -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<ServerLobby>();
if (direct_socket && sl && sl->waitingForPlayers())
{
@ -785,6 +793,12 @@ void STKHost::mainLoop()
{
auto stk_peer = std::make_shared<STKPeer>
(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<std::mutex> lock(m_peers_mutex);
m_peers[event.peer] = stk_peer;
lock.unlock();

View File

@ -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

View File

@ -164,6 +164,9 @@ public:
}
}
// ------------------------------------------------------------------------
void setPingInterval(uint32_t interval)
{ enet_peer_ping_interval(m_enet_peer, interval); }
// ------------------------------------------------------------------------
uint32_t getPing() const;
}; // STKPeer

View File

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

View File

@ -125,7 +125,7 @@ public:
int/*icon id*/> >& p);
void addSplitscreenPlayer(irr::core::stringw name);
void cleanAddedPlayers();
uint32_t getServerPing() const;
}; // class NetworkingLobby
#endif