From f510a5583fc7e8ca48f0dd6b5f1bc9ee0d7ccaa0 Mon Sep 17 00:00:00 2001 From: Koncord Date: Tue, 10 Jan 2017 19:21:31 +0800 Subject: [PATCH] Move MasterClient to Networking class Rename MOTD to hostname --- apps/openmw-mp/MasterClient.cpp | 42 +++++++++++++++++++------- apps/openmw-mp/MasterClient.hpp | 12 ++++++-- apps/openmw-mp/Networking.cpp | 12 ++++++++ apps/openmw-mp/Networking.hpp | 5 +++ apps/openmw-mp/main.cpp | 26 +++++++--------- files/tes3mp/tes3mp-server-default.cfg | 2 +- 6 files changed, 69 insertions(+), 30 deletions(-) diff --git a/apps/openmw-mp/MasterClient.cpp b/apps/openmw-mp/MasterClient.cpp index 86115ce35..e7c86c6d2 100644 --- a/apps/openmw-mp/MasterClient.cpp +++ b/apps/openmw-mp/MasterClient.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "MasterClient.hpp" #include @@ -19,12 +20,10 @@ MasterClient::MasterClient(std::string queryAddr, unsigned short queryPort, std: unsigned short serverPort) : queryAddr(queryAddr), queryPort(queryPort), serverAddr(serverAddr), serverPort(serverPort) { - httpConnection = RakNet::HTTPConnection2::GetInstance(); - tcpInterface.Start(0, 64); - tcpInterface.AttachPlugin(httpConnection); players = 0; maxPlayers = 0; - motd = ""; + hostname = ""; + modname = ""; timeout = 1000; // every 1 seconds } @@ -42,14 +41,22 @@ void MasterClient::SetMaxPlayers(unsigned pl) mutexData.unlock(); } -void MasterClient::SetMOTD(std::string &motd) +void MasterClient::SetHostname(std::string hostname) { mutexData.lock(); - this->motd = motd.substr(0, 200); + this->hostname = hostname.substr(0, 200); mutexData.unlock(); } -RakNet::RakString MasterClient::Send(std::string motd, unsigned players, unsigned maxPlayers, bool update) +void MasterClient::SetModname(std::string modname) +{ + mutexData.lock(); + this->modname = modname.substr(0, 200); + mutexData.unlock(); +} + +RakNet::RakString +MasterClient::Send(std::string hostname, std::string modname, unsigned maxPlayers, bool update, unsigned players) { /*static unsigned short oldServerPort, oldQueryPort; static string oldMotd; @@ -59,7 +66,8 @@ RakNet::RakString MasterClient::Send(std::string motd, unsigned players, unsigne sstr << "{"; sstr << "\"port\": " << serverPort << ", "; sstr << "\"query_port\": " << queryPort << ", "; - sstr << "\"motd\": \"" << motd << "\", "; + sstr << "\"hostname\": \"" << hostname.c_str() << "\", "; + sstr << "\"hostname\": \"" << modname.c_str() << "\", "; sstr << "\"players\": " << players << ", "; sstr << "\"max_players\": " << maxPlayers; sstr << "}"; @@ -126,7 +134,12 @@ RakNet::RakString MasterClient::Send(std::string motd, unsigned players, unsigne void MasterClient::Update() { assert(!sRun); - RakNet::RakString response = Send(motd, players, maxPlayers, false); + + httpConnection = RakNet::HTTPConnection2::GetInstance(); + tcpInterface.Start(0, 64); + tcpInterface.AttachPlugin(httpConnection); + + RakNet::RakString response = Send(hostname, std::__cxx11::string(), maxPlayers, false, players); bool update = true; sRun = true; while (sRun) @@ -151,15 +164,22 @@ void MasterClient::Update() RakSleep(timeout); ; players = mwmp::Networking::get().numberOfConnections(); - response = Send(motd, players, maxPlayers, update); + response = Send(hostname, std::__cxx11::string(), maxPlayers, update, players); update = true; } } +void MasterClient::Start() +{ + thrQuery = thread(&MasterClient::Update, this); +} + void MasterClient::Stop() { sRun = false; + if(thrQuery.joinable()) + thrQuery.join(); } void MasterClient::SetUpdateRate(unsigned int rate) @@ -169,4 +189,4 @@ void MasterClient::SetUpdateRate(unsigned int rate) else if (timeout > max_rate) timeout = max_rate; timeout = rate; -} +} \ No newline at end of file diff --git a/apps/openmw-mp/MasterClient.hpp b/apps/openmw-mp/MasterClient.hpp index c9b75b15b..c34c8b9e1 100644 --- a/apps/openmw-mp/MasterClient.hpp +++ b/apps/openmw-mp/MasterClient.hpp @@ -9,6 +9,7 @@ #include #include #include +#include class MasterClient { @@ -20,25 +21,30 @@ public: MasterClient(std::string queryAddr, unsigned short queryPort, std::string serverAddr, unsigned short serverPort); void SetPlayers(unsigned pl); void SetMaxPlayers(unsigned pl); - void SetMOTD(std::string &motd); + void SetHostname(std::string hostname); + void SetModname(std::string hostname); void Update(); + void Start(); void Stop(); void SetUpdateRate(unsigned int rate); private: - RakNet::RakString Send(std::string motd, unsigned players, unsigned maxPlayers, bool update = true); + RakNet::RakString + Send(std::string hostname, std::string modname, unsigned maxPlayers, bool update, unsigned players); private: std::string queryAddr; unsigned short queryPort; std::string serverAddr; unsigned short serverPort; - std::string motd; + std::string hostname; + std::string modname; unsigned players, maxPlayers; RakNet::HTTPConnection2 *httpConnection; RakNet::TCPInterface tcpInterface; unsigned int timeout; static bool sRun; std::mutex mutexData; + std::thread thrQuery; }; diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index a485d01be..0f1fea249 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -14,6 +14,7 @@ #include #include "Networking.hpp" +#include "MasterClient.hpp" using namespace mwmp; using namespace std; @@ -910,3 +911,14 @@ int Networking::getAvgPing(RakNet::AddressOrGUID addr) const { return peer->GetAveragePing(addr); } + +MasterClient *Networking::getMasterClient() +{ + return mclient; +} + +void Networking::InitQuery(std::string queryAddr, unsigned short queryPort, std::string serverAddr, + unsigned short serverPort) +{ + mclient = new MasterClient(queryAddr, (unsigned short) queryPort, serverAddr, (unsigned short) serverPort); +} diff --git a/apps/openmw-mp/Networking.hpp b/apps/openmw-mp/Networking.hpp index 7cfe01a74..6bad8b7bb 100644 --- a/apps/openmw-mp/Networking.hpp +++ b/apps/openmw-mp/Networking.hpp @@ -9,6 +9,7 @@ #include #include "Player.hpp" +class MasterClient; namespace mwmp { class Networking @@ -36,6 +37,9 @@ namespace mwmp PlayerPacketController *getPlayerController() const; WorldPacketController *getWorldController() const; + MasterClient *getMasterClient(); + void InitQuery(std::string queryAddr, unsigned short queryPort, std::string serverAddr, unsigned short serverPort); + static const Networking &get(); static Networking *getPtr(); @@ -44,6 +48,7 @@ namespace mwmp RakNet::RakPeerInterface *peer; RakNet::BitStream bsOut; TPlayers *players; + MasterClient *mclient; PlayerPacketController *playerController; WorldPacketController *worldController; diff --git a/apps/openmw-mp/main.cpp b/apps/openmw-mp/main.cpp index e3a4860f6..8320c97ae 100644 --- a/apps/openmw-mp/main.cpp +++ b/apps/openmw-mp/main.cpp @@ -11,12 +11,13 @@ #include #include #include -#include #include #include #include #include +#include "MasterClient.hpp" + #ifdef ENABLE_BREAKPAD #include #endif @@ -232,30 +233,25 @@ int main(int argc, char *argv[]) Networking networking(peer); - bool masterEnabled = mgr.getBool("enabled", "MasterServer"); - thread thrQuery; - MasterClient *mclient; - if (masterEnabled) + if ( mgr.getBool("enabled", "MasterServer")) { LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sharing server query info to master enabled."); string masterAddr = mgr.getString("address", "MasterServer"); int masterPort = mgr.getInt("port", "MasterServer"); - mclient = new MasterClient(masterAddr, (unsigned short) masterPort, addr, (unsigned short) port); - mclient->SetMaxPlayers((unsigned)players); - string motd = mgr.getString("motd", "General"); - mclient->SetMOTD(motd); - thrQuery = thread(queryThread, mclient); + + networking.InitQuery(masterAddr, (unsigned short) masterPort, addr, (unsigned short) port); + networking.getMasterClient()->SetMaxPlayers((unsigned)players); + string hostname = mgr.getString("hostname", "General"); + networking.getMasterClient()->SetHostname(hostname); + + networking.getMasterClient()->Start(); } int code = networking.mainLoop(); RakNet::RakPeerInterface::DestroyInstance(peer); - if (thrQuery.joinable()) - { - mclient->Stop(); - thrQuery.join(); - } + networking.getMasterClient()->Stop(); if (code == 0) LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Quitting peacefully."); diff --git a/files/tes3mp/tes3mp-server-default.cfg b/files/tes3mp/tes3mp-server-default.cfg index 43c9c5c32..3ced51c57 100644 --- a/files/tes3mp/tes3mp-server-default.cfg +++ b/files/tes3mp/tes3mp-server-default.cfg @@ -2,7 +2,7 @@ address = 0.0.0.0 port = 25565 players = 64 -motd = My TES3MP server +hostname = My TES3MP server # 0 - Verbose (spam), 1 - Info, 2 - Warnings, 3 - Errors, 4 - Only fatal errors loglevel = 0