[General] Explicitly use limitations of the master server

0.6.3
Koncord 7 years ago
parent a48d5b48ef
commit d162f6fd3a

@ -83,7 +83,7 @@ void MasterClient::SetRuleString(std::string key, std::string value)
{ {
ServerRule rule; ServerRule rule;
rule.str = value; rule.str = value;
rule.type = 's'; rule.type = ServerRule::Type::string;
queryData.rules.insert({key, rule}); queryData.rules.insert({key, rule});
updated = true; updated = true;
} }
@ -98,7 +98,7 @@ void MasterClient::SetRuleValue(std::string key, double value)
{ {
ServerRule rule; ServerRule rule;
rule.val = value; rule.val = value;
rule.type = 'v'; rule.type = ServerRule::Type::number;
queryData.rules.insert({key, rule}); queryData.rules.insert({key, rule});
updated = true; updated = true;
} }

@ -6,6 +6,7 @@
#define NEWMASTERPROTO_MASTERDATA_HPP #define NEWMASTERPROTO_MASTERDATA_HPP
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include <map> #include <map>
#include <list> #include <list>
@ -20,7 +21,11 @@ enum MASTER_PACKETS
struct ServerRule struct ServerRule
{ {
char type; enum Type : char
{
string = 's',
number = 'v'
} type;
std::string str; std::string str;
double val; double val;
@ -30,48 +35,54 @@ struct Plugin
{ {
std::string name; std::string name;
unsigned hash; 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 struct QueryData
{ {
QueryData() QueryData()
{ {
rules["name"].type = 's'; rules["name"].type = ServerRule::Type::string;
rules["name"].str = ""; rules["name"].str = "";
rules["version"].type = 's'; rules["version"].type = ServerRule::Type::string;
rules["version"].str = ""; rules["version"].str = "";
rules["players"].type = 'v'; rules["players"].type = ServerRule::Type::number;
rules["players"].val = 0; rules["players"].val = 0;
rules["maxPlayers"].type = 'v'; rules["maxPlayers"].type = ServerRule::Type::number;
rules["maxPlayers"].val = 0; rules["maxPlayers"].val = 0;
rules["gamemode"].type = 's'; rules["gamemode"].type = ServerRule::Type::string;
rules["gamemode"].str = ""; rules["gamemode"].str = "";
rules["passw"].type = 'v'; rules["passw"].type = ServerRule::Type::number;
rules["passw"].val = 0; 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; } 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; } 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; } 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; } 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 SetGameMode(const char *str) { rules["gamemode"].str = str; }
void SetPassword(int value) { rules["passw"].val = value; }; void SetPassword(int value) { rules["passw"].val = value; };
int GetPassword() { return rules["passw"].val; } int GetPassword() const { return rules.at("passw").val; }
std::vector<std::string> players; std::vector<std::string> players;
std::map<std::string, ServerRule> rules; std::map<std::string, ServerRule> rules;
std::vector<Plugin> plugins; std::vector<Plugin> 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 #endif //NEWMASTERPROTO_MASTERDATA_HPP

@ -35,7 +35,7 @@ void PacketMasterAnnounce::SetServer(QueryData *_server)
server = _server; server = _server;
} }
void PacketMasterAnnounce::SetFunc(int _func) void PacketMasterAnnounce::SetFunc(uint32_t _func)
{ {
func = _func; func = _func;
} }

@ -15,12 +15,12 @@ namespace mwmp
{ {
friend class ProxyMasterPacket; friend class ProxyMasterPacket;
public: 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 SetServer(QueryData *server);
void SetFunc(int keep); void SetFunc(uint32_t keep);
int GetFunc(); int GetFunc();
enum Func enum Func
@ -31,7 +31,7 @@ namespace mwmp
}; };
private: private:
QueryData *server; QueryData *server;
int func; uint32_t func;
}; };
} }

@ -26,7 +26,7 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send)
if (send) if (send)
bs->Write(packetID); bs->Write(packetID);
int serversCount = servers->size(); int32_t serversCount = servers->size();
RW(serversCount, send); RW(serversCount, send);
@ -36,7 +36,7 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send)
QueryData server; QueryData server;
string addr; string addr;
unsigned short port; uint16_t port;
while (serversCount--) while (serversCount--)
{ {
if (send) if (send)
@ -50,6 +50,12 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send)
ProxyMasterPacket::addServer(this, server, send); ProxyMasterPacket::addServer(this, server, send);
if(addr.empty())
{
std::cerr << "Address empty. Aborting PacketMasterQuery::Packet" << std::endl;
return;
}
if (send) if (send)
serverIt++; serverIt++;
else else

@ -15,9 +15,9 @@ namespace mwmp
{ {
friend class ProxyMasterPacket; friend class ProxyMasterPacket;
public: 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<RakNet::SystemAddress, QueryData> *serverMap); void SetServers(std::map<RakNet::SystemAddress, QueryData> *serverMap);
private: private:

@ -24,7 +24,7 @@ void PacketMasterUpdate::Packet(RakNet::BitStream *bs, bool send)
bs->Write(packetID); bs->Write(packetID);
string addr = server->first.ToString(false); string addr = server->first.ToString(false);
unsigned short port = server->first.GetPort(); uint16_t port = server->first.GetPort();
RW(addr, send); RW(addr, send);
RW(port, send); RW(port, send);

@ -15,9 +15,9 @@ namespace mwmp
{ {
friend class ProxyMasterPacket; friend class ProxyMasterPacket;
public: 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<RakNet::SystemAddress, QueryData> *serverPair); void SetServer(std::pair<RakNet::SystemAddress, QueryData> *serverPair);
private: private:

@ -14,9 +14,8 @@ namespace mwmp
class ProxyMasterPacket : public BasePacket class ProxyMasterPacket : public BasePacket
{ {
private: private:
ProxyMasterPacket(RakNet::RakPeerInterface *peer) : BasePacket(peer) explicit ProxyMasterPacket(RakNet::RakPeerInterface *peer) : BasePacket(peer)
{ {
} }
public: public:
@ -25,11 +24,10 @@ namespace mwmp
{ {
using namespace std; using namespace std;
int rulesSize = server.rules.size(); int32_t rulesSize = server.rules.size();
packet->RW(rulesSize, send); packet->RW(rulesSize, send);
if (rulesSize > 2000) if (rulesSize > QueryData::maxRules)
rulesSize = 0; rulesSize = 0;
map<string, ServerRule>::iterator ruleIt; map<string, ServerRule>::iterator ruleIt;
@ -38,7 +36,7 @@ namespace mwmp
while (rulesSize--) while (rulesSize--)
{ {
ServerRule *rule = 0; ServerRule *rule = nullptr;
string key; string key;
if (send) if (send)
{ {
@ -46,7 +44,7 @@ namespace mwmp
rule = &ruleIt->second; rule = &ruleIt->second;
} }
packet->RW(key, send); packet->RW(key, send, QueryData::maxStringLength);
if (!send) if (!send)
{ {
ruleIt = server.rules.insert(pair<string, ServerRule>(key, ServerRule())).first; ruleIt = server.rules.insert(pair<string, ServerRule>(key, ServerRule())).first;
@ -55,8 +53,8 @@ namespace mwmp
packet->RW(rule->type, send); packet->RW(rule->type, send);
if (rule->type == 's') if (rule->type == ServerRule::Type::string)
packet->RW(rule->str, send); packet->RW(rule->str, send, QueryData::maxStringLength);
else else
packet->RW(rule->val, send); packet->RW(rule->val, send);
@ -66,56 +64,38 @@ namespace mwmp
vector<string>::iterator plIt; vector<string>::iterator plIt;
if (send) int32_t playersCount = server.players.size();
plIt = server.players.begin();
else
server.players.clear();
int playersCount = server.players.size();
packet->RW(playersCount, send); packet->RW(playersCount, send);
if (playersCount > 2000) if (playersCount > QueryData::maxPlayers)
playersCount = 0; playersCount = 0;
while (playersCount--) if (!send)
{ {
string player; server.players.clear();
if (send) server.players.resize(playersCount);
player = *plIt; }
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); packet->RW(pluginsCount, send);
if (pluginsCount > 2000) if (pluginsCount > QueryData::maxPlugins)
pluginsCount = 0; pluginsCount = 0;
vector<Plugin>::iterator pluginIt; if (!send)
{
if (send)
pluginIt = server.plugins.begin();
else
server.plugins.clear(); server.plugins.clear();
server.plugins.resize(pluginsCount);
}
while (pluginsCount--) for (auto &&plugin : server.plugins)
{ {
Plugin plugin; packet->RW(plugin.name, send, QueryData::maxStringLength);
if (send)
plugin = *pluginIt;
packet->RW(plugin.name, send);
packet->RW(plugin.hash, send); packet->RW(plugin.hash, send);
if (!send)
server.plugins.push_back(plugin);
else
pluginIt++;
} }
} }
}; };

Loading…
Cancel
Save