[Master] Waiting ACK instead of immediately closing connection.

This commit is contained in:
Koncord 2017-07-31 19:07:20 +08:00
parent 1aabcdd09c
commit 918658d383
2 changed files with 28 additions and 7 deletions

View file

@ -16,6 +16,7 @@
using namespace RakNet; using namespace RakNet;
using namespace std; using namespace std;
using namespace mwmp; using namespace mwmp;
using namespace chrono;
MasterServer::MasterServer(unsigned short maxConnections, unsigned short port) MasterServer::MasterServer(unsigned short maxConnections, unsigned short port)
{ {
@ -55,10 +56,10 @@ void MasterServer::Thread()
{ {
Packet *packet = peer->Receive(); Packet *packet = peer->Receive();
auto now = chrono::steady_clock::now(); auto now = steady_clock::now();
if (now - startTime >= 60s) if (now - startTime >= 60s)
{ {
startTime = chrono::steady_clock::now(); startTime = steady_clock::now();
for (auto it = servers.begin(); it != servers.end();) for (auto it = servers.begin(); it != servers.end();)
{ {
@ -66,6 +67,17 @@ void MasterServer::Thread()
servers.erase(it++); servers.erase(it++);
else ++it; else ++it;
} }
for(auto id = pendingACKs.begin(); id != pendingACKs.end();)
{
if(now - id->second >= 30s)
{
cout << "timeout: " << peer->GetSystemAddressFromGuid(id->first).ToString() << endl;
peer->CloseConnection(id->first, true);
id = pendingACKs.erase(id);
}
else
++id;
}
} }
if (packet == nullptr) if (packet == nullptr)
@ -90,10 +102,10 @@ void MasterServer::Thread()
{ {
pmq.SetServers(reinterpret_cast<map<SystemAddress, QueryData> *>(&servers)); pmq.SetServers(reinterpret_cast<map<SystemAddress, QueryData> *>(&servers));
pmq.Send(packet->systemAddress); pmq.Send(packet->systemAddress);
pendingACKs[packet->guid] = steady_clock::now();
cout << "Sent info about all " << servers.size() << " servers to " cout << "Sent info about all " << servers.size() << " servers to "
<< packet->systemAddress.ToString() << endl; << packet->systemAddress.ToString() << endl;
peer->CloseConnection(packet->systemAddress, true);
break; break;
} }
case ID_MASTER_UPDATE: case ID_MASTER_UPDATE:
@ -107,10 +119,10 @@ void MasterServer::Thread()
pair<SystemAddress, QueryData> pairPtr(it->first, static_cast<QueryData>(it->second)); pair<SystemAddress, QueryData> pairPtr(it->first, static_cast<QueryData>(it->second));
pmu.SetServer(&pairPtr); pmu.SetServer(&pairPtr);
pmu.Send(packet->systemAddress); pmu.Send(packet->systemAddress);
pendingACKs[packet->guid] = steady_clock::now();
cout << "Sent info about " << addr.ToString() << " to " << packet->systemAddress.ToString() cout << "Sent info about " << addr.ToString() << " to " << packet->systemAddress.ToString()
<< endl; << endl;
} }
peer->CloseConnection(packet->systemAddress, true);
break; break;
} }
case ID_MASTER_ANNOUNCE: case ID_MASTER_ANNOUNCE:
@ -126,6 +138,7 @@ void MasterServer::Thread()
iter->second.lastUpdate = now; iter->second.lastUpdate = now;
pma.SetFunc(PacketMasterAnnounce::FUNCTION_KEEP); pma.SetFunc(PacketMasterAnnounce::FUNCTION_KEEP);
pma.Send(packet->systemAddress); pma.Send(packet->systemAddress);
pendingACKs[packet->guid] = steady_clock::now();
}; };
if (iter != servers.end()) if (iter != servers.end())
@ -135,6 +148,7 @@ void MasterServer::Thread()
servers.erase(iter); servers.erase(iter);
cout << "Deleted"; cout << "Deleted";
pma.Send(packet->systemAddress); pma.Send(packet->systemAddress);
pendingACKs[packet->guid] = steady_clock::now();
} }
else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE) else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE)
{ {
@ -159,14 +173,20 @@ void MasterServer::Thread()
cout << "Unknown"; cout << "Unknown";
pma.SetFunc(PacketMasterAnnounce::FUNCTION_DELETE); pma.SetFunc(PacketMasterAnnounce::FUNCTION_DELETE);
pma.Send(packet->systemAddress); pma.Send(packet->systemAddress);
pendingACKs[packet->guid] = steady_clock::now();
} }
cout << " server " << packet->systemAddress.ToString() << endl; cout << " server " << packet->systemAddress.ToString() << endl;
peer->CloseConnection(packet->systemAddress, true);
break; break;
} }
case ID_SND_RECEIPT_ACKED:
uint32_t num;
memcpy(&num, packet->data+1, 4);
cout << "Packet with id " << num << " was delivered." << endl;
pendingACKs.erase(packet->guid);
peer->CloseConnection(packet->systemAddress, true);
break;
default: default:
cout << "Wrong packet" << endl; cout << "Wrong packet. id " << (unsigned) packet->data[0] << " packet length " << packet->length << " from " << packet->systemAddress.ToString() << endl;
peer->CloseConnection(packet->systemAddress, true); peer->CloseConnection(packet->systemAddress, true);
} }
} }

View file

@ -48,6 +48,7 @@ private:
RakNet::SocketDescriptor sockdescr; RakNet::SocketDescriptor sockdescr;
ServerMap servers; ServerMap servers;
bool run; bool run;
std::map<RakNet::RakNetGUID, std::chrono::steady_clock::time_point> pendingACKs;
}; };