diff --git a/src/Components/Modules/Node.cpp b/src/Components/Modules/Node.cpp index 083d42a7..5dea692c 100644 --- a/src/Components/Modules/Node.cpp +++ b/src/Components/Modules/Node.cpp @@ -279,25 +279,48 @@ namespace Components 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 _(Node::Mutex); - for (auto& node : Node::Nodes) - { - if (node.isValid()) - { - std::string* str = list.add_nodes(); + // need to keep the message size below 1404 bytes else recipient will just drop it + std::vector nodeListReponseMessages; - sockaddr addr = node.address.getSockAddr(); - str->append(reinterpret_cast(&addr), sizeof(addr)); + for (size_t curNode = 0; curNode < Node::Nodes.size();) + { + 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(&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() diff --git a/src/Components/Modules/Node.hpp b/src/Components/Modules/Node.hpp index c6d84b62..aa7943c3 100644 --- a/src/Components/Modules/Node.hpp +++ b/src/Components/Modules/Node.hpp @@ -1,6 +1,8 @@ #pragma once #define NODE_HALFLIFE (3 * 60 * 1000) //3min +#define NODE_MAX_NODES_TO_SEND 64 +#define NODE_SEND_RATE 500ms #ifdef NODE_LOG_MESSAGES #define NODE_LOG(x, ...) Logger::Print(x, __VA_ARGS__)