From 546bb42abb5108edcb501b868688cd6bc604f67b Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 23 Apr 2017 13:36:47 +0800 Subject: [PATCH] [General] Add Master Packets --- .../openmw-mp/Master/PacketMasterAnnounce.cpp | 45 ++++++++ .../openmw-mp/Master/PacketMasterAnnounce.hpp | 38 +++++++ .../openmw-mp/Master/PacketMasterQuery.cpp | 60 ++++++++++ .../openmw-mp/Master/PacketMasterQuery.hpp | 28 +++++ .../openmw-mp/Master/PacketMasterUpdate.cpp | 34 ++++++ .../openmw-mp/Master/PacketMasterUpdate.hpp | 28 +++++ .../openmw-mp/Master/ProxyMasterPacket.hpp | 107 ++++++++++++++++++ 7 files changed, 340 insertions(+) create mode 100644 components/openmw-mp/Master/PacketMasterAnnounce.cpp create mode 100644 components/openmw-mp/Master/PacketMasterAnnounce.hpp create mode 100644 components/openmw-mp/Master/PacketMasterQuery.cpp create mode 100644 components/openmw-mp/Master/PacketMasterQuery.hpp create mode 100644 components/openmw-mp/Master/PacketMasterUpdate.cpp create mode 100644 components/openmw-mp/Master/PacketMasterUpdate.hpp create mode 100644 components/openmw-mp/Master/ProxyMasterPacket.hpp diff --git a/components/openmw-mp/Master/PacketMasterAnnounce.cpp b/components/openmw-mp/Master/PacketMasterAnnounce.cpp new file mode 100644 index 000000000..a206835bc --- /dev/null +++ b/components/openmw-mp/Master/PacketMasterAnnounce.cpp @@ -0,0 +1,45 @@ +// +// Created by koncord on 22.04.17. +// + +#include +#include +#include "PacketMasterAnnounce.hpp" +#include "ProxyMasterPacket.hpp" + +using namespace mwmp; +using namespace RakNet; +using namespace std; + +PacketMasterAnnounce::PacketMasterAnnounce(RakNet::RakPeerInterface *peer) : BasePacket(peer) +{ + packetID = ID_MASTER_ANNOUNCE; + orderChannel = CHANNEL_MASTER; +} + +void PacketMasterAnnounce::Packet(BitStream *bs, bool send) +{ + this->bs = bs; + if (send) + bs->Write(packetID); + + RW(func, send); + + if(func == FUNCTION_ANNOUNCE) + ProxyMasterPacket::addServer(this, *server, send); +} + +void PacketMasterAnnounce::SetServer(Server *_server) +{ + server = _server; +} + +void PacketMasterAnnounce::SetFunc(int _func) +{ + func = _func; +} + +int PacketMasterAnnounce::GetFunc() +{ + return func; +} diff --git a/components/openmw-mp/Master/PacketMasterAnnounce.hpp b/components/openmw-mp/Master/PacketMasterAnnounce.hpp new file mode 100644 index 000000000..e64aea95a --- /dev/null +++ b/components/openmw-mp/Master/PacketMasterAnnounce.hpp @@ -0,0 +1,38 @@ +// +// Created by koncord on 22.04.17. +// + +#ifndef OPENMW_PACKETMASTERANNOUNCE_HPP +#define OPENMW_PACKETMASTERANNOUNCE_HPP + +#include "../Packets/BasePacket.hpp" +#include "MasterData.hpp" + +namespace mwmp +{ + class ProxyMasterPacket; + class PacketMasterAnnounce : public BasePacket + { + friend class ProxyMasterPacket; + public: + PacketMasterAnnounce(RakNet::RakPeerInterface *peer); + + virtual void Packet(RakNet::BitStream *bs, bool send); + + void SetServer(Server *server); + void SetFunc(int keep); + int GetFunc(); + + enum Func + { + FUNCTION_DELETE = 0, + FUNCTION_ANNOUNCE, + FUNCTION_KEEP + }; + private: + Server *server; + int func; + }; +} + +#endif //OPENMW_PACKETMASTERANNOUNCE_HPP diff --git a/components/openmw-mp/Master/PacketMasterQuery.cpp b/components/openmw-mp/Master/PacketMasterQuery.cpp new file mode 100644 index 000000000..c98e096be --- /dev/null +++ b/components/openmw-mp/Master/PacketMasterQuery.cpp @@ -0,0 +1,60 @@ +// +// Created by koncord on 22.04.17. +// + +#include +#include +#include "MasterData.hpp" +#include "PacketMasterQuery.hpp" +#include "ProxyMasterPacket.hpp" + + +using namespace mwmp; +using namespace std; +using namespace RakNet; + +PacketMasterQuery::PacketMasterQuery(RakNet::RakPeerInterface *peer) : BasePacket(peer) +{ + packetID = ID_MASTER_QUERY; + orderChannel = CHANNEL_MASTER; +} + +void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send) +{ + this->bs = bs; + if (send) + bs->Write(packetID); + + int serversCount = servers->size(); + + RW(serversCount, send); + + map::iterator serverIt; + if(send) + serverIt = servers->begin(); + + Server server; + SystemAddress sa; + while(serversCount--) + { + if(send) + { + sa = serverIt->first; + server = serverIt->second; + } + + RW(sa, send); + ProxyMasterPacket::addServer(this, server, send); + + if(send) + serverIt++; + else + servers->insert(pair(sa, server)); + } + +} + +void PacketMasterQuery::SetServers(map *serverMap) +{ + servers = serverMap; +} diff --git a/components/openmw-mp/Master/PacketMasterQuery.hpp b/components/openmw-mp/Master/PacketMasterQuery.hpp new file mode 100644 index 000000000..3b953b993 --- /dev/null +++ b/components/openmw-mp/Master/PacketMasterQuery.hpp @@ -0,0 +1,28 @@ +// +// Created by koncord on 22.04.17. +// + +#ifndef OPENMW_PACKETMASTERQUERY_HPP +#define OPENMW_PACKETMASTERQUERY_HPP + +#include "../Packets/BasePacket.hpp" +#include "MasterData.hpp" + +namespace mwmp +{ + class ProxyMasterPacket; + class PacketMasterQuery : public BasePacket + { + friend class ProxyMasterPacket; + public: + PacketMasterQuery(RakNet::RakPeerInterface *peer); + + virtual void Packet(RakNet::BitStream *bs, bool send); + + void SetServers(std::map *serverMap); + private: + std::map *servers; + }; +} + +#endif //OPENMW_PACKETMASTERQUERY_HPP diff --git a/components/openmw-mp/Master/PacketMasterUpdate.cpp b/components/openmw-mp/Master/PacketMasterUpdate.cpp new file mode 100644 index 000000000..538d20611 --- /dev/null +++ b/components/openmw-mp/Master/PacketMasterUpdate.cpp @@ -0,0 +1,34 @@ +// +// Created by koncord on 22.04.17. +// + +#include +#include "PacketMasterUpdate.hpp" +#include "ProxyMasterPacket.hpp" + +using namespace mwmp; +using namespace std; +using namespace RakNet; + +PacketMasterUpdate::PacketMasterUpdate(RakNet::RakPeerInterface *peer) : BasePacket(peer) +{ + packetID = ID_MASTER_UPDATE; + orderChannel = CHANNEL_MASTER; +} + +void PacketMasterUpdate::Packet(RakNet::BitStream *bs, bool send) +{ + this->bs = bs; + if (send) + bs->Write(packetID); + + RW(server->first, send); + + ProxyMasterPacket::addServer(this, server->second, send); + +} + +void PacketMasterUpdate::SetServer(std::pair *serverPair) +{ + server = serverPair; +} diff --git a/components/openmw-mp/Master/PacketMasterUpdate.hpp b/components/openmw-mp/Master/PacketMasterUpdate.hpp new file mode 100644 index 000000000..d21547a86 --- /dev/null +++ b/components/openmw-mp/Master/PacketMasterUpdate.hpp @@ -0,0 +1,28 @@ +// +// Created by koncord on 22.04.17. +// + +#ifndef OPENMW_PACKETMASTERUPDATE_HPP +#define OPENMW_PACKETMASTERUPDATE_HPP + +#include "../Packets/BasePacket.hpp" +#include "MasterData.hpp" + +namespace mwmp +{ + class ProxyMasterPacket; + class PacketMasterUpdate : public BasePacket + { + friend class ProxyMasterPacket; + public: + PacketMasterUpdate(RakNet::RakPeerInterface *peer); + + virtual void Packet(RakNet::BitStream *bs, bool send); + + void SetServer(std::pair *serverPair); + private: + std::pair *server; + }; +} + +#endif //OPENMW_PACKETMASTERUPDATE_HPP diff --git a/components/openmw-mp/Master/ProxyMasterPacket.hpp b/components/openmw-mp/Master/ProxyMasterPacket.hpp new file mode 100644 index 000000000..9f1a2da61 --- /dev/null +++ b/components/openmw-mp/Master/ProxyMasterPacket.hpp @@ -0,0 +1,107 @@ +// +// Created by koncord on 22.04.17. +// + +#ifndef OPENMW_PROXYMASTERPACKET_HPP +#define OPENMW_PROXYMASTERPACKET_HPP + +#include +#include "MasterData.hpp" +#include + +namespace mwmp +{ + class ProxyMasterPacket : public BasePacket + { + private: + ProxyMasterPacket(RakNet::RakPeerInterface *peer) : BasePacket(peer) + { + + } + + public: + template + static void addServer(Packet *packet, Server &server, bool send) + { + using namespace std; + + int rulesSize = server.rules.size(); + + packet->RW(rulesSize, send); + + map::iterator ruleIt; + if (send) + ruleIt = server.rules.begin(); + + while (rulesSize--) + { + ServerRule *rule = 0; + string key; + if (send) + { + key = ruleIt->first; + rule = &ruleIt->second; + } + + packet->RW(key, send); + if (!send) + { + ruleIt = server.rules.insert(pair(key, ServerRule())).first; + rule = &ruleIt->second; + } + + packet->RW(rule->type, send); + + if (rule->type == 's') + packet->RW(rule->str, send); + else + packet->RW(rule->val, send); + + if (send) + ruleIt++; + } + + list::iterator plIt; + + if (send) + plIt = server.players.begin(); + else + server.players.clear(); + + int playersCount = server.GetPlayers(); + while (playersCount--) + { + string player; + if (send) + player = *plIt; + + packet->RW(player, send); + + if (!send) + server.players.push_back(player); + } + + int pluginsCount = server.plugins.size(); + packet->RW(pluginsCount, send); + + vector::iterator pluginIt; + + if (send) + pluginIt = server.plugins.begin(); + else + server.plugins.clear(); + + while (pluginsCount--) + { + Plugin plugin; + if (send) + plugin = *pluginIt; + + packet->RW(plugin.name, send); + packet->RW(plugin.hash, send); + } + } + }; +} + +#endif //OPENMW_PROXYMASTERPACKET_HPP