From 9643eb6026aa38e91dc54d5d27681968f7c8adc7 Mon Sep 17 00:00:00 2001 From: Koncord Date: Mon, 24 Apr 2017 19:58:48 +0800 Subject: [PATCH] [Master] Notify server about announce state Rename structure Server to QueryData --- apps/master/MasterServer.cpp | 77 +++++++++++-------- apps/master/MasterServer.hpp | 2 +- apps/master/ServerTest.cpp | 6 +- components/openmw-mp/Master/MasterData.hpp | 4 +- .../openmw-mp/Master/PacketMasterAnnounce.cpp | 2 +- .../openmw-mp/Master/PacketMasterAnnounce.hpp | 4 +- .../openmw-mp/Master/PacketMasterQuery.cpp | 8 +- .../openmw-mp/Master/PacketMasterQuery.hpp | 4 +- .../openmw-mp/Master/PacketMasterUpdate.cpp | 2 +- .../openmw-mp/Master/PacketMasterUpdate.hpp | 4 +- .../openmw-mp/Master/ProxyMasterPacket.hpp | 2 +- 11 files changed, 66 insertions(+), 49 deletions(-) diff --git a/apps/master/MasterServer.cpp b/apps/master/MasterServer.cpp index e0622b391..3f408ea2b 100644 --- a/apps/master/MasterServer.cpp +++ b/apps/master/MasterServer.cpp @@ -39,6 +39,17 @@ void MasterServer::Thread() unsigned char packetId = 0; auto startTime = chrono::steady_clock::now(); + + BitStream send; + PacketMasterQuery pmq(peer); + pmq.SetSendStream(&send); + + PacketMasterUpdate pmu(peer); + pmu.SetSendStream(&send); + + PacketMasterAnnounce pma(peer); + pma.SetSendStream(&send); + while (run) { Packet *packet = peer->Receive(); @@ -76,10 +87,7 @@ void MasterServer::Thread() break; case ID_MASTER_QUERY: { - BitStream send; - PacketMasterQuery pmq(peer); - pmq.SetSendStream(&send); - pmq.SetServers(reinterpret_cast *>(&servers)); + pmq.SetServers(reinterpret_cast *>(&servers)); pmq.Send(packet->systemAddress); cout << "Sent info about all " << servers.size() << " servers to " @@ -95,10 +103,7 @@ void MasterServer::Thread() ServerIter it = servers.find(addr); if (it != servers.end()) { - mwmp::PacketMasterUpdate pmu(peer); - BitStream send; - pmu.SetSendStream(&send); - pair pairPtr(it->first, static_cast(it->second)); + pair pairPtr(it->first, static_cast(it->second)); pmu.SetServer(&pairPtr); pmu.Send(packet->systemAddress); cout << "Sent info about " << addr.ToString() << " to " << packet->systemAddress.ToString() @@ -111,39 +116,51 @@ void MasterServer::Thread() { ServerIter iter = servers.find(packet->systemAddress); - PacketMasterAnnounce pma(peer); pma.SetReadStream(&data); - SServer server; pma.SetServer(&server); pma.Read(); - switch (pma.GetFunc()) - { - case PacketMasterAnnounce::FUNCTION_DELETE: - { - if (iter != servers.end()) - servers.erase(iter); - cout << "Deleted"; - break; - } - case PacketMasterAnnounce::FUNCTION_ANNOUNCE: - { + auto keepAliveFunc = [&]() { + iter->second.lastUpdate = now; + pma.SetFunc(PacketMasterAnnounce::FUNCTION_KEEP); + pma.Send(packet->systemAddress); + }; - if (iter == servers.end()) - cout << "Added"; - else - cout << "Updated"; - iter = servers.insert({packet->systemAddress, server}).first; - break; + if (iter != servers.end()) + { + if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_DELETE) + { + servers.erase(iter); + cout << "Deleted"; + pma.Send(packet->systemAddress); } - default: + else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE) + { + cout << "Updated"; + iter = servers.insert({packet->systemAddress, server}).first; + keepAliveFunc(); + } + else + { cout << "Keeping alive"; + keepAliveFunc(); + } + } + else if(pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE) + { + cout << "Added"; + iter = servers.insert({packet->systemAddress, server}).first; + keepAliveFunc(); + } + else + { + cout << "Unknown"; + pma.SetFunc(PacketMasterAnnounce::FUNCTION_DELETE); + pma.Send(packet->systemAddress); } cout << " server " << packet->systemAddress.ToString() << endl; - if (pma.GetFunc() != PacketMasterAnnounce::FUNCTION_DELETE) - iter->second.lastUpdate = now; peer->CloseConnection(packet->systemAddress, true); break; } diff --git a/apps/master/MasterServer.hpp b/apps/master/MasterServer.hpp index 556c23628..1b2a161be 100644 --- a/apps/master/MasterServer.hpp +++ b/apps/master/MasterServer.hpp @@ -21,7 +21,7 @@ public: bool isRunning(); void Wait(); - struct SServer : Server + struct SServer : QueryData { std::chrono::steady_clock::time_point lastUpdate; }; diff --git a/apps/master/ServerTest.cpp b/apps/master/ServerTest.cpp index 56f4ee835..80b4a9f29 100644 --- a/apps/master/ServerTest.cpp +++ b/apps/master/ServerTest.cpp @@ -59,7 +59,7 @@ int main() else if (strcmp(message, "send") == 0) { puts("Sending data about server"); - Server server; + QueryData server; server.SetName("Super Server"); server.SetPlayers(0); server.SetMaxPlayers(0); @@ -152,7 +152,7 @@ int main() break; case ID_MASTER_QUERY: { - map servers; + map servers; pmq.SetReadStream(&data); pmq.SetServers(&servers); @@ -167,7 +167,7 @@ int main() } case ID_MASTER_UPDATE: { - pair serverPair; + pair serverPair; PacketMasterUpdate pmu(peer); pmu.SetReadStream(&data); pmu.SetServer(&serverPair); diff --git a/components/openmw-mp/Master/MasterData.hpp b/components/openmw-mp/Master/MasterData.hpp index 38348452c..ccde4fc99 100644 --- a/components/openmw-mp/Master/MasterData.hpp +++ b/components/openmw-mp/Master/MasterData.hpp @@ -32,9 +32,9 @@ struct Plugin unsigned long hash; }; -struct Server +struct QueryData { - Server() + QueryData() { rules["name"].type = 's'; rules["name"].str = ""; diff --git a/components/openmw-mp/Master/PacketMasterAnnounce.cpp b/components/openmw-mp/Master/PacketMasterAnnounce.cpp index a206835bc..adb7f50b0 100644 --- a/components/openmw-mp/Master/PacketMasterAnnounce.cpp +++ b/components/openmw-mp/Master/PacketMasterAnnounce.cpp @@ -29,7 +29,7 @@ void PacketMasterAnnounce::Packet(BitStream *bs, bool send) ProxyMasterPacket::addServer(this, *server, send); } -void PacketMasterAnnounce::SetServer(Server *_server) +void PacketMasterAnnounce::SetServer(QueryData *_server) { server = _server; } diff --git a/components/openmw-mp/Master/PacketMasterAnnounce.hpp b/components/openmw-mp/Master/PacketMasterAnnounce.hpp index e64aea95a..4a3a81be0 100644 --- a/components/openmw-mp/Master/PacketMasterAnnounce.hpp +++ b/components/openmw-mp/Master/PacketMasterAnnounce.hpp @@ -19,7 +19,7 @@ namespace mwmp virtual void Packet(RakNet::BitStream *bs, bool send); - void SetServer(Server *server); + void SetServer(QueryData *server); void SetFunc(int keep); int GetFunc(); @@ -30,7 +30,7 @@ namespace mwmp FUNCTION_KEEP }; private: - Server *server; + QueryData *server; int func; }; } diff --git a/components/openmw-mp/Master/PacketMasterQuery.cpp b/components/openmw-mp/Master/PacketMasterQuery.cpp index c98e096be..a36735fdb 100644 --- a/components/openmw-mp/Master/PacketMasterQuery.cpp +++ b/components/openmw-mp/Master/PacketMasterQuery.cpp @@ -29,11 +29,11 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send) RW(serversCount, send); - map::iterator serverIt; + map::iterator serverIt; if(send) serverIt = servers->begin(); - Server server; + QueryData server; SystemAddress sa; while(serversCount--) { @@ -49,12 +49,12 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send) if(send) serverIt++; else - servers->insert(pair(sa, server)); + servers->insert(pair(sa, server)); } } -void PacketMasterQuery::SetServers(map *serverMap) +void PacketMasterQuery::SetServers(map *serverMap) { servers = serverMap; } diff --git a/components/openmw-mp/Master/PacketMasterQuery.hpp b/components/openmw-mp/Master/PacketMasterQuery.hpp index 3b953b993..ada4b308d 100644 --- a/components/openmw-mp/Master/PacketMasterQuery.hpp +++ b/components/openmw-mp/Master/PacketMasterQuery.hpp @@ -19,9 +19,9 @@ namespace mwmp virtual void Packet(RakNet::BitStream *bs, bool send); - void SetServers(std::map *serverMap); + void SetServers(std::map *serverMap); private: - std::map *servers; + std::map *servers; }; } diff --git a/components/openmw-mp/Master/PacketMasterUpdate.cpp b/components/openmw-mp/Master/PacketMasterUpdate.cpp index 538d20611..08ad0d1a2 100644 --- a/components/openmw-mp/Master/PacketMasterUpdate.cpp +++ b/components/openmw-mp/Master/PacketMasterUpdate.cpp @@ -28,7 +28,7 @@ void PacketMasterUpdate::Packet(RakNet::BitStream *bs, bool send) } -void PacketMasterUpdate::SetServer(std::pair *serverPair) +void PacketMasterUpdate::SetServer(std::pair *serverPair) { server = serverPair; } diff --git a/components/openmw-mp/Master/PacketMasterUpdate.hpp b/components/openmw-mp/Master/PacketMasterUpdate.hpp index d21547a86..37adf2216 100644 --- a/components/openmw-mp/Master/PacketMasterUpdate.hpp +++ b/components/openmw-mp/Master/PacketMasterUpdate.hpp @@ -19,9 +19,9 @@ namespace mwmp virtual void Packet(RakNet::BitStream *bs, bool send); - void SetServer(std::pair *serverPair); + void SetServer(std::pair *serverPair); private: - std::pair *server; + std::pair *server; }; } diff --git a/components/openmw-mp/Master/ProxyMasterPacket.hpp b/components/openmw-mp/Master/ProxyMasterPacket.hpp index 9f1a2da61..2e90625a4 100644 --- a/components/openmw-mp/Master/ProxyMasterPacket.hpp +++ b/components/openmw-mp/Master/ProxyMasterPacket.hpp @@ -21,7 +21,7 @@ namespace mwmp public: template - static void addServer(Packet *packet, Server &server, bool send) + static void addServer(Packet *packet, QueryData &server, bool send) { using namespace std;