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 <sstream>
#include <iostream>
#include <thread>
#include <RakPeerInterface.h>
#include "MasterClient.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),
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;
}
}

@ -9,6 +9,7 @@
#include <HTTPConnection2.h>
#include <TCPInterface.h>
#include <mutex>
#include <thread>
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;
};

@ -14,6 +14,7 @@
#include <thread>
#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);
}

@ -9,6 +9,7 @@
#include <components/openmw-mp/Controllers/WorldPacketController.hpp>
#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;

@ -11,12 +11,13 @@
#include <iostream>
#include <components/files/configurationmanager.hpp>
#include <components/settings/settings.hpp>
#include <thread>
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/stream_buffer.hpp>
#include <boost/filesystem/fstream.hpp>
#include <components/openmw-mp/Version.hpp>
#include "MasterClient.hpp"
#ifdef ENABLE_BREAKPAD
#include <handler/exception_handler.h>
#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.");

@ -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

Loading…
Cancel
Save