2016-01-03 13:28:47 -05:00
|
|
|
#include "..\..\STDInclude.hpp"
|
|
|
|
|
2016-01-03 21:27:43 -05:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2016-01-03 13:28:47 -05:00
|
|
|
namespace Components
|
|
|
|
{
|
2016-01-03 21:27:43 -05:00
|
|
|
Discovery::Container Discovery::DiscoveryContainer = { false, false, nullptr };
|
|
|
|
|
2016-01-03 18:00:07 -05:00
|
|
|
void Discovery::Perform()
|
|
|
|
{
|
2016-01-03 21:27:43 -05:00
|
|
|
Discovery::DiscoveryContainer.Perform = true;
|
|
|
|
}
|
2016-01-03 18:00:07 -05:00
|
|
|
|
2016-01-03 21:27:43 -05:00
|
|
|
Discovery::Discovery()
|
|
|
|
{
|
|
|
|
Discovery::DiscoveryContainer.Perform = false;
|
|
|
|
Discovery::DiscoveryContainer.Terminate = false;
|
|
|
|
Discovery::DiscoveryContainer.Thread = new std::thread([] ()
|
2016-01-03 18:00:07 -05:00
|
|
|
{
|
2016-01-03 21:27:43 -05:00
|
|
|
while (!Discovery::DiscoveryContainer.Terminate)
|
|
|
|
{
|
|
|
|
if (Discovery::DiscoveryContainer.Perform)
|
|
|
|
{
|
|
|
|
int start = Game::Com_Milliseconds();
|
|
|
|
|
|
|
|
Logger::Print("Starting local server discovery...\n");
|
2016-01-04 05:32:05 -05:00
|
|
|
|
|
|
|
Discovery::DiscoveryContainer.Challenge = Utils::VA("%d", Game::Com_Milliseconds());
|
|
|
|
|
2016-01-03 21:27:43 -05:00
|
|
|
//Network::BroadcastAll("discovery\n");
|
2016-01-04 05:32:05 -05:00
|
|
|
Network::BroadcastRange(28960, 38960, Utils::VA("discovery\n%s", Discovery::DiscoveryContainer.Challenge.data()));
|
|
|
|
|
2016-01-03 21:27:43 -05:00
|
|
|
Logger::Print("Discovery sent within %dms, awaiting responses...\n", Game::Com_Milliseconds() - start);
|
2016-01-03 18:00:07 -05:00
|
|
|
|
2016-01-03 21:27:43 -05:00
|
|
|
Discovery::DiscoveryContainer.Perform = false;
|
|
|
|
}
|
2016-01-03 18:00:07 -05:00
|
|
|
|
2016-01-03 21:27:43 -05:00
|
|
|
std::this_thread::sleep_for(50ms);
|
|
|
|
}
|
2016-01-03 18:00:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
Network::Handle("discovery", [] (Network::Address address, std::string data)
|
2016-01-03 13:28:47 -05:00
|
|
|
{
|
2016-01-03 18:00:07 -05:00
|
|
|
if (address.IsSelf()) return;
|
|
|
|
|
|
|
|
if (!address.IsLocal())
|
|
|
|
{
|
|
|
|
Logger::Print("Received discovery request from non-local address: %s\n", address.GetString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::Print("Received discovery request from %s\n", address.GetString());
|
2016-01-04 05:32:05 -05:00
|
|
|
Network::Send(address, Utils::VA("discoveryResponse\n%s", data.data()));
|
2016-01-03 18:00:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
Network::Handle("discoveryResponse", [] (Network::Address address, std::string data)
|
|
|
|
{
|
|
|
|
if (address.IsSelf()) return;
|
|
|
|
|
|
|
|
if (!address.IsLocal())
|
|
|
|
{
|
|
|
|
Logger::Print("Received discovery response from non-local address: %s\n", address.GetString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-04 05:32:05 -05:00
|
|
|
if (Utils::ParseChallenge(data) != Discovery::DiscoveryContainer.Challenge)
|
|
|
|
{
|
|
|
|
Logger::Print("Received discovery with invalid challenge from: %s\n", address.GetString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::Print("Received discovery response from: %s\n", address.GetString());
|
2016-01-03 18:00:07 -05:00
|
|
|
|
|
|
|
if (ServerList::IsOfflineList())
|
|
|
|
{
|
|
|
|
ServerList::InsertRequest(address, true);
|
|
|
|
}
|
2016-01-03 13:28:47 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Discovery::~Discovery()
|
|
|
|
{
|
2016-01-03 21:27:43 -05:00
|
|
|
Discovery::DiscoveryContainer.Perform = false;
|
|
|
|
Discovery::DiscoveryContainer.Terminate = true;
|
|
|
|
|
|
|
|
if (Discovery::DiscoveryContainer.Thread)
|
|
|
|
{
|
|
|
|
Discovery::DiscoveryContainer.Thread->join();
|
|
|
|
delete Discovery::DiscoveryContainer.Thread;
|
|
|
|
Discovery::DiscoveryContainer.Thread = nullptr;
|
|
|
|
}
|
2016-01-03 13:28:47 -05:00
|
|
|
}
|
|
|
|
}
|