mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-19 22:23:51 +00:00
[Master] Add isServerValid lambda. Add "OnServerAnnounce" callback
This commit is contained in:
parent
f99dafbf51
commit
dc18916f46
1 changed files with 39 additions and 4 deletions
|
@ -47,6 +47,11 @@ MasterServer::MasterServer(const std::string &luaScript)
|
|||
state.set_function("BanAddress", [this](const string &address) {
|
||||
this->ban(address);
|
||||
});
|
||||
state.new_usertype<MasterServer::SServer>("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<bool>();
|
||||
else
|
||||
cerr << "Error: " << result.get<string>() << endl;
|
||||
});
|
||||
return ret;
|
||||
};
|
||||
|
||||
if (iter != servers.end())
|
||||
{
|
||||
if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_DELETE)
|
||||
|
@ -185,10 +208,19 @@ void MasterServer::Thread()
|
|||
else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE)
|
||||
{
|
||||
cout << "Updated";
|
||||
|
||||
if (isServerValid(server))
|
||||
{
|
||||
iter->second = server;
|
||||
keepAliveFunc();
|
||||
}
|
||||
else
|
||||
{
|
||||
servers.erase(iter);
|
||||
pendingACKs[packet->guid] = steady_clock::now();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Keeping alive";
|
||||
keepAliveFunc();
|
||||
|
@ -197,9 +229,12 @@ void MasterServer::Thread()
|
|||
else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE)
|
||||
{
|
||||
cout << "Added";
|
||||
if (isServerValid(server))
|
||||
{
|
||||
iter = servers.insert({packet->systemAddress, server}).first;
|
||||
keepAliveFunc();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Unknown";
|
||||
|
|
Loading…
Reference in a new issue