Merge branch 'node_crash_fix' into bot_scr

This commit is contained in:
INeedBots 2020-11-17 01:13:59 -06:00
commit b3e8ce95b4
2 changed files with 38 additions and 13 deletions

View File

@ -278,26 +278,49 @@ namespace Components
} }
void Node::SendList(Network::Address address) void Node::SendList(Network::Address address)
{
std::lock_guard<std::recursive_mutex> _(Node::Mutex);
// need to keep the message size below 1404 bytes else recipient will just drop it
std::vector<std::string> nodeListReponseMessages;
for (size_t curNode = 0; curNode < Node::Nodes.size();)
{ {
Proto::Node::List list; Proto::Node::List list;
list.set_isnode(Dedicated::IsEnabled()); list.set_isnode(Dedicated::IsEnabled());
list.set_protocol(PROTOCOL); list.set_protocol(PROTOCOL);
list.set_port(Node::GetPort()); list.set_port(Node::GetPort());
std::lock_guard<std::recursive_mutex> _(Node::Mutex); for (size_t i = 0; i < NODE_MAX_NODES_TO_SEND;)
for (auto& node : Node::Nodes)
{ {
if (curNode >= Node::Nodes.size())
break;
auto node = Node::Nodes.at(curNode++);
if (node.isValid()) if (node.isValid())
{ {
std::string* str = list.add_nodes(); std::string* str = list.add_nodes();
sockaddr addr = node.address.getSockAddr(); sockaddr addr = node.address.getSockAddr();
str->append(reinterpret_cast<char*>(&addr), sizeof(addr)); str->append(reinterpret_cast<char*>(&addr), sizeof(addr));
i++;
} }
} }
Session::Send(address, "nodeListResponse", list.SerializeAsString()); nodeListReponseMessages.push_back(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__)