update players info

This commit is contained in:
Lion Kortlepel 2021-02-17 12:17:41 +01:00 committed by Anonymous275
parent bca4b3f140
commit 13f8be5d39
15 changed files with 135 additions and 44 deletions

View File

@ -56,7 +56,7 @@ private:
static inline std::deque<TShutdownHandler> mShutdownHandlers {}; static inline std::deque<TShutdownHandler> mShutdownHandlers {};
}; };
/* #ifndef DEBUG
static inline void warn(const std::string& str) { static inline void warn(const std::string& str) {
Application::Console().Write(std::string("[WARN] ") + str); Application::Console().Write(std::string("[WARN] ") + str);
} }
@ -72,9 +72,9 @@ static inline void debug(const std::string& str) {
} }
} }
static inline void luaprint(const std::string& str) { static inline void luaprint(const std::string& str) {
Application::Console().Write(std::string("[LUA] ") + str); Application::Console().WriteRaw(str);
} }
*/ #else // DEBUG
#define warn(x) Application::Console().Write(std::string(__PRETTY_FUNCTION__) + ":" + std::to_string(__LINE__) + std::string(" [WARN] ") + (x)) #define warn(x) Application::Console().Write(std::string(__PRETTY_FUNCTION__) + ":" + std::to_string(__LINE__) + std::string(" [WARN] ") + (x))
#define info(x) Application::Console().Write(std::string(__PRETTY_FUNCTION__) + ":" + std::to_string(__LINE__) + std::string(" [INFO] ") + (x)) #define info(x) Application::Console().Write(std::string(__PRETTY_FUNCTION__) + ":" + std::to_string(__LINE__) + std::string(" [INFO] ") + (x))
@ -86,6 +86,7 @@ static inline void luaprint(const std::string& str) {
Application::Console().Write(std::string(__PRETTY_FUNCTION__) + ":" + std::to_string(__LINE__) + std::string(" [DEBUG] ") + (x)); \ Application::Console().Write(std::string(__PRETTY_FUNCTION__) + ":" + std::to_string(__LINE__) + std::string(" [DEBUG] ") + (x)); \
} \ } \
} while (false) } while (false)
#endif // DEBUG
#define Biggest 30000 #define Biggest 30000
std::string Comp(std::string Data); std::string Comp(std::string Data);

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "TLuaFile.h"
#include "commandline/commandline.h" #include "commandline/commandline.h"
#include <atomic> #include <atomic>
#include <fstream> #include <fstream>
@ -9,7 +10,10 @@ public:
TConsole(); TConsole();
void Write(const std::string& str); void Write(const std::string& str);
void WriteRaw(const std::string& str);
void InitializeLuaConsole(TLuaEngine& Engine);
private: private:
std::unique_ptr<TLuaFile> mLuaConsole { nullptr };
Commandline mCommandline; Commandline mCommandline;
}; };

View File

@ -1,5 +1,4 @@
#ifndef TLUAENGINE_H #pragma once
#define TLUAENGINE_H
#include "Common.h" #include "Common.h"
#include "IThreaded.h" #include "IThreaded.h"
@ -12,6 +11,7 @@
class TLuaEngine : public IThreaded { class TLuaEngine : public IThreaded {
public: public:
explicit TLuaEngine(TServer& Server, TTCPServer& TCPServer, TUDPServer& UDPServer); explicit TLuaEngine(TServer& Server, TTCPServer& TCPServer, TUDPServer& UDPServer);
~TLuaEngine();
using TSetOfLuaFile = std::set<std::unique_ptr<TLuaFile>>; using TSetOfLuaFile = std::set<std::unique_ptr<TLuaFile>>;
@ -38,5 +38,3 @@ private:
bool mShutdown { false }; bool mShutdown { false };
TSetOfLuaFile mLuaFiles; TSetOfLuaFile mLuaFiles;
}; };
#endif // TLUAENGINE_H

View File

@ -11,6 +11,7 @@ class TResourceManager;
class TTCPServer : public IThreaded { class TTCPServer : public IThreaded {
public: public:
explicit TTCPServer(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager); explicit TTCPServer(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager);
~TTCPServer();
void operator()() override; void operator()() override;

View File

@ -10,6 +10,7 @@
class TUDPServer : public IThreaded { class TUDPServer : public IThreaded {
public: public:
explicit TUDPServer(TServer& Server, TPPSMonitor& PPSMonitor, TTCPServer& TCPServer); explicit TUDPServer(TServer& Server, TPPSMonitor& PPSMonitor, TTCPServer& TCPServer);
~TUDPServer();
void operator()() override; void operator()() override;
@ -21,6 +22,7 @@ private:
TPPSMonitor& mPPSMonitor; TPPSMonitor& mPPSMonitor;
TTCPServer& mTCPServer; TTCPServer& mTCPServer;
SOCKET mUDPSock {}; SOCKET mUDPSock {};
bool mShutdown { false };
std::string UDPRcvFromClient(sockaddr_in& client) const; std::string UDPRcvFromClient(sockaddr_in& client) const;
}; };

@ -1 +1 @@
Subproject commit 01ec8e0388ee4759813abf12c9619ac294721eab Subproject commit 19184df67e80cecb02de957d922477f7218b7703

View File

@ -16,6 +16,7 @@ void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
} }
void Application::GracefullyShutdown() { void Application::GracefullyShutdown() {
info("please wait while all subsystems are shutting down...");
std::unique_lock Lock(mShutdownHandlersMutex); std::unique_lock Lock(mShutdownHandlersMutex);
for (auto& Handler : mShutdownHandlers) { for (auto& Handler : mShutdownHandlers) {
Handler(); Handler();

View File

@ -43,6 +43,10 @@ TConsole::TConsole() {
mCommandline.enable_history(); mCommandline.enable_history();
mCommandline.set_history_limit(20); mCommandline.set_history_limit(20);
mCommandline.set_prompt("> "); mCommandline.set_prompt("> ");
bool success = mCommandline.enable_write_to_file("Server.log");
if (!success) {
error("unable to open file for writing: \"Server.log\"");
}
mCommandline.on_command = [this](Commandline& c) { mCommandline.on_command = [this](Commandline& c) {
auto cmd = c.get_command(); auto cmd = c.get_command();
mCommandline.write("> " + cmd); mCommandline.write("> " + cmd);
@ -52,7 +56,11 @@ TConsole::TConsole() {
} else if (cmd == "clear" || cmd == "cls") { } else if (cmd == "clear" || cmd == "cls") {
// TODO: clear screen // TODO: clear screen
} else { } else {
// TODO: execute as lua if (mLuaConsole) {
mLuaConsole->Execute(cmd);
} else {
error("Lua subsystem not yet initialized, please wait a few seconds and try again");
}
} }
}; };
} }
@ -62,4 +70,9 @@ void TConsole::Write(const std::string& str) {
mCommandline.write(ToWrite); mCommandline.write(ToWrite);
// TODO write to logfile, too // TODO write to logfile, too
} }
void TConsole::InitializeLuaConsole(TLuaEngine& Engine) {
mLuaConsole = std::make_unique<TLuaFile>(Engine, true);
}
void TConsole::WriteRaw(const std::string& str) {
mCommandline.write(str);
}

View File

@ -18,10 +18,12 @@ void THeartbeatThread::operator()() {
Body = GenerateCall(); Body = GenerateCall();
// a hot-change occurs when a setting has changed, to update the backend of that change. // a hot-change occurs when a setting has changed, to update the backend of that change.
auto Now = std::chrono::high_resolution_clock::now(); auto Now = std::chrono::high_resolution_clock::now();
if (Last == Body && (Now - LastNormalUpdateTime) < std::chrono::seconds(30)) { if (((Now - LastNormalUpdateTime) < std::chrono::seconds(5))
std::this_thread::sleep_for(std::chrono::seconds(5)); || (Last == Body && (Now - LastNormalUpdateTime) < std::chrono::seconds(30))) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue; continue;
} }
Last = Body; Last = Body;
LastNormalUpdateTime = Now; LastNormalUpdateTime = Now;
if (!Application::Settings.CustomIP.empty()) if (!Application::Settings.CustomIP.empty())
@ -77,7 +79,14 @@ std::string THeartbeatThread::GenerateCall() {
THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& Server) THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& Server)
: mResourceManager(ResourceManager) : mResourceManager(ResourceManager)
, mServer(Server) { , mServer(Server) {
Application::RegisterShutdownHandler([&] { mShutdown = true; }); Application::RegisterShutdownHandler([&] {
if (mThread.joinable()) {
debug("shutting down Heartbeat");
mShutdown = true;
mThread.join();
debug("shut down Heartbeat");
}
});
Start(); Start();
} }
std::string THeartbeatThread::GetPlayers() { std::string THeartbeatThread::GetPlayers() {
@ -91,7 +100,4 @@ std::string THeartbeatThread::GetPlayers() {
return Return; return Return;
} }
THeartbeatThread::~THeartbeatThread() { THeartbeatThread::~THeartbeatThread() {
if (mThread.joinable()) {
mThread.join();
}
} }

View File

@ -23,7 +23,12 @@ TLuaEngine::TLuaEngine(TServer& Server, TTCPServer& TCPServer, TUDPServer& UDPSe
} }
FolderList(Path, false); FolderList(Path, false);
mPath = Path; mPath = Path;
Application::RegisterShutdownHandler([&] { mShutdown = true; }); Application::RegisterShutdownHandler([&] {if (mThread.joinable()) {
debug("shutting down LuaEngine");
mShutdown = true;
mThread.join();
debug("shut down LuaEngine");
} });
Start(); Start();
} }
@ -96,3 +101,6 @@ bool TLuaEngine::NewFile(const std::string& Path) {
} }
return true; return true;
} }
TLuaEngine::~TLuaEngine() {
}

View File

@ -537,18 +537,44 @@ int lua_Set(lua_State* L) {
extern "C" { extern "C" {
int lua_Print(lua_State* L) { int lua_Print(lua_State* L) {
int Arg = lua_gettop(L); int Arg = lua_gettop(L);
std::string to_print;
for (int i = 1; i <= Arg; i++) { for (int i = 1; i <= Arg; i++) {
auto str = lua_tostring(L, i); if (lua_isstring(L, i)) {
if (str != nullptr) { to_print += lua_tostring(L, i);
luaprint(str + std::string(("\n"))); } else if (lua_isinteger(L, i)) {
to_print += std::to_string(lua_tointeger(L, 1));
} else if (lua_isnumber(L, i)) {
to_print += std::to_string(lua_tonumber(L, 1));
} else if (lua_isboolean(L, i)) {
to_print += lua_toboolean(L, i) ? "true" : "false";
} else if (lua_isfunction(L, i)) {
std::stringstream ss;
ss << std::hex << reinterpret_cast<const void*>(lua_tocfunction(L, i));
to_print += "function: " + ss.str();
} else if (lua_istable(L, i)) {
std::stringstream ss;
ss << std::hex << reinterpret_cast<const void*>(lua_topointer(L, i));
to_print += "table: " + ss.str();
} else if (lua_isnoneornil(L, i)) {
to_print += "nil";
} else if (lua_isthread(L, i)) {
std::stringstream ss;
ss << std::hex << reinterpret_cast<const void*>(lua_tothread(L, i));
to_print += "thread: " + ss.str();
} else { } else {
luaprint("nil\n"); to_print += "(unknown)";
}
if (i + 1 <= Arg) {
to_print += "\t";
} }
} }
luaprint(to_print);
return 0; return 0;
} }
} }
int lua_TempFix(lua_State* L);
TLuaFile::TLuaFile(TLuaEngine& Engine, const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote, bool Console) TLuaFile::TLuaFile(TLuaEngine& Engine, const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote, bool Console)
: mEngine(Engine) : mEngine(Engine)
, mLuaState(luaL_newstate()) { , mLuaState(luaL_newstate()) {
@ -564,13 +590,14 @@ TLuaFile::TLuaFile(TLuaEngine& Engine, const std::string& PluginName, const std:
SetFileName(FileName); SetFileName(FileName);
} }
SetLastWrite(LastWrote); SetLastWrite(LastWrote);
mConsole = Console; Init();
} }
TLuaFile::TLuaFile(TLuaEngine& Engine, bool Console) TLuaFile::TLuaFile(TLuaEngine& Engine, bool Console)
: mEngine(Engine) : mEngine(Engine)
, mLuaState(luaL_newstate()) { , mLuaState(luaL_newstate()) {
mConsole = Console; mConsole = Console;
Init();
} }
void TLuaFile::Execute(const std::string& Command) { void TLuaFile::Execute(const std::string& Command) {
@ -614,23 +641,7 @@ void TLuaFile::SetPluginName(const std::string& Name) {
void TLuaFile::SetFileName(const std::string& Name) { void TLuaFile::SetFileName(const std::string& Name) {
mFileName = Name; mFileName = Name;
} }
int lua_TempFix(lua_State* L) {
if (lua_isnumber(L, 1)) {
int ID = int(lua_tonumber(L, 1));
auto MaybeClient = GetClient(Engine().Server(), ID);
if (!MaybeClient || MaybeClient.value().expired())
return 0;
std::string Ret;
auto c = MaybeClient.value().lock();
if (c->IsGuest()) {
Ret = "Guest-" + c->GetName();
} else
Ret = c->GetName();
lua_pushstring(L, Ret.c_str());
} else
SendError(Engine(), L, "GetDID not enough arguments");
return 1;
}
void TLuaFile::Init() { void TLuaFile::Init() {
Assert(mLuaState); Assert(mLuaState);
luaL_openlibs(mLuaState); luaL_openlibs(mLuaState);
@ -722,6 +733,23 @@ void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) {
} }
warn(a + (" | Incorrect Call of ") + msg); warn(a + (" | Incorrect Call of ") + msg);
} }
int lua_TempFix(lua_State* L) {
if (lua_isnumber(L, 1)) {
int ID = int(lua_tonumber(L, 1));
auto MaybeClient = GetClient(Engine().Server(), ID);
if (!MaybeClient || MaybeClient.value().expired())
return 0;
std::string Ret;
auto c = MaybeClient.value().lock();
if (c->IsGuest()) {
Ret = "Guest-" + c->GetName();
} else
Ret = c->GetName();
lua_pushstring(L, Ret.c_str());
} else
SendError(Engine(), L, "GetDID not enough arguments");
return 1;
}
void TLuaArg::PushArgs(lua_State* State) { void TLuaArg::PushArgs(lua_State* State) {
for (std::any arg : args) { for (std::any arg : args) {

View File

@ -4,7 +4,14 @@
TPPSMonitor::TPPSMonitor(TServer& Server) TPPSMonitor::TPPSMonitor(TServer& Server)
: mServer(Server) { : mServer(Server) {
Application::SetPPS("-"); Application::SetPPS("-");
Application::RegisterShutdownHandler([&] { mShutdown = true; }); Application::RegisterShutdownHandler([&] {
if (mThread.joinable()) {
debug("shutting down PPSMonitor");
mShutdown = true;
mThread.join();
debug("shut down PPSMonitor");
}
});
Start(); Start();
} }
void TPPSMonitor::operator()() { void TPPSMonitor::operator()() {

View File

@ -19,7 +19,13 @@ TTCPServer::TTCPServer(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManage
: mServer(Server) : mServer(Server)
, mPPSMonitor(PPSMonitor) , mPPSMonitor(PPSMonitor)
, mResourceManager(ResourceManager) { , mResourceManager(ResourceManager) {
Application::RegisterShutdownHandler([this] { mShutdown = true; }); Application::RegisterShutdownHandler([&] {if (mThread.joinable()) {
debug("shutting down TCPServer");
mShutdown = true;
// FIXME: Join once TCPServer can timeout on a read, accept, etc.
mThread.detach();
debug("shut down TCPServer");
} });
Start(); Start();
} }
@ -282,6 +288,7 @@ void TTCPServer::TCPClient(std::weak_ptr<TClient> c) {
} }
void TTCPServer::UpdatePlayers() { void TTCPServer::UpdatePlayers() {
debug("Update Players!");
std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::Settings.MaxPlayers) + ":"; std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::Settings.MaxPlayers) + ":";
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
@ -368,8 +375,8 @@ void TTCPServer::SyncResources(TClient& c) {
} }
#ifndef DEBUG #ifndef DEBUG
} catch (std::exception& e) { } catch (std::exception& e) {
except("Exception! : " + std::string(e.what())); error("Exception! : " + std::string(e.what()));
c->SetStatus(-1); c.SetStatus(-1);
} }
#endif #endif
} }
@ -651,3 +658,5 @@ void TTCPServer::operator()() {
CloseSocketProper(client); CloseSocketProper(client);
#endif #endif
} }
TTCPServer::~TTCPServer() {
}

View File

@ -9,6 +9,13 @@ TUDPServer::TUDPServer(TServer& Server, TPPSMonitor& PPSMonitor, TTCPServer& TCP
: mServer(Server) : mServer(Server)
, mPPSMonitor(PPSMonitor) , mPPSMonitor(PPSMonitor)
, mTCPServer(TCPServer) { , mTCPServer(TCPServer) {
Application::RegisterShutdownHandler([&] {if (mThread.joinable()) {
debug("shutting down UDPServer");
mShutdown = true;
// FIXME: Once we use boost for UDP, set up a timeout so this doesn't block
mThread.detach();
debug("shut down UDPServer");
} });
Start(); Start();
} }
@ -53,7 +60,7 @@ void TUDPServer::operator()() {
info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ") info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ")
+ std::to_string(Application::Settings.MaxPlayers) + (" Clients")); + std::to_string(Application::Settings.MaxPlayers) + (" Clients"));
while (true) { while (!mShutdown) {
try { try {
sockaddr_in client {}; sockaddr_in client {};
std::string Data = UDPRcvFromClient(client); //Receives any data from Socket std::string Data = UDPRcvFromClient(client); //Receives any data from Socket
@ -163,3 +170,6 @@ std::string TUDPServer::UDPRcvFromClient(sockaddr_in& client) const {
} }
return std::string(Ret.begin(), Ret.begin() + Rcv); return std::string(Ret.begin(), Ret.begin() + Rcv);
} }
TUDPServer::~TUDPServer() {
}

View File

@ -38,7 +38,9 @@ int main(int argc, char** argv) {
info("registering handlers for SIGINT, SIGTERM, SIGPIPE"); info("registering handlers for SIGINT, SIGTERM, SIGPIPE");
signal(SIGPIPE, UnixSignalHandler); signal(SIGPIPE, UnixSignalHandler);
signal(SIGTERM, UnixSignalHandler); signal(SIGTERM, UnixSignalHandler);
#ifndef DEBUG
signal(SIGINT, UnixSignalHandler); signal(SIGINT, UnixSignalHandler);
#endif // DEBUG
#endif // __unix #endif // __unix
bool Shutdown = false; bool Shutdown = false;
@ -53,6 +55,7 @@ int main(int argc, char** argv) {
TUDPServer UDPServer(Server, PPSMonitor, TCPServer); TUDPServer UDPServer(Server, PPSMonitor, TCPServer);
TLuaEngine LuaEngine(Server, TCPServer, UDPServer); TLuaEngine LuaEngine(Server, TCPServer, UDPServer);
TCPServer.SetUDPServer(UDPServer); TCPServer.SetUDPServer(UDPServer);
Application::Console().InitializeLuaConsole(LuaEngine);
// TODO: replace // TODO: replace
while (!Shutdown) { while (!Shutdown) {