Add mean packet loss info of peers to database

This commit is contained in:
Benau 2019-10-27 23:43:51 +08:00
parent e9b5fb2c81
commit 1904e37ff1
4 changed files with 18 additions and 5 deletions

View File

@ -283,7 +283,8 @@ void ServerLobby::initServerStatsTable()
" version TEXT NOT NULL, -- SuperTuxKart version of the host (with OS info)\n"
" connected_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Time when connected\n"
" disconnected_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Time when disconnected (saved when disconnected)\n"
" ping INTEGER UNSIGNED NOT NULL DEFAULT 0 -- Ping of the host\n"
" ping INTEGER UNSIGNED NOT NULL DEFAULT 0, -- Ping of the host\n"
" packet_loss INTEGER NOT NULL DEFAULT 0 -- Mean packet loss count from ENet (saved when disconnected)\n"
") WITHOUT ROWID;";
std::string query = oss.str();
sqlite3_stmt* stmt = NULL;
@ -337,7 +338,7 @@ void ServerLobby::initServerStatsTable()
oss << " port, online_id, username, player_num,\n"
<< " " << m_server_stats_table << ".country_code AS country_code, country_flag, country_name, version,\n"
<< " ROUND((STRFTIME(\"%s\", disconnected_time) - STRFTIME(\"%s\", connected_time)) / 60.0, 2) AS time_played,\n"
<< " connected_time, disconnected_time, ping FROM " << m_server_stats_table << "\n"
<< " connected_time, disconnected_time, ping, packet_loss FROM " << m_server_stats_table << "\n"
<< " LEFT JOIN " << country_table_name << " ON "
<< country_table_name << ".country_code = " << m_server_stats_table << ".country_code\n"
<< " ORDER BY connected_time DESC;";
@ -396,7 +397,7 @@ void ServerLobby::initServerStatsTable()
if (ServerConfig::m_ipv6_server)
oss << " a.ipv6,";
oss << " a.port, a.player_num,\n"
<< " a.country_code, a.country_flag, a.country_name, a.version, a.ping,\n"
<< " a.country_code, a.country_flag, a.country_name, a.version, a.ping, a.packet_loss,\n"
<< " b.num_connections, b.first_connected_time, b.first_disconnected_time,\n"
<< " a.connected_time AS last_connected_time, a.disconnected_time AS last_disconnected_time,\n"
<< " a.time_played AS last_time_played, b.total_time_played, b.average_time_played,\n"
@ -486,9 +487,11 @@ void ServerLobby::writeDisconnectInfoTable(STKPeer* peer)
if (m_server_stats_table.empty())
return;
std::string query = StringUtils::insertValues(
"UPDATE %s SET disconnected_time = datetime('now'), ping = %d "
"UPDATE %s SET disconnected_time = datetime('now'), "
"ping = %d, packet_loss = %d "
"WHERE host_id = %u;", m_server_stats_table.c_str(),
peer->getAveragePing(), peer->getHostId());
peer->getAveragePing(), peer->getPacketLoss(),
peer->getHostId());
easySQLQuery(query);
#endif
} // writeDisconnectInfoTable

View File

@ -971,6 +971,9 @@ void STKHost::mainLoop()
{
m_peer_pings.getData()[p.second->getHostId()] =
p.second->getPing();
// Set packet loss before enet command, so if the peer is
// disconnected later the loss won't be cleared
p.second->setPacketLoss(p.first->packetLoss);
const unsigned ap = p.second->getAveragePing();
const unsigned max_ping = ServerConfig::m_max_ping;
if (p.second->isValidated() &&

View File

@ -45,6 +45,7 @@ STKPeer::STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id)
m_connected_time = StkTime::getMonoTimeMs();
m_validated.store(false);
m_average_ping.store(0);
m_packet_loss.store(0);
m_waiting_for_game.store(true);
m_spectator.store(false);
m_disconnected.store(false);

View File

@ -96,6 +96,8 @@ protected:
std::atomic<uint32_t> m_average_ping;
std::atomic<int> m_packet_loss;
std::set<unsigned> m_available_kart_ids;
std::string m_user_version;
@ -250,6 +252,10 @@ public:
{ return m_client_capabilities; }
// ------------------------------------------------------------------------
bool isAIPeer() const { return m_user_version == "AI"; }
// ------------------------------------------------------------------------
void setPacketLoss(int loss) { m_packet_loss.store(loss); }
// ------------------------------------------------------------------------
int getPacketLoss() const { return m_packet_loss.load(); }
}; // STKPeer
#endif // STK_PEER_HPP