From 2bd015a92d9df4ac7151b4682e00ac9e00755d9f Mon Sep 17 00:00:00 2001 From: Daid Date: Fri, 23 Aug 2024 12:10:20 +0200 Subject: [PATCH] Fix server scanner callbacks being called from a different thread, prepare for steam friend list scanning. Able to scan on LAN and MasterServer a the same time. --- src/multiplayer_server_scanner.cpp | 74 ++++++++---------------------- src/multiplayer_server_scanner.h | 10 ++-- 2 files changed, 25 insertions(+), 59 deletions(-) diff --git a/src/multiplayer_server_scanner.cpp b/src/multiplayer_server_scanner.cpp index 46f967c..4a8eaec 100644 --- a/src/multiplayer_server_scanner.cpp +++ b/src/multiplayer_server_scanner.cpp @@ -19,22 +19,7 @@ void ServerScanner::scanMasterServer(string url) abort_wait.notify_all(); if (master_server_scan_thread.joinable()) return; - LOG(INFO) << "Switching to master server scanning"; - if (socket) - { - socket = nullptr; - } - - { - std::lock_guard guard(server_list_mutex); - for(unsigned int n=0; n(); int port_nr = server_port + 1; @@ -76,7 +45,12 @@ void ServerScanner::scanLocalNetwork() void ServerScanner::update(float /*gameDelta*/) { - server_list_mutex.lock(); + master_server_list_mutex.lock(); + for(const auto& info : master_server_update_list) { + updateServerEntry(info); + } + master_server_update_list.clear(); + master_server_list_mutex.unlock(); for(unsigned int n=0; n> verification >> version_nr >> name; if (verification == multiplayerVerficationNumber && (version_nr == version_number || version_nr == 0 || version_number == 0)) { - updateServerEntry(ServerType::LAN, recv_address, recv_port, name); + updateServerEntry({ServerType::LAN, recv_address, recv_port, name, {}}); } } } } -void ServerScanner::updateServerEntry(ServerType type, sp::io::network::Address address, int port, string name) +void ServerScanner::updateServerEntry(const ServerInfo& info) { - std::lock_guard guard(server_list_mutex); - for(unsigned int n=0; n newServe std::vector ServerScanner::getServerList() { - std::vector ret; - { - std::lock_guard guard(server_list_mutex); - ret = server_list; - } - return ret; + return server_list; } void ServerScanner::masterServerScanThread() @@ -209,7 +171,9 @@ void ServerScanner::masterServerScanThread() if (version == version_number || version == 0 || version_number == 0) { - updateServerEntry(ServerType::MasterServer, address, part_port, name); + master_server_list_mutex.lock(); + master_server_update_list.push_back({ServerType::MasterServer, address, part_port, name, {}}); + master_server_list_mutex.unlock(); } } } diff --git a/src/multiplayer_server_scanner.h b/src/multiplayer_server_scanner.h index aa29ab1..b91e1ae 100644 --- a/src/multiplayer_server_scanner.h +++ b/src/multiplayer_server_scanner.h @@ -13,6 +13,7 @@ class ServerScanner : public Updatable { public: enum class ServerType { + Manual, LAN, MasterServer, SteamFriend, @@ -21,7 +22,7 @@ class ServerScanner : public Updatable { ServerType type; sp::io::network::Address address; - int port; + uint64_t port; string name; sp::SystemTimer timeout; @@ -43,19 +44,20 @@ class ServerScanner : public Updatable private: void masterServerScanThread(); - void updateServerEntry(ServerType type, sp::io::network::Address address, int port, string name); + void updateServerEntry(const ServerInfo& info); int server_port; std::unique_ptr socket; sp::SystemTimer broadcast_timer; - std::vector server_list; + std::vector server_list; int version_number; constexpr static float BroadcastTimeout = 2.0f; constexpr static float ServerTimeout = 30.0f; string master_server_url; - std::mutex server_list_mutex; + std::mutex master_server_list_mutex; + std::vector master_server_update_list; std::thread master_server_scan_thread; std::condition_variable abort_wait;