Use a fixed 10 packets per second for ping packet

This commit is contained in:
Benau 2018-12-21 22:53:28 +08:00
parent 86957c9d5e
commit c46a5eafdb
3 changed files with 7 additions and 8 deletions

View File

@ -19,7 +19,6 @@
#ifndef HEADER_NETWORK_TIMER_SYNCHRONIZER_HPP #ifndef HEADER_NETWORK_TIMER_SYNCHRONIZER_HPP
#define HEADER_NETWORK_TIMER_SYNCHRONIZER_HPP #define HEADER_NETWORK_TIMER_SYNCHRONIZER_HPP
#include "config/user_config.hpp"
#include "network/stk_host.hpp" #include "network/stk_host.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
#include "utils/time.hpp" #include "utils/time.hpp"
@ -70,8 +69,8 @@ public:
const uint64_t cur_time = StkTime::getRealTimeMs(); const uint64_t cur_time = StkTime::getRealTimeMs();
// Discard too close time compared to last ping // Discard too close time compared to last ping
// (due to resend when packet loss) // (due to resend when packet loss)
const uint64_t frequency = (uint64_t)((1.0f / // 10 packets per second as seen in STKHost
(float)(stk_config->m_network_state_frequeny)) * 1000.0f) / 2; const uint64_t frequency = (uint64_t)((1.0f / 10.0f) * 1000.0f) / 2;
if (!m_times.empty() && if (!m_times.empty() &&
cur_time - std::get<2>(m_times.back()) < frequency) cur_time - std::get<2>(m_times.back()) < frequency)
return; return;

View File

@ -735,11 +735,10 @@ void STKHost::mainLoop()
if (sl && (!sl->isRacing() || sl->allowJoinedPlayersWaiting()) && if (sl && (!sl->isRacing() || sl->allowJoinedPlayersWaiting()) &&
last_ping_time < StkTime::getRealTimeMs()) last_ping_time < StkTime::getRealTimeMs())
{ {
// If not racing, send an reliable packet at the same rate with // If not racing, send an reliable packet at the 10 packets
// state exchange to keep enet ping accurate // per second, which is for accurate ping calculation by enet
last_ping_time = StkTime::getRealTimeMs() + last_ping_time = StkTime::getRealTimeMs() +
(uint64_t)((1.0f / (uint64_t)((1.0f / 10.0f) * 1000.0f);
(float)(stk_config->m_network_state_frequeny)) * 1000.0f);
need_ping = true; need_ping = true;
} }

View File

@ -178,7 +178,8 @@ uint32_t STKPeer::getPing()
if (NetworkConfig::get()->isServer()) if (NetworkConfig::get()->isServer())
{ {
// Average ping in 5 seconds // Average ping in 5 seconds
const unsigned ap = stk_config->m_network_state_frequeny * 5; // Frequency is 10 packets per second as seen in STKHost
const unsigned ap = 10 * 5;
m_previous_pings.push_back(m_enet_peer->roundTripTime); m_previous_pings.push_back(m_enet_peer->roundTripTime);
while (m_previous_pings.size() > ap) while (m_previous_pings.size() > ap)
{ {