From dc18916f46fff964a3ade2936655ac3b3712f497 Mon Sep 17 00:00:00 2001 From: Koncord Date: Tue, 12 Sep 2017 21:29:42 +0800 Subject: [PATCH] [Master] Add isServerValid lambda. Add "OnServerAnnounce" callback --- apps/master/MasterServer.cpp | 43 ++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/apps/master/MasterServer.cpp b/apps/master/MasterServer.cpp index 301e59c29..27bba07d9 100644 --- a/apps/master/MasterServer.cpp +++ b/apps/master/MasterServer.cpp @@ -47,6 +47,11 @@ MasterServer::MasterServer(const std::string &luaScript) state.set_function("BanAddress", [this](const string &address) { this->ban(address); }); + state.new_usertype("Server", + "name", sol::property(&MasterServer::SServer::GetName), + "gamemode", sol::property(&MasterServer::SServer::GetGameMode), + "version", sol::property(&MasterServer::SServer::GetVersion) + ); state.set_function("UnbanAddress", [this](const string &address) { this->unban(address); @@ -173,6 +178,24 @@ void MasterServer::Thread() pendingACKs[packet->guid] = steady_clock::now(); }; + auto isServerValid = [&](const SServer &sserver) { + bool ret = false; + auto addr = packet->systemAddress.ToString(false); + + if (find(banned.begin(), banned.end(), addr) != banned.end()) // check if address is banned + return true; + + luaStuff([&ret, &packet, &sserver, &addr](sol::state &state) { + sol::protected_function func = state["OnServerAnnounce"]; + sol::protected_function_result result = func.call(addr, sserver); + if (result.valid()) + ret = result.get(); + else + cerr << "Error: " << result.get() << endl; + }); + return ret; + }; + if (iter != servers.end()) { if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_DELETE) @@ -185,8 +208,17 @@ void MasterServer::Thread() else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE) { cout << "Updated"; - iter->second = server; - keepAliveFunc(); + + if (isServerValid(server)) + { + iter->second = server; + keepAliveFunc(); + } + else + { + servers.erase(iter); + pendingACKs[packet->guid] = steady_clock::now(); + } } else { @@ -197,8 +229,11 @@ void MasterServer::Thread() else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE) { cout << "Added"; - iter = servers.insert({packet->systemAddress, server}).first; - keepAliveFunc(); + if (isServerValid(server)) + { + iter = servers.insert({packet->systemAddress, server}).first; + keepAliveFunc(); + } } else {