From c17cb8a4f713513d0d6e79322c1ae014580d0a42 Mon Sep 17 00:00:00 2001 From: otavepto Date: Sat, 30 Mar 2024 07:58:35 +0200 Subject: [PATCH] in gameserver_stats: remove & timeout pending requests if the user disconnected --- dll/dll/steam_gameserverstats.h | 4 +++ dll/steam_gameserverstats.cpp | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/dll/dll/steam_gameserverstats.h b/dll/dll/steam_gameserverstats.h index e38a0fd0..2fbab4fc 100644 --- a/dll/dll/steam_gameserverstats.h +++ b/dll/dll/steam_gameserverstats.h @@ -65,6 +65,10 @@ class Steam_GameServerStats : public ISteamGameServerStats void network_callback_updated_stats(Common_Message *msg); void network_callback(Common_Message *msg); + // user connect/disconnect + void network_callback_low_level(Common_Message *msg); + + static void steam_gameserverstats_network_low_level(void *object, Common_Message *msg); static void steam_gameserverstats_network_callback(void *object, Common_Message *msg); static void steam_gameserverstats_run_every_runcb(void *object); diff --git a/dll/steam_gameserverstats.cpp b/dll/steam_gameserverstats.cpp index 3922dc3b..fd06afa4 100644 --- a/dll/steam_gameserverstats.cpp +++ b/dll/steam_gameserverstats.cpp @@ -21,6 +21,14 @@ #define PENDING_RequestUserStats_TIMEOUT 7.0 +void Steam_GameServerStats::steam_gameserverstats_network_low_level(void *object, Common_Message *msg) +{ + // PRINT_DEBUG("Steam_GameServerStats::steam_gameserverstats_network_low_level\n"); + + auto inst = (Steam_GameServerStats *)object; + inst->network_callback_low_level(msg); +} + void Steam_GameServerStats::steam_gameserverstats_network_callback(void *object, Common_Message *msg) { // PRINT_DEBUG("Steam_GameServerStats::steam_gameserverstats_network_callback\n"); @@ -82,6 +90,7 @@ Steam_GameServerStats::Steam_GameServerStats(class Settings *settings, class Net this->run_every_runcb = run_every_runcb; this->network->setCallback(CALLBACK_ID_GAMESERVER_STATS, settings->get_local_steam_id(), &Steam_GameServerStats::steam_gameserverstats_network_callback, this); + this->network->setCallback(CALLBACK_ID_USER_STATUS, settings->get_local_steam_id(), &Steam_GameServerStats::steam_gameserverstats_network_low_level, this); this->run_every_runcb->add(&Steam_GameServerStats::steam_gameserverstats_run_every_runcb, this); } @@ -89,6 +98,7 @@ Steam_GameServerStats::Steam_GameServerStats(class Settings *settings, class Net Steam_GameServerStats::~Steam_GameServerStats() { this->network->rmCallback(CALLBACK_ID_GAMESERVER_STATS, settings->get_local_steam_id(), &Steam_GameServerStats::steam_gameserverstats_network_callback, this); + this->network->rmCallback(CALLBACK_ID_USER_STATUS, settings->get_local_steam_id(), &Steam_GameServerStats::steam_gameserverstats_network_low_level, this); this->run_every_runcb->remove(&Steam_GameServerStats::steam_gameserverstats_run_every_runcb, this); } @@ -520,3 +530,46 @@ void Steam_GameServerStats::network_callback(Common_Message *msg) break; } } + + +// user connect/disconnect +void Steam_GameServerStats::network_callback_low_level(Common_Message *msg) +{ + uint64 steamid = msg->source_id(); + // this should never happen, but just in case + if (steamid == settings->get_local_steam_id().ConvertToUint64()) return; + + switch (msg->low_level().type()) + { + case Low_Level::CONNECT: + // nothing + break; + + case Low_Level::DISCONNECT: { + all_users_data.erase(steamid); + auto it_rm = std::remove_if( + pending_RequestUserStats.begin(), pending_RequestUserStats.end(), + [=](const RequestAllStats &item) { return item.steamIDUser.ConvertToUint64() == steamid; } + ); + while (pending_RequestUserStats.end() != it_rm) { + GSStatsReceived_t data{}; + data.m_eResult = k_EResultTimeout; + data.m_steamIDUser = it_rm->steamIDUser; + callback_results->addCallResult(it_rm->steamAPICall, data.k_iCallback, &data, sizeof(data)); + + PRINT_DEBUG( + "Steam_GameServerStats::network_callback_low_level RequestUserStats timeout, %llu\n", + it_rm->steamIDUser.ConvertToUint64() + ); + it_rm = pending_RequestUserStats.erase(it_rm); + } + + // PRINT_DEBUG("Steam_GameServerStats::network_callback_low_level removed user %llu\n", steamid); + } + break; + + default: + PRINT_DEBUG("Steam_GameServerStats::network_callback_low_level unknown type %i\n", (int)msg->low_level().type()); + break; + } +}