[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
new-script-api
Koncord 7 years ago
parent 0e2817da88
commit b869fe0b76

@ -12,19 +12,51 @@
#include <components/openmw-mp/Master/PacketMasterUpdate.hpp>
#include <components/openmw-mp/Master/PacketMasterAnnounce.hpp>
#include <components/openmw-mp/Version.hpp>
#include <components/openmw-mp/Utils.hpp>
#include <boost/filesystem.hpp>
using namespace RakNet;
using namespace std;
using namespace mwmp;
using namespace chrono;
MasterServer::MasterServer(unsigned short maxConnections, unsigned short port)
MasterServer::MasterServer(const std::string &luaScript)
{
state.open_libraries();
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, 0);
peer->Startup(maxConnections, &sockdescr, 1, 1000);
sockdescr = SocketDescriptor(port.as<unsigned short>(), nullptr);
peer->Startup(maxConnections.as<unsigned short>(), &sockdescr, 1, 1000);
peer->SetMaximumIncomingConnections(maxConnections);
peer->SetMaximumIncomingConnections(maxConnections.as<unsigned short>());
peer->SetIncomingPassword(TES3MP_MASTERSERVER_PASSW, (int) strlen(TES3MP_MASTERSERVER_PASSW));
run = false;
}
@ -67,9 +99,9 @@ void MasterServer::Thread()
servers.erase(it++);
else ++it;
}
for(auto id = pendingACKs.begin(); id != pendingACKs.end();)
for (auto id = pendingACKs.begin(); id != pendingACKs.end();)
{
if(now - id->second >= 30s)
if (now - id->second >= 30s)
{
cout << "timeout: " << peer->GetSystemAddressFromGuid(id->first).ToString() << endl;
peer->CloseConnection(id->first, true);
@ -235,6 +267,13 @@ MasterServer::ServerMap *MasterServer::GetServers()
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)
{
banned.push_back(addr);

@ -9,6 +9,8 @@
#include <chrono>
#include <RakPeerInterface.h>
#include <components/openmw-mp/Master/MasterData.hpp>
#include <extern/sol/sol.hpp>
#include <mutex>
class MasterServer
{
@ -21,7 +23,7 @@ public:
//typedef ServerMap::const_iterator ServerCIter;
typedef ServerMap::iterator ServerIter;
MasterServer(unsigned short maxConnections, unsigned short port);
explicit MasterServer(const std::string &luaScript);
~MasterServer();
void Start();
@ -30,6 +32,7 @@ public:
void Wait();
ServerMap* GetServers();
void luaStuff(std::function<void(sol::state &)> f);
void ban(const std::string &addr);
void unban(const std::string &addr);
@ -44,6 +47,8 @@ private:
ServerMap servers;
bool run;
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
};

@ -1,6 +1,7 @@
#include <iostream>
#include <Kbhit.h>
#include <RakSleep.h>
#include <extern/sol/sol.hpp>
#include "MasterServer.hpp"
#include "RestServer.hpp"
@ -8,19 +9,36 @@ using namespace RakNet;
using namespace std;
unique_ptr<RestServer> restServer;
unique_ptr<MasterServer> masterServer;
bool run = true;
shared_ptr<MasterServer> masterServer;
int main()
int main(int argc, char* argv[])
{
masterServer.reset(new MasterServer(2000, 25560));
restServer.reset(new RestServer(8080, masterServer->GetServers()));
if (argc != 2)
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*/){
restServer->stop();
masterServer->luaStuff([](sol::state &state) {
sol::protected_function func = state["OnExit"];
if (func.valid())
func.call();
});
masterServer->Stop(false);
masterServer->Wait();
run = false;
};
signal(SIGINT, onExit);
@ -28,9 +46,9 @@ int main()
masterServer->Start();
thread server_thread([]() { restServer->start(); });
thread rest_thread([]() { restServer->start();});
server_thread.join();
rest_thread.join();
masterServer->Wait();
return 0;

Loading…
Cancel
Save