iw4x-client/src/Components/Modules/Discovery.cpp

84 lines
2.1 KiB
C++
Raw Normal View History

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");
//Network::BroadcastAll("discovery\n");
Network::BroadcastRange(28960, 38960, "discovery\n");
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());
Network::Send(address, "discoveryResponse\n");
});
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;
}
Logger::Print("Received discovery response from %s\n", address.GetString());
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
}
}