Fix discovery.

This commit is contained in:
momo5502 2016-01-04 03:27:43 +01:00
parent 4765dd24fc
commit 8175888e1d
4 changed files with 50 additions and 22 deletions

View File

@ -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;
}
}
}

View File

@ -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;
};
}

View File

@ -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++)

View File

@ -23,7 +23,7 @@
#include <locale>
#include <regex>
#include <thread>
#include <future>
#include <chrono>
#include <version.hpp>