diff --git a/apps/openmw-mp/MasterClient.cpp b/apps/openmw-mp/MasterClient.cpp index 36de134e1..a13718d42 100644 --- a/apps/openmw-mp/MasterClient.cpp +++ b/apps/openmw-mp/MasterClient.cpp @@ -83,7 +83,7 @@ void MasterClient::SetRuleString(std::string key, std::string value) { ServerRule rule; rule.str = value; - rule.type = 's'; + rule.type = ServerRule::Type::string; queryData.rules.insert({key, rule}); updated = true; } @@ -98,7 +98,7 @@ void MasterClient::SetRuleValue(std::string key, double value) { ServerRule rule; rule.val = value; - rule.type = 'v'; + rule.type = ServerRule::Type::number; queryData.rules.insert({key, rule}); updated = true; } diff --git a/components/openmw-mp/Master/MasterData.hpp b/components/openmw-mp/Master/MasterData.hpp index e553b3710..ef85e1adc 100644 --- a/components/openmw-mp/Master/MasterData.hpp +++ b/components/openmw-mp/Master/MasterData.hpp @@ -6,6 +6,7 @@ #define NEWMASTERPROTO_MASTERDATA_HPP #include +#include #include #include #include @@ -20,7 +21,11 @@ enum MASTER_PACKETS struct ServerRule { - char type; + enum Type : char + { + string = 's', + number = 'v' + } type; std::string str; double val; @@ -30,48 +35,54 @@ struct Plugin { std::string name; unsigned hash; - Plugin(std::string name = "", unsigned hash = 0): name(name), hash(hash) {}; + Plugin(std::string name = "", unsigned hash = 0): name(std::move(name)), hash(hash) {}; }; struct QueryData { QueryData() { - rules["name"].type = 's'; + rules["name"].type = ServerRule::Type::string; rules["name"].str = ""; - rules["version"].type = 's'; + rules["version"].type = ServerRule::Type::string; rules["version"].str = ""; - rules["players"].type = 'v'; + rules["players"].type = ServerRule::Type::number; rules["players"].val = 0; - rules["maxPlayers"].type = 'v'; + rules["maxPlayers"].type = ServerRule::Type::number; rules["maxPlayers"].val = 0; - rules["gamemode"].type = 's'; + rules["gamemode"].type = ServerRule::Type::string; rules["gamemode"].str = ""; - rules["passw"].type = 'v'; + rules["passw"].type = ServerRule::Type::number; rules["passw"].val = 0; } - const char *GetName() { return rules["name"].str.c_str(); } + const char *GetName() const { return rules.at("name").str.c_str(); } void SetName(const char *name) { rules["name"].str = name; } - const char *GetVersion() { return rules["version"].str.c_str(); } + const char *GetVersion() const { return rules.at("version").str.c_str(); } void SetVersion(const char *version) { rules["version"].str = version; } - int GetPlayers() { return rules["players"].val; } + int GetPlayers() const { return rules.at("players").val; } void SetPlayers(int players) { rules["players"].val = players; } - int GetMaxPlayers() { return rules["maxPlayers"].val; } + int GetMaxPlayers() const { return rules.at("maxPlayers").val; } void SetMaxPlayers(int players) { rules["maxPlayers"].val = players; } - const char *GetGameMode() { return rules["gamemode"].str.c_str(); } + const char *GetGameMode() const { return rules.at("gamemode").str.c_str(); } void SetGameMode(const char *str) { rules["gamemode"].str = str; } void SetPassword(int value) { rules["passw"].val = value; }; - int GetPassword() { return rules["passw"].val; } + int GetPassword() const { return rules.at("passw").val; } std::vector players; std::map rules; std::vector plugins; + const static int predefinedRules = 6; + const static int maxRules = 128; + const static int maxUserRules = maxRules - predefinedRules; + const static int maxPlayers = 100; // will shown only maxPlayers players + const static int maxPlugins = 256; + const static int maxStringLength = 256; }; #endif //NEWMASTERPROTO_MASTERDATA_HPP diff --git a/components/openmw-mp/Master/PacketMasterAnnounce.cpp b/components/openmw-mp/Master/PacketMasterAnnounce.cpp index ac8b3cecb..ff22b6465 100644 --- a/components/openmw-mp/Master/PacketMasterAnnounce.cpp +++ b/components/openmw-mp/Master/PacketMasterAnnounce.cpp @@ -35,7 +35,7 @@ void PacketMasterAnnounce::SetServer(QueryData *_server) server = _server; } -void PacketMasterAnnounce::SetFunc(int _func) +void PacketMasterAnnounce::SetFunc(uint32_t _func) { func = _func; } diff --git a/components/openmw-mp/Master/PacketMasterAnnounce.hpp b/components/openmw-mp/Master/PacketMasterAnnounce.hpp index 4a3a81be0..5af7ad4e0 100644 --- a/components/openmw-mp/Master/PacketMasterAnnounce.hpp +++ b/components/openmw-mp/Master/PacketMasterAnnounce.hpp @@ -15,12 +15,12 @@ namespace mwmp { friend class ProxyMasterPacket; public: - PacketMasterAnnounce(RakNet::RakPeerInterface *peer); + explicit PacketMasterAnnounce(RakNet::RakPeerInterface *peer); - virtual void Packet(RakNet::BitStream *bs, bool send); + void Packet(RakNet::BitStream *bs, bool send) override; void SetServer(QueryData *server); - void SetFunc(int keep); + void SetFunc(uint32_t keep); int GetFunc(); enum Func @@ -31,7 +31,7 @@ namespace mwmp }; private: QueryData *server; - int func; + uint32_t func; }; } diff --git a/components/openmw-mp/Master/PacketMasterQuery.cpp b/components/openmw-mp/Master/PacketMasterQuery.cpp index 1850de6c5..c14e32d3e 100644 --- a/components/openmw-mp/Master/PacketMasterQuery.cpp +++ b/components/openmw-mp/Master/PacketMasterQuery.cpp @@ -26,7 +26,7 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send) if (send) bs->Write(packetID); - int serversCount = servers->size(); + int32_t serversCount = servers->size(); RW(serversCount, send); @@ -36,7 +36,7 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send) QueryData server; string addr; - unsigned short port; + uint16_t port; while (serversCount--) { if (send) @@ -50,6 +50,12 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send) ProxyMasterPacket::addServer(this, server, send); + if(addr.empty()) + { + std::cerr << "Address empty. Aborting PacketMasterQuery::Packet" << std::endl; + return; + } + if (send) serverIt++; else diff --git a/components/openmw-mp/Master/PacketMasterQuery.hpp b/components/openmw-mp/Master/PacketMasterQuery.hpp index ada4b308d..43eeafee0 100644 --- a/components/openmw-mp/Master/PacketMasterQuery.hpp +++ b/components/openmw-mp/Master/PacketMasterQuery.hpp @@ -15,9 +15,9 @@ namespace mwmp { friend class ProxyMasterPacket; public: - PacketMasterQuery(RakNet::RakPeerInterface *peer); + explicit PacketMasterQuery(RakNet::RakPeerInterface *peer); - virtual void Packet(RakNet::BitStream *bs, bool send); + void Packet(RakNet::BitStream *bs, bool send) override; void SetServers(std::map *serverMap); private: diff --git a/components/openmw-mp/Master/PacketMasterUpdate.cpp b/components/openmw-mp/Master/PacketMasterUpdate.cpp index 54f32e266..33ef10ec4 100644 --- a/components/openmw-mp/Master/PacketMasterUpdate.cpp +++ b/components/openmw-mp/Master/PacketMasterUpdate.cpp @@ -24,7 +24,7 @@ void PacketMasterUpdate::Packet(RakNet::BitStream *bs, bool send) bs->Write(packetID); string addr = server->first.ToString(false); - unsigned short port = server->first.GetPort(); + uint16_t port = server->first.GetPort(); RW(addr, send); RW(port, send); diff --git a/components/openmw-mp/Master/PacketMasterUpdate.hpp b/components/openmw-mp/Master/PacketMasterUpdate.hpp index 37adf2216..f3c254dbb 100644 --- a/components/openmw-mp/Master/PacketMasterUpdate.hpp +++ b/components/openmw-mp/Master/PacketMasterUpdate.hpp @@ -15,9 +15,9 @@ namespace mwmp { friend class ProxyMasterPacket; public: - PacketMasterUpdate(RakNet::RakPeerInterface *peer); + explicit PacketMasterUpdate(RakNet::RakPeerInterface *peer); - virtual void Packet(RakNet::BitStream *bs, bool send); + void Packet(RakNet::BitStream *bs, bool send) override; void SetServer(std::pair *serverPair); private: diff --git a/components/openmw-mp/Master/ProxyMasterPacket.hpp b/components/openmw-mp/Master/ProxyMasterPacket.hpp index d5d4506b5..a9920d424 100644 --- a/components/openmw-mp/Master/ProxyMasterPacket.hpp +++ b/components/openmw-mp/Master/ProxyMasterPacket.hpp @@ -14,9 +14,8 @@ namespace mwmp class ProxyMasterPacket : public BasePacket { private: - ProxyMasterPacket(RakNet::RakPeerInterface *peer) : BasePacket(peer) + explicit ProxyMasterPacket(RakNet::RakPeerInterface *peer) : BasePacket(peer) { - } public: @@ -25,11 +24,10 @@ namespace mwmp { using namespace std; - int rulesSize = server.rules.size(); - + int32_t rulesSize = server.rules.size(); packet->RW(rulesSize, send); - if (rulesSize > 2000) + if (rulesSize > QueryData::maxRules) rulesSize = 0; map::iterator ruleIt; @@ -38,7 +36,7 @@ namespace mwmp while (rulesSize--) { - ServerRule *rule = 0; + ServerRule *rule = nullptr; string key; if (send) { @@ -46,7 +44,7 @@ namespace mwmp rule = &ruleIt->second; } - packet->RW(key, send); + packet->RW(key, send, QueryData::maxStringLength); if (!send) { ruleIt = server.rules.insert(pair(key, ServerRule())).first; @@ -55,8 +53,8 @@ namespace mwmp packet->RW(rule->type, send); - if (rule->type == 's') - packet->RW(rule->str, send); + if (rule->type == ServerRule::Type::string) + packet->RW(rule->str, send, QueryData::maxStringLength); else packet->RW(rule->val, send); @@ -66,56 +64,38 @@ namespace mwmp vector::iterator plIt; - if (send) - plIt = server.players.begin(); - else - server.players.clear(); - - int playersCount = server.players.size(); + int32_t playersCount = server.players.size(); packet->RW(playersCount, send); - if (playersCount > 2000) + if (playersCount > QueryData::maxPlayers) playersCount = 0; - while (playersCount--) + if (!send) { - string player; - if (send) - player = *plIt; + server.players.clear(); + server.players.resize(playersCount); + } - packet->RW(player, send); + for(auto &&player : server.players) + packet->RW(player, send, QueryData::maxStringLength); - if (!send) - server.players.push_back(player); - else - plIt++; - } - int pluginsCount = server.plugins.size(); + int32_t pluginsCount = server.plugins.size(); packet->RW(pluginsCount, send); - if (pluginsCount > 2000) + if (pluginsCount > QueryData::maxPlugins) pluginsCount = 0; - vector::iterator pluginIt; - - if (send) - pluginIt = server.plugins.begin(); - else + if (!send) + { server.plugins.clear(); + server.plugins.resize(pluginsCount); + } - while (pluginsCount--) + for (auto &&plugin : server.plugins) { - Plugin plugin; - if (send) - plugin = *pluginIt; - - packet->RW(plugin.name, send); + packet->RW(plugin.name, send, QueryData::maxStringLength); packet->RW(plugin.hash, send); - if (!send) - server.plugins.push_back(plugin); - else - pluginIt++; } } };