Move MasterClient to Networking class

Rename MOTD to hostname
coverity_scan^2
Koncord 8 years ago
parent b5c586d5b7
commit f510a5583f

@ -6,6 +6,7 @@
#include <Getche.h> #include <Getche.h>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <thread>
#include <RakPeerInterface.h> #include <RakPeerInterface.h>
#include "MasterClient.hpp" #include "MasterClient.hpp"
#include <components/openmw-mp/Log.hpp> #include <components/openmw-mp/Log.hpp>
@ -19,12 +20,10 @@ MasterClient::MasterClient(std::string queryAddr, unsigned short queryPort, std:
unsigned short serverPort) : queryAddr(queryAddr), queryPort(queryPort), unsigned short serverPort) : queryAddr(queryAddr), queryPort(queryPort),
serverAddr(serverAddr), serverPort(serverPort) serverAddr(serverAddr), serverPort(serverPort)
{ {
httpConnection = RakNet::HTTPConnection2::GetInstance();
tcpInterface.Start(0, 64);
tcpInterface.AttachPlugin(httpConnection);
players = 0; players = 0;
maxPlayers = 0; maxPlayers = 0;
motd = ""; hostname = "";
modname = "";
timeout = 1000; // every 1 seconds timeout = 1000; // every 1 seconds
} }
@ -42,14 +41,22 @@ void MasterClient::SetMaxPlayers(unsigned pl)
mutexData.unlock(); mutexData.unlock();
} }
void MasterClient::SetMOTD(std::string &motd) void MasterClient::SetHostname(std::string hostname)
{ {
mutexData.lock(); mutexData.lock();
this->motd = motd.substr(0, 200); this->hostname = hostname.substr(0, 200);
mutexData.unlock(); 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 unsigned short oldServerPort, oldQueryPort;
static string oldMotd; static string oldMotd;
@ -59,7 +66,8 @@ RakNet::RakString MasterClient::Send(std::string motd, unsigned players, unsigne
sstr << "{"; sstr << "{";
sstr << "\"port\": " << serverPort << ", "; sstr << "\"port\": " << serverPort << ", ";
sstr << "\"query_port\": " << queryPort << ", "; sstr << "\"query_port\": " << queryPort << ", ";
sstr << "\"motd\": \"" << motd << "\", "; sstr << "\"hostname\": \"" << hostname.c_str() << "\", ";
sstr << "\"hostname\": \"" << modname.c_str() << "\", ";
sstr << "\"players\": " << players << ", "; sstr << "\"players\": " << players << ", ";
sstr << "\"max_players\": " << maxPlayers; sstr << "\"max_players\": " << maxPlayers;
sstr << "}"; sstr << "}";
@ -126,7 +134,12 @@ RakNet::RakString MasterClient::Send(std::string motd, unsigned players, unsigne
void MasterClient::Update() void MasterClient::Update()
{ {
assert(!sRun); 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; bool update = true;
sRun = true; sRun = true;
while (sRun) while (sRun)
@ -151,15 +164,22 @@ void MasterClient::Update()
RakSleep(timeout); RakSleep(timeout);
; ;
players = mwmp::Networking::get().numberOfConnections(); players = mwmp::Networking::get().numberOfConnections();
response = Send(motd, players, maxPlayers, update); response = Send(hostname, std::__cxx11::string(), maxPlayers, update, players);
update = true; update = true;
} }
} }
void MasterClient::Start()
{
thrQuery = thread(&MasterClient::Update, this);
}
void MasterClient::Stop() void MasterClient::Stop()
{ {
sRun = false; sRun = false;
if(thrQuery.joinable())
thrQuery.join();
} }
void MasterClient::SetUpdateRate(unsigned int rate) void MasterClient::SetUpdateRate(unsigned int rate)
@ -169,4 +189,4 @@ void MasterClient::SetUpdateRate(unsigned int rate)
else if (timeout > max_rate) else if (timeout > max_rate)
timeout = max_rate; timeout = max_rate;
timeout = rate; timeout = rate;
} }

@ -9,6 +9,7 @@
#include <HTTPConnection2.h> #include <HTTPConnection2.h>
#include <TCPInterface.h> #include <TCPInterface.h>
#include <mutex> #include <mutex>
#include <thread>
class MasterClient class MasterClient
{ {
@ -20,25 +21,30 @@ public:
MasterClient(std::string queryAddr, unsigned short queryPort, std::string serverAddr, unsigned short serverPort); MasterClient(std::string queryAddr, unsigned short queryPort, std::string serverAddr, unsigned short serverPort);
void SetPlayers(unsigned pl); void SetPlayers(unsigned pl);
void SetMaxPlayers(unsigned pl); void SetMaxPlayers(unsigned pl);
void SetMOTD(std::string &motd); void SetHostname(std::string hostname);
void SetModname(std::string hostname);
void Update(); void Update();
void Start();
void Stop(); void Stop();
void SetUpdateRate(unsigned int rate); void SetUpdateRate(unsigned int rate);
private: 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: private:
std::string queryAddr; std::string queryAddr;
unsigned short queryPort; unsigned short queryPort;
std::string serverAddr; std::string serverAddr;
unsigned short serverPort; unsigned short serverPort;
std::string motd; std::string hostname;
std::string modname;
unsigned players, maxPlayers; unsigned players, maxPlayers;
RakNet::HTTPConnection2 *httpConnection; RakNet::HTTPConnection2 *httpConnection;
RakNet::TCPInterface tcpInterface; RakNet::TCPInterface tcpInterface;
unsigned int timeout; unsigned int timeout;
static bool sRun; static bool sRun;
std::mutex mutexData; std::mutex mutexData;
std::thread thrQuery;
}; };

@ -14,6 +14,7 @@
#include <thread> #include <thread>
#include "Networking.hpp" #include "Networking.hpp"
#include "MasterClient.hpp"
using namespace mwmp; using namespace mwmp;
using namespace std; using namespace std;
@ -910,3 +911,14 @@ int Networking::getAvgPing(RakNet::AddressOrGUID addr) const
{ {
return peer->GetAveragePing(addr); 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);
}

@ -9,6 +9,7 @@
#include <components/openmw-mp/Controllers/WorldPacketController.hpp> #include <components/openmw-mp/Controllers/WorldPacketController.hpp>
#include "Player.hpp" #include "Player.hpp"
class MasterClient;
namespace mwmp namespace mwmp
{ {
class Networking class Networking
@ -36,6 +37,9 @@ namespace mwmp
PlayerPacketController *getPlayerController() const; PlayerPacketController *getPlayerController() const;
WorldPacketController *getWorldController() 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 const Networking &get();
static Networking *getPtr(); static Networking *getPtr();
@ -44,6 +48,7 @@ namespace mwmp
RakNet::RakPeerInterface *peer; RakNet::RakPeerInterface *peer;
RakNet::BitStream bsOut; RakNet::BitStream bsOut;
TPlayers *players; TPlayers *players;
MasterClient *mclient;
PlayerPacketController *playerController; PlayerPacketController *playerController;
WorldPacketController *worldController; WorldPacketController *worldController;

@ -11,12 +11,13 @@
#include <iostream> #include <iostream>
#include <components/files/configurationmanager.hpp> #include <components/files/configurationmanager.hpp>
#include <components/settings/settings.hpp> #include <components/settings/settings.hpp>
#include <thread>
#include <boost/iostreams/concepts.hpp> #include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/stream_buffer.hpp> #include <boost/iostreams/stream_buffer.hpp>
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
#include <components/openmw-mp/Version.hpp> #include <components/openmw-mp/Version.hpp>
#include "MasterClient.hpp"
#ifdef ENABLE_BREAKPAD #ifdef ENABLE_BREAKPAD
#include <handler/exception_handler.h> #include <handler/exception_handler.h>
#endif #endif
@ -232,30 +233,25 @@ int main(int argc, char *argv[])
Networking networking(peer); Networking networking(peer);
bool masterEnabled = mgr.getBool("enabled", "MasterServer"); if ( mgr.getBool("enabled", "MasterServer"))
thread thrQuery;
MasterClient *mclient;
if (masterEnabled)
{ {
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sharing server query info to master enabled."); LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sharing server query info to master enabled.");
string masterAddr = mgr.getString("address", "MasterServer"); string masterAddr = mgr.getString("address", "MasterServer");
int masterPort = mgr.getInt("port", "MasterServer"); int masterPort = mgr.getInt("port", "MasterServer");
mclient = new MasterClient(masterAddr, (unsigned short) masterPort, addr, (unsigned short) port);
mclient->SetMaxPlayers((unsigned)players); networking.InitQuery(masterAddr, (unsigned short) masterPort, addr, (unsigned short) port);
string motd = mgr.getString("motd", "General"); networking.getMasterClient()->SetMaxPlayers((unsigned)players);
mclient->SetMOTD(motd); string hostname = mgr.getString("hostname", "General");
thrQuery = thread(queryThread, mclient); networking.getMasterClient()->SetHostname(hostname);
networking.getMasterClient()->Start();
} }
int code = networking.mainLoop(); int code = networking.mainLoop();
RakNet::RakPeerInterface::DestroyInstance(peer); RakNet::RakPeerInterface::DestroyInstance(peer);
if (thrQuery.joinable()) networking.getMasterClient()->Stop();
{
mclient->Stop();
thrQuery.join();
}
if (code == 0) if (code == 0)
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Quitting peacefully."); LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Quitting peacefully.");

@ -2,7 +2,7 @@
address = 0.0.0.0 address = 0.0.0.0
port = 25565 port = 25565
players = 64 players = 64
motd = My TES3MP server hostname = My TES3MP server
# 0 - Verbose (spam), 1 - Info, 2 - Warnings, 3 - Errors, 4 - Only fatal errors # 0 - Verbose (spam), 1 - Info, 2 - Warnings, 3 - Errors, 4 - Only fatal errors
loglevel = 0 loglevel = 0

Loading…
Cancel
Save