Skip to content

Commit

Permalink
Update the serverscanner API for later improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
daid committed Aug 22, 2024
1 parent 80351a2 commit 4070cbb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
19 changes: 10 additions & 9 deletions src/multiplayer_server_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void ServerScanner::scanMasterServer(string url)
for(unsigned int n=0; n<server_list.size(); n++)
{
if (removedServerCallback)
removedServerCallback(server_list[n].address);
removedServerCallback(server_list[n]);
server_list.erase(server_list.begin() + n);
n--;
}
Expand All @@ -57,7 +57,7 @@ void ServerScanner::scanLocalNetwork()
for(unsigned int n=0; n<server_list.size(); n++)
{
if (removedServerCallback)
removedServerCallback(server_list[n].address);
removedServerCallback(server_list[n]);
server_list.erase(server_list.begin() + n);
n--;
}
Expand All @@ -82,7 +82,7 @@ void ServerScanner::update(float /*gameDelta*/)
if (server_list[n].timeout.isExpired())
{
if (removedServerCallback)
removedServerCallback(server_list[n].address);
removedServerCallback(server_list[n]);
server_list.erase(server_list.begin() + n);
n--;
}
Expand All @@ -108,19 +108,19 @@ void ServerScanner::update(float /*gameDelta*/)
recv_packet >> verification >> version_nr >> name;
if (verification == multiplayerVerficationNumber && (version_nr == version_number || version_nr == 0 || version_number == 0))
{
updateServerEntry(recv_address, recv_port, name);
updateServerEntry(ServerType::LAN, recv_address, recv_port, name);
}
}
}
}

void ServerScanner::updateServerEntry(sp::io::network::Address address, int port, string name)
void ServerScanner::updateServerEntry(ServerType type, sp::io::network::Address address, int port, string name)
{
std::lock_guard<std::mutex> guard(server_list_mutex);

for(unsigned int n=0; n<server_list.size(); n++)
{
if (server_list[n].address == address)
if (server_list[n].type == type && server_list[n].address == address)
{
server_list[n].port = port;
server_list[n].name = name;
Expand All @@ -131,17 +131,18 @@ void ServerScanner::updateServerEntry(sp::io::network::Address address, int port

LOG(INFO) << "ServerScanner::New server: " << address.getHumanReadable()[0] << " " << port << " " << name;
ServerInfo si;
si.type = type;
si.address = address;
si.port = port;
si.name = name;
si.timeout.start(ServerTimeout);
server_list.push_back(si);

if (newServerCallback)
newServerCallback(address, name);
newServerCallback(si);
}

void ServerScanner::addCallbacks(std::function<void(sp::io::network::Address, string)> newServerCallbackIn, std::function<void(sp::io::network::Address)> removedServerCallbackIn)
void ServerScanner::addCallbacks(std::function<void(const ServerInfo&)> newServerCallbackIn, std::function<void(const ServerInfo&)> removedServerCallbackIn)
{
this->newServerCallback = newServerCallbackIn;
this->removedServerCallback = removedServerCallbackIn;
Expand Down Expand Up @@ -208,7 +209,7 @@ void ServerScanner::masterServerScanThread()

if (version == version_number || version == 0 || version_number == 0)
{
updateServerEntry(address, part_port, name);
updateServerEntry(ServerType::MasterServer, address, part_port, name);
}
}
}
Expand Down
45 changes: 25 additions & 20 deletions src/multiplayer_server_scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,29 @@
//Class to find all servers that have the correct version number. Creates a big nice list.
class ServerScanner : public Updatable
{
int server_port;
std::unique_ptr<sp::io::network::UdpSocket> socket;
sp::SystemTimer broadcast_timer;

public:
enum class ServerType {
LAN,
MasterServer,
SteamFriend,
};
struct ServerInfo
{
ServerType type;
sp::io::network::Address address;
int port;
string name;

sp::SystemTimer timeout;
};
private:
std::vector<struct ServerInfo> server_list;
int version_number;
constexpr static float BroadcastTimeout = 2.0f;
constexpr static float ServerTimeout = 30.0f;

string master_server_url;
std::mutex server_list_mutex;
std::thread master_server_scan_thread;
std::condition_variable abort_wait;

std::function<void(sp::io::network::Address, string)> newServerCallback;
std::function<void(sp::io::network::Address)> removedServerCallback;
public:

ServerScanner(int version_number, int server_port = defaultServerPort);
virtual ~ServerScanner();

virtual void destroy() override;

virtual void update(float delta) override;
void addCallbacks(std::function<void(sp::io::network::Address, string)> newServerCallback, std::function<void(sp::io::network::Address)> removedServerCallback);
void addCallbacks(std::function<void(const ServerInfo&)> newServerCallback, std::function<void(const ServerInfo&)> removedServerCallback);

void scanLocalNetwork();
void scanMasterServer(string url);
Expand All @@ -55,7 +43,24 @@ class ServerScanner : public Updatable
private:
void masterServerScanThread();

void updateServerEntry(sp::io::network::Address address, int port, string name);
void updateServerEntry(ServerType type, sp::io::network::Address address, int port, string name);

int server_port;
std::unique_ptr<sp::io::network::UdpSocket> socket;
sp::SystemTimer broadcast_timer;

std::vector<struct ServerInfo> server_list;
int version_number;
constexpr static float BroadcastTimeout = 2.0f;
constexpr static float ServerTimeout = 30.0f;

string master_server_url;
std::mutex server_list_mutex;
std::thread master_server_scan_thread;
std::condition_variable abort_wait;

std::function<void(const ServerInfo&)> newServerCallback;
std::function<void(const ServerInfo&)> removedServerCallback;
};

#endif//MULTIPLAYER_SERVER_SCANER_H

0 comments on commit 4070cbb

Please sign in to comment.