mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-31 05:45:32 +00:00
[Master] Add Lua
Init master server values by config table in script Add BanAddress/UnbanAddress to lua Fix a couple of indents Pass lua script as first launch parameter of master server
This commit is contained in:
parent
0e2817da88
commit
b869fe0b76
3 changed files with 78 additions and 16 deletions
|
@ -12,19 +12,51 @@
|
||||||
#include <components/openmw-mp/Master/PacketMasterUpdate.hpp>
|
#include <components/openmw-mp/Master/PacketMasterUpdate.hpp>
|
||||||
#include <components/openmw-mp/Master/PacketMasterAnnounce.hpp>
|
#include <components/openmw-mp/Master/PacketMasterAnnounce.hpp>
|
||||||
#include <components/openmw-mp/Version.hpp>
|
#include <components/openmw-mp/Version.hpp>
|
||||||
|
#include <components/openmw-mp/Utils.hpp>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
using namespace RakNet;
|
using namespace RakNet;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace mwmp;
|
using namespace mwmp;
|
||||||
using namespace chrono;
|
using namespace chrono;
|
||||||
|
|
||||||
MasterServer::MasterServer(unsigned short maxConnections, unsigned short port)
|
MasterServer::MasterServer(const std::string &luaScript)
|
||||||
{
|
{
|
||||||
peer = RakPeerInterface::GetInstance();
|
state.open_libraries();
|
||||||
sockdescr = SocketDescriptor(port, 0);
|
|
||||||
peer->Startup(maxConnections, &sockdescr, 1, 1000);
|
|
||||||
|
|
||||||
peer->SetMaximumIncomingConnections(maxConnections);
|
boost::filesystem::path absPath = boost::filesystem::absolute(luaScript);
|
||||||
|
|
||||||
|
std::string package_path = state["package"]["path"];
|
||||||
|
state["package"]["path"] = Utils::convertPath(absPath.parent_path().string() + "/?.lua") + ";" + package_path;
|
||||||
|
|
||||||
|
state.script_file(luaScript);
|
||||||
|
|
||||||
|
sol::table config = state["config"];
|
||||||
|
|
||||||
|
if (config.get_type() != sol::type::table)
|
||||||
|
throw runtime_error("config is not correct");
|
||||||
|
|
||||||
|
sol::object maxConnections = config["maxConnections"];
|
||||||
|
if (maxConnections.get_type() != sol::type::number)
|
||||||
|
throw runtime_error("config.maxConnections is not correct");
|
||||||
|
|
||||||
|
sol::object port = config["port"];
|
||||||
|
if (port.get_type() != sol::type::number)
|
||||||
|
throw runtime_error("config.port is not correct");
|
||||||
|
|
||||||
|
state.set_function("BanAddress", [this](const string &address) {
|
||||||
|
this->ban(address);
|
||||||
|
});
|
||||||
|
|
||||||
|
state.set_function("UnbanAddress", [this](const string &address) {
|
||||||
|
this->unban(address);
|
||||||
|
});
|
||||||
|
|
||||||
|
peer = RakPeerInterface::GetInstance();
|
||||||
|
sockdescr = SocketDescriptor(port.as<unsigned short>(), nullptr);
|
||||||
|
peer->Startup(maxConnections.as<unsigned short>(), &sockdescr, 1, 1000);
|
||||||
|
|
||||||
|
peer->SetMaximumIncomingConnections(maxConnections.as<unsigned short>());
|
||||||
peer->SetIncomingPassword(TES3MP_MASTERSERVER_PASSW, (int) strlen(TES3MP_MASTERSERVER_PASSW));
|
peer->SetIncomingPassword(TES3MP_MASTERSERVER_PASSW, (int) strlen(TES3MP_MASTERSERVER_PASSW));
|
||||||
run = false;
|
run = false;
|
||||||
}
|
}
|
||||||
|
@ -235,6 +267,13 @@ MasterServer::ServerMap *MasterServer::GetServers()
|
||||||
return &servers;
|
return &servers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MasterServer::luaStuff(std::function<void(sol::state &)> f)
|
||||||
|
{
|
||||||
|
lock_guard<mutex> lock(luaMutex);
|
||||||
|
f(state);
|
||||||
|
}
|
||||||
|
|
||||||
void MasterServer::ban(const std::string &addr)
|
void MasterServer::ban(const std::string &addr)
|
||||||
{
|
{
|
||||||
banned.push_back(addr);
|
banned.push_back(addr);
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <RakPeerInterface.h>
|
#include <RakPeerInterface.h>
|
||||||
#include <components/openmw-mp/Master/MasterData.hpp>
|
#include <components/openmw-mp/Master/MasterData.hpp>
|
||||||
|
#include <extern/sol/sol.hpp>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
class MasterServer
|
class MasterServer
|
||||||
{
|
{
|
||||||
|
@ -21,7 +23,7 @@ public:
|
||||||
//typedef ServerMap::const_iterator ServerCIter;
|
//typedef ServerMap::const_iterator ServerCIter;
|
||||||
typedef ServerMap::iterator ServerIter;
|
typedef ServerMap::iterator ServerIter;
|
||||||
|
|
||||||
MasterServer(unsigned short maxConnections, unsigned short port);
|
explicit MasterServer(const std::string &luaScript);
|
||||||
~MasterServer();
|
~MasterServer();
|
||||||
|
|
||||||
void Start();
|
void Start();
|
||||||
|
@ -30,6 +32,7 @@ public:
|
||||||
void Wait();
|
void Wait();
|
||||||
|
|
||||||
ServerMap* GetServers();
|
ServerMap* GetServers();
|
||||||
|
void luaStuff(std::function<void(sol::state &)> f);
|
||||||
|
|
||||||
void ban(const std::string &addr);
|
void ban(const std::string &addr);
|
||||||
void unban(const std::string &addr);
|
void unban(const std::string &addr);
|
||||||
|
@ -44,6 +47,8 @@ private:
|
||||||
ServerMap servers;
|
ServerMap servers;
|
||||||
bool run;
|
bool run;
|
||||||
std::map<RakNet::RakNetGUID, std::chrono::steady_clock::time_point> pendingACKs;
|
std::map<RakNet::RakNetGUID, std::chrono::steady_clock::time_point> pendingACKs;
|
||||||
|
sol::state state;
|
||||||
|
std::mutex luaMutex;
|
||||||
std::vector<std::string> banned; // does not save on restart
|
std::vector<std::string> banned; // does not save on restart
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <Kbhit.h>
|
#include <Kbhit.h>
|
||||||
#include <RakSleep.h>
|
#include <RakSleep.h>
|
||||||
|
#include <extern/sol/sol.hpp>
|
||||||
#include "MasterServer.hpp"
|
#include "MasterServer.hpp"
|
||||||
#include "RestServer.hpp"
|
#include "RestServer.hpp"
|
||||||
|
|
||||||
|
@ -8,19 +9,36 @@ using namespace RakNet;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
unique_ptr<RestServer> restServer;
|
unique_ptr<RestServer> restServer;
|
||||||
unique_ptr<MasterServer> masterServer;
|
shared_ptr<MasterServer> masterServer;
|
||||||
bool run = true;
|
|
||||||
|
|
||||||
int main()
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
masterServer.reset(new MasterServer(2000, 25560));
|
if (argc != 2)
|
||||||
restServer.reset(new RestServer(8080, masterServer->GetServers()));
|
return 1;
|
||||||
|
|
||||||
|
string luaScript(argv[1]);
|
||||||
|
|
||||||
|
masterServer = make_shared<MasterServer>(luaScript);
|
||||||
|
masterServer->luaStuff([](sol::state &state)
|
||||||
|
{
|
||||||
|
sol::table config = state["config"];
|
||||||
|
sol::object restPort = config["restPort"];
|
||||||
|
if (restPort.get_type() != sol::type::number)
|
||||||
|
throw runtime_error("config.restPort is not correct");
|
||||||
|
|
||||||
|
|
||||||
|
restServer = make_unique<RestServer>(restPort.as<unsigned short>(), masterServer->GetServers());
|
||||||
|
});
|
||||||
|
|
||||||
auto onExit = [](int /*sig*/){
|
auto onExit = [](int /*sig*/){
|
||||||
restServer->stop();
|
restServer->stop();
|
||||||
|
masterServer->luaStuff([](sol::state &state) {
|
||||||
|
sol::protected_function func = state["OnExit"];
|
||||||
|
if (func.valid())
|
||||||
|
func.call();
|
||||||
|
});
|
||||||
masterServer->Stop(false);
|
masterServer->Stop(false);
|
||||||
masterServer->Wait();
|
masterServer->Wait();
|
||||||
run = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
signal(SIGINT, onExit);
|
signal(SIGINT, onExit);
|
||||||
|
@ -28,9 +46,9 @@ int main()
|
||||||
|
|
||||||
masterServer->Start();
|
masterServer->Start();
|
||||||
|
|
||||||
thread server_thread([]() { restServer->start(); });
|
thread rest_thread([]() { restServer->start();});
|
||||||
|
|
||||||
server_thread.join();
|
rest_thread.join();
|
||||||
masterServer->Wait();
|
masterServer->Wait();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue