Ignore the first 15 seconds for ping result

This commit is contained in:
Benau 2018-03-24 08:20:27 +08:00
parent 076fb3273f
commit c3ea7f1088
2 changed files with 15 additions and 7 deletions

View File

@ -37,6 +37,7 @@ STKPeer::STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id)
m_client_server_token = 0;
m_host_id = host_id;
m_token_set = false;
m_connected_time = (float)StkTime::getRealTime();
} // STKPeer
//-----------------------------------------------------------------------------
@ -121,3 +122,14 @@ bool STKPeer::isSamePeer(const ENetPeer* peer) const
{
return peer==m_enet_peer;
} // isSamePeer
//-----------------------------------------------------------------------------
/** Returns the ping to this peer from host, it waits for 15 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)
return 0;
return m_enet_peer->lastRoundTripTime;
} // getPing

View File

@ -69,6 +69,8 @@ protected:
std::vector<std::shared_ptr<NetworkPlayerProfile> > m_players;
float m_connected_time;
public:
STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id);
~STKPeer() {}
@ -118,13 +120,7 @@ public:
/** Returns the host id of this peer. */
uint32_t getHostId() const { return m_host_id; }
// ------------------------------------------------------------------------
/** Returns the ping to this peer from host, if variance is too large
* return 0. */
uint32_t getPing() const
{
return m_enet_peer->roundTripTimeVariance > 20 ?
0 : m_enet_peer->roundTripTime;
}
uint32_t getPing() const;
}; // STKPeer