Network protocol and invalidation stuff.

This commit is contained in:
momo5502 2016-01-28 23:36:57 +01:00
parent b350c3774b
commit b0e0b1ff2f
3 changed files with 78 additions and 14 deletions

View File

@ -15,11 +15,11 @@ namespace Components
} }
void Network::Address::SetPort(unsigned short port) void Network::Address::SetPort(unsigned short port)
{ {
this->address.port = htons(port); this->address.port = ntohs(port);
}; };
unsigned short Network::Address::GetPort() unsigned short Network::Address::GetPort()
{ {
return ntohs(this->address.port); return htons(this->address.port);
} }
void Network::Address::SetIP(DWORD ip) void Network::Address::SetIP(DWORD ip)
{ {

View File

@ -27,10 +27,13 @@ namespace Components
for (auto entry : Node::Nodes) for (auto entry : Node::Nodes)
{ {
Node::AddressEntry thisAddress; if (entry.state == Node::STATE_VALID)
thisAddress.fromNetAddress(entry.address); {
Node::AddressEntry thisAddress;
thisAddress.fromNetAddress(entry.address);
entries.push_back(thisAddress); entries.push_back(thisAddress);
}
} }
std::string nodeStream(reinterpret_cast<char*>(entries.data()), entries.size() * sizeof(Node::AddressEntry)); std::string nodeStream(reinterpret_cast<char*>(entries.data()), entries.size() * sizeof(Node::AddressEntry));
@ -121,7 +124,7 @@ namespace Components
for (auto entry : Node::Nodes) for (auto entry : Node::Nodes)
{ {
if (entry.state != Node::STATE_INVALID) // Only send valid nodes, or shall we send invalid ones as well? if (entry.state == Node::STATE_VALID) // Only send valid nodes, or shall we send invalid ones as well?
{ {
Node::AddressEntry thisAddress; Node::AddressEntry thisAddress;
thisAddress.fromNetAddress(entry.address); thisAddress.fromNetAddress(entry.address);
@ -187,6 +190,52 @@ namespace Components
} }
} }
void Node::DeleteInvalidNodes()
{
std::vector<Node::NodeEntry> cleanNodes;
for (auto node : Node::Nodes)
{
if (node.state != Node::STATE_INVALID)
{
cleanNodes.push_back(node);
}
else
{
Logger::Print("Removing invalid node %s\n", node.address.GetString());
}
}
if (cleanNodes.size() != Node::Nodes.size())
{
Node::Nodes.clear();
Utils::Merge(&Node::Nodes, cleanNodes);
}
}
void Node::DeleteInvalidDedis()
{
std::vector<Node::DediEntry> cleanDedis;
for (auto dedi : Node::Dedis)
{
if (dedi.state != Node::STATE_INVALID)
{
cleanDedis.push_back(dedi);
}
else
{
Logger::Print("Removing invalid dedi %s\n", dedi.address.GetString());
}
}
if (cleanDedis.size() != Node::Dedis.size())
{
Node::Dedis.clear();
Utils::Merge(&Node::Dedis, cleanDedis);
}
}
Node::Node() Node::Node()
{ {
//#ifdef USE_NODE_STUFF //#ifdef USE_NODE_STUFF
@ -205,7 +254,7 @@ namespace Components
{ {
for (auto node : Node::Nodes) for (auto node : Node::Nodes)
{ {
Network::Send(node.address, "heartbeatDeadline\n"); Network::Send(node.address, "deadline\n");
} }
}); });
} }
@ -271,18 +320,27 @@ namespace Components
Node::AddDedi(address, true); Node::AddDedi(address, true);
}); });
Network::Handle("heartbeatDeadline", [] (Network::Address address, std::string data) Network::Handle("deadline", [] (Network::Address address, std::string data)
{ {
Logger::Print("Invalidation message received from %s\n", address.GetString());
for (auto &dedi : Node::Dedis) for (auto &dedi : Node::Dedis)
{ {
if (dedi.address == address) if (dedi.address == address)
{ {
Logger::Print("Dedi invalidation message received from %s\n", address.GetString());
dedi.state = Node::STATE_INVALID; dedi.state = Node::STATE_INVALID;
dedi.endTime = Game::Com_Milliseconds(); dedi.endTime = Game::Com_Milliseconds();
} }
} }
for (auto &node : Node::Nodes)
{
if (node.address == address)
{
node.state = Node::STATE_INVALID;
node.endTime = Game::Com_Milliseconds();
}
}
}); });
Dedicated::OnFrame([] () Dedicated::OnFrame([] ()
@ -364,6 +422,9 @@ namespace Components
} }
} }
Node::DeleteInvalidNodes();
Node::DeleteInvalidDedis();
count = 0; count = 0;
}); });

View File

@ -1,5 +1,5 @@
#define HEARTBEAT_DEADLINE 1000 * 3 * 10 // Invalidate servers after 10 minutes without heartbeat #define HEARTBEAT_DEADLINE 1000 * 60 * 10 // Invalidate servers after 10 minutes without heartbeat
#define HEARTBEAT_INTERVAL 1000 * 10 * 1 // Send heartbeats to each node every 3 minutes #define HEARTBEAT_INTERVAL 1000 * 60 * 3 // Send heartbeats to each node every 3 minutes
#define NODE_VALIDITY_EXPIRE 1000 * 60 * 2 // Revalidate nodes after 2 minutes #define NODE_VALIDITY_EXPIRE 1000 * 60 * 2 // Revalidate nodes after 2 minutes
#define DEDI_VALIDITY_EXPIRE 1000 * 60 * 2 // Revalidate dedis after 2 minutes #define DEDI_VALIDITY_EXPIRE 1000 * 60 * 2 // Revalidate dedis after 2 minutes
@ -60,7 +60,7 @@ namespace Components
Network::Address address; Network::Address address;
address.SetIP(this->ip); address.SetIP(this->ip);
address.SetPort(ntohs(this->port)); address.SetPort(this->port);
address.SetType(Game::netadrtype_t::NA_IP); address.SetType(Game::netadrtype_t::NA_IP);
return address; return address;
@ -69,7 +69,7 @@ namespace Components
void fromNetAddress(Network::Address address) void fromNetAddress(Network::Address address)
{ {
this->ip = address.GetIP(); this->ip = address.GetIP();
this->port = htons(address.GetPort()); this->port = address.GetPort();
} }
}; };
#pragma pack(pop) #pragma pack(pop)
@ -85,5 +85,8 @@ namespace Components
static void SendNodeList(Network::Address target); static void SendNodeList(Network::Address target);
static void SendDediList(Network::Address target); static void SendDediList(Network::Address target);
static void DeleteInvalidNodes();
static void DeleteInvalidDedis();
}; };
} }