[Node] Fragment the nodeListResponse send messages

This commit is contained in:
INeedBots 2020-11-17 01:06:02 -06:00
parent 7aaa64f02d
commit e5a45023f2
2 changed files with 38 additions and 13 deletions

View File

@ -279,25 +279,48 @@ namespace Components
void Node::SendList(Network::Address address) void Node::SendList(Network::Address address)
{ {
Proto::Node::List list;
list.set_isnode(Dedicated::IsEnabled());
list.set_protocol(PROTOCOL);
list.set_port(Node::GetPort());
std::lock_guard<std::recursive_mutex> _(Node::Mutex); std::lock_guard<std::recursive_mutex> _(Node::Mutex);
for (auto& node : Node::Nodes) // need to keep the message size below 1404 bytes else recipient will just drop it
{ std::vector<std::string> nodeListReponseMessages;
if (node.isValid())
{
std::string* str = list.add_nodes();
sockaddr addr = node.address.getSockAddr(); for (size_t curNode = 0; curNode < Node::Nodes.size();)
str->append(reinterpret_cast<char*>(&addr), sizeof(addr)); {
Proto::Node::List list;
list.set_isnode(Dedicated::IsEnabled());
list.set_protocol(PROTOCOL);
list.set_port(Node::GetPort());
for (size_t i = 0; i < NODE_MAX_NODES_TO_SEND;)
{
if (curNode >= Node::Nodes.size())
break;
auto node = Node::Nodes.at(curNode++);
if (node.isValid())
{
std::string* str = list.add_nodes();
sockaddr addr = node.address.getSockAddr();
str->append(reinterpret_cast<char*>(&addr), sizeof(addr));
i++;
}
} }
nodeListReponseMessages.push_back(list.SerializeAsString());
} }
Session::Send(address, "nodeListResponse", list.SerializeAsString()); size_t i = 0;
for (auto& nodeListData : nodeListReponseMessages)
{
Scheduler::OnDelay([nodeListData, i, address]()
{
NODE_LOG("Sending %d nodeListResponse length to %s\n", nodeListData.length(), address.getCString());
Session::Send(address, "nodeListResponse", nodeListData);
}, NODE_SEND_RATE * i++);
}
} }
unsigned short Node::GetPort() unsigned short Node::GetPort()

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#define NODE_HALFLIFE (3 * 60 * 1000) //3min #define NODE_HALFLIFE (3 * 60 * 1000) //3min
#define NODE_MAX_NODES_TO_SEND 64
#define NODE_SEND_RATE 500ms
#ifdef NODE_LOG_MESSAGES #ifdef NODE_LOG_MESSAGES
#define NODE_LOG(x, ...) Logger::Print(x, __VA_ARGS__) #define NODE_LOG(x, ...) Logger::Print(x, __VA_ARGS__)