From 4070cbb38c63132f66c16401f18b97367975681c Mon Sep 17 00:00:00 2001 From: Daid Date: Thu, 22 Aug 2024 11:23:27 +0200 Subject: [PATCH] Update the serverscanner API for later improvements --- src/multiplayer_server_scanner.cpp | 19 +++++++------ src/multiplayer_server_scanner.h | 45 +++++++++++++++++------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/multiplayer_server_scanner.cpp b/src/multiplayer_server_scanner.cpp index ec9bb910..46f967c3 100644 --- a/src/multiplayer_server_scanner.cpp +++ b/src/multiplayer_server_scanner.cpp @@ -30,7 +30,7 @@ void ServerScanner::scanMasterServer(string url) for(unsigned int n=0; n> verification >> version_nr >> name; if (verification == multiplayerVerficationNumber && (version_nr == version_number || version_nr == 0 || version_number == 0)) { - updateServerEntry(recv_address, recv_port, name); + updateServerEntry(ServerType::LAN, recv_address, recv_port, name); } } } } -void ServerScanner::updateServerEntry(sp::io::network::Address address, int port, string name) +void ServerScanner::updateServerEntry(ServerType type, sp::io::network::Address address, int port, string name) { std::lock_guard guard(server_list_mutex); for(unsigned int n=0; n newServerCallbackIn, std::function removedServerCallbackIn) +void ServerScanner::addCallbacks(std::function newServerCallbackIn, std::function removedServerCallbackIn) { this->newServerCallback = newServerCallbackIn; this->removedServerCallback = removedServerCallbackIn; @@ -208,7 +209,7 @@ void ServerScanner::masterServerScanThread() if (version == version_number || version == 0 || version_number == 0) { - updateServerEntry(address, part_port, name); + updateServerEntry(ServerType::MasterServer, address, part_port, name); } } } diff --git a/src/multiplayer_server_scanner.h b/src/multiplayer_server_scanner.h index ff84eafd..aa29ab16 100644 --- a/src/multiplayer_server_scanner.h +++ b/src/multiplayer_server_scanner.h @@ -11,33 +11,21 @@ //Class to find all servers that have the correct version number. Creates a big nice list. class ServerScanner : public Updatable { - int server_port; - std::unique_ptr socket; - sp::SystemTimer broadcast_timer; - public: + enum class ServerType { + LAN, + MasterServer, + SteamFriend, + }; struct ServerInfo { + ServerType type; sp::io::network::Address address; int port; string name; sp::SystemTimer timeout; }; -private: - 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::thread master_server_scan_thread; - std::condition_variable abort_wait; - - std::function newServerCallback; - std::function removedServerCallback; -public: ServerScanner(int version_number, int server_port = defaultServerPort); virtual ~ServerScanner(); @@ -45,7 +33,7 @@ class ServerScanner : public Updatable virtual void destroy() override; virtual void update(float delta) override; - void addCallbacks(std::function newServerCallback, std::function removedServerCallback); + void addCallbacks(std::function newServerCallback, std::function removedServerCallback); void scanLocalNetwork(); void scanMasterServer(string url); @@ -55,7 +43,24 @@ class ServerScanner : public Updatable private: void masterServerScanThread(); - void updateServerEntry(sp::io::network::Address address, int port, string name); + void updateServerEntry(ServerType type, sp::io::network::Address address, int port, string name); + + int server_port; + std::unique_ptr socket; + sp::SystemTimer broadcast_timer; + + 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::thread master_server_scan_thread; + std::condition_variable abort_wait; + + std::function newServerCallback; + std::function removedServerCallback; }; #endif//MULTIPLAYER_SERVER_SCANER_H