[Node] Introduce dvar to control server query rate

This commit is contained in:
momo5502 2017-07-12 10:12:51 +02:00
parent 902d40188f
commit d3930ce19a
3 changed files with 32 additions and 9 deletions

View File

@ -39,6 +39,7 @@ namespace Components
Session::Send(this->address, "nodeListRequest");
Node::SendList(this->address);
NODE_LOG("Sent request to %s\n", this->address.getCString());
}
void Node::Entry::reset()
@ -154,7 +155,13 @@ namespace Components
{
if (Dedicated::IsEnabled() && Dvar::Var("sv_lanOnly").get<bool>()) return;
static Utils::Time::Interval frameLimit;
int interval = static_cast<int>(1000.0f / Dvar::Var("net_serverFrames").get<int>());
if (!frameLimit.elapsed(std::chrono::milliseconds(interval))) return;
frameLimit.update();
std::lock_guard<std::recursive_mutex> _(Node::Mutex);
Dvar::Var queryLimit("net_serverQueryLimit");
int sentRequests = 0;
for (auto i = Node::Nodes.begin(); i != Node::Nodes.end();)
@ -164,7 +171,7 @@ namespace Components
i = Node::Nodes.erase(i);
continue;
}
else if (sentRequests < NODE_REQUEST_LIMIT && i->requiresRequest())
else if (sentRequests < queryLimit.get<int>() && i->requiresRequest())
{
++sentRequests;
i->sendRequest();
@ -191,6 +198,8 @@ namespace Components
Proto::Node::List list;
if (!list.ParseFromString(data)) return;
NODE_LOG("Received response from %s\n", address.getCString());
std::lock_guard<std::recursive_mutex> _(Node::Mutex);
for (int i = 0; i < list.nodes_size(); ++i)
@ -207,8 +216,13 @@ namespace Components
{
if (!Dedicated::IsEnabled() && ServerList::IsOnlineList() && list.protocol() == PROTOCOL)
{
NODE_LOG("Inserting %s into the serverlist\n", address.getCString());
ServerList::InsertRequest(address);
}
else
{
NODE_LOG("Dropping serverlist insertion for %s\n", address.getCString());
}
for (auto& node : Node::Nodes)
{

View File

@ -1,7 +1,12 @@
#pragma once
#define NODE_HALFLIFE (3 * 60 * 1000) //3min
#define NODE_REQUEST_LIMIT 3
#ifdef NODE_LOG_MESSAGES
#define NODE_LOG(x, ...) Logger::Print(x, __VA_ARGS__)
#else
#define NODE_LOG(x, ...)
#endif
namespace Components
{

View File

@ -610,6 +610,11 @@ namespace Components
void ServerList::Frame()
{
static Utils::Time::Interval frameLimit;
int interval = static_cast<int>(1000.0f / Dvar::Var("net_serverFrames").get<int>());
if (!frameLimit.elapsed(std::chrono::milliseconds(interval))) return;
frameLimit.update();
std::lock_guard<std::recursive_mutex> _(ServerList::RefreshContainer.mutex);
if (ServerList::RefreshContainer.awatingList)
@ -623,17 +628,15 @@ namespace Components
}
}
// Send requests to 10 servers each frame
int SendServers = 10;
for (unsigned int i = 0; i < ServerList::RefreshContainer.servers.size(); ++i)
int requestLimit = Dvar::Var("net_serverQueryLimit").get<int>();
for (unsigned int i = 0; i < ServerList::RefreshContainer.servers.size() && requestLimit > 0; ++i)
{
ServerList::Container::ServerContainer* server = &ServerList::RefreshContainer.servers[i];
if (server->sent) continue;
// Found server we can send a request to
server->sent = true;
SendServers--;
requestLimit--;
server->sendTime = Game::Sys_Milliseconds();
server->challenge = Utils::Cryptography::Rand::GenerateChallenge();
@ -644,8 +647,6 @@ namespace Components
// Display in the menu, like in COD4
//Localization::Set("MPUI_SERVERQUERIED", Utils::String::VA("Sent requests: %d/%d", ServerList::RefreshContainer.sentCount, ServerList::RefreshContainer.sendCount));
if (SendServers <= 0) break;
}
ServerList::UpdateVisibleInfo();
@ -725,6 +726,9 @@ namespace Components
{
Dvar::Register<bool>("ui_serverSelected", false, Game::dvar_flag::DVAR_FLAG_NONE, "Whether a server has been selected in the serverlist");
Dvar::Register<const char*>("ui_serverSelectedMap", "mp_afghan", Game::dvar_flag::DVAR_FLAG_NONE, "Map of the selected server");
Dvar::Register<int>("net_serverQueryLimit", 3, 1, 10, Dedicated::IsEnabled() ? 0 : Game::dvar_flag::DVAR_FLAG_SAVED, "Amount of server queries per frame");
Dvar::Register<int>("net_serverFrames", 60, 1, 1000, Dedicated::IsEnabled() ? 0 : Game::dvar_flag::DVAR_FLAG_SAVED, "Amount of server query frames per second");
});
// Fix ui_netsource dvar