2022-02-27 07:53:44 -05:00
|
|
|
#include <STDInclude.hpp>
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
bool Discovery::IsTerminating = false;
|
|
|
|
bool Discovery::IsPerforming = false;
|
|
|
|
std::thread Discovery::Thread;
|
|
|
|
std::string Discovery::Challenge;
|
|
|
|
|
|
|
|
void Discovery::Perform()
|
|
|
|
{
|
|
|
|
Discovery::IsPerforming = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Discovery::Discovery()
|
|
|
|
{
|
2022-03-08 07:20:28 -05:00
|
|
|
Dvar::Register<int>("net_discoveryPortRangeMin", 25000, 0, 65535, Game::dvar_flag::DVAR_ARCHIVE, "Minimum scan range port for local server discovery");
|
|
|
|
Dvar::Register<int>("net_discoveryPortRangeMax", 35000, 1, 65536, Game::dvar_flag::DVAR_ARCHIVE, "Maximum scan range port for local server discovery");
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
// An additional thread prevents lags
|
|
|
|
// Not sure if that's the best way though
|
|
|
|
Discovery::IsPerforming = false;
|
|
|
|
Discovery::IsTerminating = false;
|
2017-01-25 08:34:53 -05:00
|
|
|
Discovery::Thread = std::thread([]()
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2017-01-25 08:34:53 -05:00
|
|
|
while (!Discovery::IsTerminating)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2017-01-25 08:34:53 -05:00
|
|
|
if (Discovery::IsPerforming)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2017-01-25 08:34:53 -05:00
|
|
|
int start = Game::Sys_Milliseconds();
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2017-01-25 08:34:53 -05:00
|
|
|
Logger::Print("Starting local server discovery...\n");
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2017-01-25 08:34:53 -05:00
|
|
|
Discovery::Challenge = Utils::Cryptography::Rand::GenerateChallenge();
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2017-01-25 08:34:53 -05:00
|
|
|
unsigned int minPort = Dvar::Var("net_discoveryPortRangeMin").get<unsigned int>();
|
|
|
|
unsigned int maxPort = Dvar::Var("net_discoveryPortRangeMax").get<unsigned int>();
|
|
|
|
Network::BroadcastRange(minPort, maxPort, Utils::String::VA("discovery %s", Discovery::Challenge.data()));
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2022-06-12 17:07:53 -04:00
|
|
|
Logger::Print("Discovery sent within {}ms, awaiting responses...\n", Game::Sys_Milliseconds() - start);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2017-01-25 08:34:53 -05:00
|
|
|
Discovery::IsPerforming = false;
|
2017-01-22 14:12:36 -05:00
|
|
|
}
|
2017-01-25 08:34:53 -05:00
|
|
|
|
|
|
|
std::this_thread::sleep_for(50ms);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-20 18:12:46 -04:00
|
|
|
Network::OnPacket("discovery", [](Network::Address& address, [[maybe_unused]] const std::string& data)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
if (address.isSelf()) return;
|
|
|
|
|
|
|
|
if (!address.isLocal())
|
|
|
|
{
|
2022-06-12 17:07:53 -04:00
|
|
|
Logger::Print("Received discovery request from non-local address: {}\n", address.getCString());
|
2017-01-19 16:23:59 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-12 17:07:53 -04:00
|
|
|
Logger::Print("Received discovery request from {}\n", address.getCString());
|
2017-01-19 16:23:59 -05:00
|
|
|
Network::SendCommand(address, "discoveryResponse", data);
|
|
|
|
});
|
|
|
|
|
2022-05-20 18:12:46 -04:00
|
|
|
Network::OnPacket("discoveryResponse", [](Network::Address& address, [[maybe_unused]] const std::string& data)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
if (address.isSelf()) return;
|
|
|
|
|
|
|
|
if (!address.isLocal())
|
|
|
|
{
|
2022-06-12 17:07:53 -04:00
|
|
|
Logger::Print("Received discovery response from non-local address: {}\n", address.getCString());
|
2017-01-19 16:23:59 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Utils::ParseChallenge(data) != Discovery::Challenge)
|
|
|
|
{
|
2022-06-12 17:07:53 -04:00
|
|
|
Logger::Print("Received discovery with invalid challenge from: {}\n", address.getCString());
|
2017-01-19 16:23:59 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-12 17:07:53 -04:00
|
|
|
Logger::Print("Received discovery response from: {}\n", address.getCString());
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
if (ServerList::IsOfflineList())
|
|
|
|
{
|
2017-02-28 13:58:03 -05:00
|
|
|
ServerList::InsertRequest(address);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:06:50 -05:00
|
|
|
void Discovery::preDestroy()
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
Discovery::IsPerforming = false;
|
|
|
|
Discovery::IsTerminating = true;
|
|
|
|
|
|
|
|
if (Discovery::Thread.joinable())
|
|
|
|
{
|
|
|
|
Discovery::Thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|