From b869fe0b768be668b649e80ce93fbb43b325588f Mon Sep 17 00:00:00 2001 From: Koncord Date: Tue, 12 Sep 2017 20:50:28 +0800 Subject: [PATCH] [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 --- apps/master/MasterServer.cpp | 51 +++++++++++++++++++++++++++++++----- apps/master/MasterServer.hpp | 7 ++++- apps/master/main.cpp | 34 ++++++++++++++++++------ 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/apps/master/MasterServer.cpp b/apps/master/MasterServer.cpp index c5b460dfe..301e59c29 100644 --- a/apps/master/MasterServer.cpp +++ b/apps/master/MasterServer.cpp @@ -12,19 +12,51 @@ #include #include #include +#include +#include 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(), nullptr); + peer->Startup(maxConnections.as(), &sockdescr, 1, 1000); - peer->SetMaximumIncomingConnections(maxConnections); + peer->SetMaximumIncomingConnections(maxConnections.as()); 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 f) +{ + lock_guard lock(luaMutex); + f(state); +} + void MasterServer::ban(const std::string &addr) { banned.push_back(addr); diff --git a/apps/master/MasterServer.hpp b/apps/master/MasterServer.hpp index 55bc4aa07..2e20b6af2 100644 --- a/apps/master/MasterServer.hpp +++ b/apps/master/MasterServer.hpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include 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 f); void ban(const std::string &addr); void unban(const std::string &addr); @@ -44,6 +47,8 @@ private: ServerMap servers; bool run; std::map pendingACKs; + sol::state state; + std::mutex luaMutex; std::vector banned; // does not save on restart }; diff --git a/apps/master/main.cpp b/apps/master/main.cpp index b549eb420..0ca8d0e02 100644 --- a/apps/master/main.cpp +++ b/apps/master/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "MasterServer.hpp" #include "RestServer.hpp" @@ -8,19 +9,36 @@ using namespace RakNet; using namespace std; unique_ptr restServer; -unique_ptr masterServer; -bool run = true; +shared_ptr 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(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(restPort.as(), 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;