diff --git a/src/Components/Modules/Discovery.cpp b/src/Components/Modules/Discovery.cpp index 24621995..df552d62 100644 --- a/src/Components/Modules/Discovery.cpp +++ b/src/Components/Modules/Discovery.cpp @@ -1,29 +1,40 @@ #include "..\..\STDInclude.hpp" +using namespace std::literals; + namespace Components { + Discovery::Container Discovery::DiscoveryContainer = { false, false, nullptr }; + void Discovery::Perform() { - static bool performing = false; - if (performing) return; - - std::async([] () - { - performing = true; - int start = Game::Com_Milliseconds(); - - Logger::Print("Starting local server discovery...\n"); - - //Network::BroadcastAll("discovery\n"); - Network::BroadcastRange(28960, 38960, "discovery\n"); - - Logger::Print("Discovery sent within %dms, awaiting responses...\n", Game::Com_Milliseconds() - start); - performing = false; - }); + Discovery::DiscoveryContainer.Perform = true; } Discovery::Discovery() { + Discovery::DiscoveryContainer.Perform = false; + Discovery::DiscoveryContainer.Terminate = false; + Discovery::DiscoveryContainer.Thread = new std::thread([] () + { + while (!Discovery::DiscoveryContainer.Terminate) + { + if (Discovery::DiscoveryContainer.Perform) + { + int start = Game::Com_Milliseconds(); + + Logger::Print("Starting local server discovery...\n"); + //Network::BroadcastAll("discovery\n"); + Network::BroadcastRange(28960, 38960, "discovery\n"); + Logger::Print("Discovery sent within %dms, awaiting responses...\n", Game::Com_Milliseconds() - start); + + Discovery::DiscoveryContainer.Perform = false; + } + + std::this_thread::sleep_for(50ms); + } + }); + Network::Handle("discovery", [] (Network::Address address, std::string data) { if (address.IsSelf()) return; @@ -52,7 +63,6 @@ namespace Components if (ServerList::IsOfflineList()) { - OutputDebugStringA("Inserting!"); ServerList::InsertRequest(address, true); } }); @@ -60,6 +70,14 @@ namespace Components Discovery::~Discovery() { - + Discovery::DiscoveryContainer.Perform = false; + Discovery::DiscoveryContainer.Terminate = true; + + if (Discovery::DiscoveryContainer.Thread) + { + Discovery::DiscoveryContainer.Thread->join(); + delete Discovery::DiscoveryContainer.Thread; + Discovery::DiscoveryContainer.Thread = nullptr; + } } } diff --git a/src/Components/Modules/Discovery.hpp b/src/Components/Modules/Discovery.hpp index 96640e4c..3771225e 100644 --- a/src/Components/Modules/Discovery.hpp +++ b/src/Components/Modules/Discovery.hpp @@ -8,5 +8,15 @@ namespace Components const char* GetName() { return "Discovery"; }; static void Perform(); + + private: + struct Container + { + bool Perform; + bool Terminate; + std::thread* Thread; + }; + + static Container DiscoveryContainer; }; } diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index 972b3722..a0fbc629 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -208,9 +208,6 @@ namespace Components break; } - // Remove server from queue - ServerList::RefreshContainer.Servers.erase(i); - ServerInfo server; server.Hostname = info.Get("hostname"); server.Mapname = info.Get("mapname"); @@ -224,6 +221,9 @@ namespace Components server.Ping = (Game::Com_Milliseconds() - i->SendTime); server.Addr = address; + // Remove server from queue + ServerList::RefreshContainer.Servers.erase(i); + // Check if already inserted and remove int k = 0; for (auto j = ServerList::GetList().begin(); j != ServerList::GetList().end(); j++, k++) diff --git a/src/STDInclude.hpp b/src/STDInclude.hpp index 0b5c2426..bbdea4e2 100644 --- a/src/STDInclude.hpp +++ b/src/STDInclude.hpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include