mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-10-28 15:00:05 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86e662209e |
No files matched your search
+5
-3
@@ -75,7 +75,7 @@ public:
|
|||||||
static void RegisterShutdownHandler(const TShutdownHandler& Handler);
|
static void RegisterShutdownHandler(const TShutdownHandler& Handler);
|
||||||
// Causes all threads to finish up and exit gracefull gracefully
|
// Causes all threads to finish up and exit gracefull gracefully
|
||||||
static void GracefullyShutdown();
|
static void GracefullyShutdown();
|
||||||
static TConsole& Console() { return mConsole; }
|
static TConsole& Console() { return *mConsole; }
|
||||||
static std::string ServerVersionString();
|
static std::string ServerVersionString();
|
||||||
static const Version& ServerVersion() { return mVersion; }
|
static const Version& ServerVersion() { return mVersion; }
|
||||||
static uint8_t ClientMajorVersion() { return 2; }
|
static uint8_t ClientMajorVersion() { return 2; }
|
||||||
@@ -101,7 +101,9 @@ public:
|
|||||||
static void SleepSafeSeconds(size_t Seconds);
|
static void SleepSafeSeconds(size_t Seconds);
|
||||||
|
|
||||||
static void InitializeConsole() {
|
static void InitializeConsole() {
|
||||||
mConsole.InitializeCommandline();
|
if (!mConsole) {
|
||||||
|
mConsole = std::make_unique<TConsole>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class Status {
|
enum class Status {
|
||||||
@@ -127,7 +129,7 @@ private:
|
|||||||
static inline SystemStatusMap mSystemStatusMap {};
|
static inline SystemStatusMap mSystemStatusMap {};
|
||||||
static inline std::mutex mSystemStatusMapMutex {};
|
static inline std::mutex mSystemStatusMapMutex {};
|
||||||
static inline std::string mPPS;
|
static inline std::string mPPS;
|
||||||
static inline TConsole mConsole;
|
static inline std::unique_ptr<TConsole> mConsole;
|
||||||
static inline std::shared_mutex mShutdownMtx {};
|
static inline std::shared_mutex mShutdownMtx {};
|
||||||
static inline bool mShutdown { false };
|
static inline bool mShutdown { false };
|
||||||
static inline std::mutex mShutdownHandlersMutex {};
|
static inline std::mutex mShutdownHandlersMutex {};
|
||||||
|
|||||||
+2
-5
@@ -17,15 +17,12 @@ class TConsole {
|
|||||||
public:
|
public:
|
||||||
TConsole();
|
TConsole();
|
||||||
|
|
||||||
// Initializes the commandline app to take over I/O
|
|
||||||
void InitializeCommandline();
|
|
||||||
|
|
||||||
void Write(const std::string& str);
|
void Write(const std::string& str);
|
||||||
void WriteRaw(const std::string& str);
|
void WriteRaw(const std::string& str);
|
||||||
void InitializeLuaConsole(TLuaEngine& Engine);
|
void InitializeLuaConsole(TLuaEngine& Engine);
|
||||||
void BackupOldLog();
|
void BackupOldLog();
|
||||||
void StartLoggingToFile();
|
void StartLoggingToFile();
|
||||||
Commandline& Internal() { return *mCommandline; }
|
Commandline& Internal() { return mCommandline; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void RunAsCommand(const std::string& cmd, bool IgnoreNotACommand = false);
|
void RunAsCommand(const std::string& cmd, bool IgnoreNotACommand = false);
|
||||||
@@ -59,7 +56,7 @@ private:
|
|||||||
{ "say", [this](const auto&, const auto&) { Command_Say(""); } }, // shouldn't actually be called
|
{ "say", [this](const auto&, const auto&) { Command_Say(""); } }, // shouldn't actually be called
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unique_ptr<Commandline> mCommandline { nullptr };
|
Commandline mCommandline;
|
||||||
std::vector<std::string> mCachedLuaHistory;
|
std::vector<std::string> mCachedLuaHistory;
|
||||||
std::vector<std::string> mCachedRegularHistory;
|
std::vector<std::string> mCachedRegularHistory;
|
||||||
TLuaEngine* mLuaEngine { nullptr };
|
TLuaEngine* mLuaEngine { nullptr };
|
||||||
|
|||||||
+11
-24
@@ -10,30 +10,15 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
// TODO: Add sentry error handling back
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
static std::shared_ptr<httplib::SSLClient> GetOrCreateClient(const std::string& host, int port) {
|
|
||||||
static thread_local std::unordered_map<std::pair<std::string, int>, std::shared_ptr<httplib::SSLClient>> clients {};
|
|
||||||
const auto idx = std::pair<std::string, int>(host, port);
|
|
||||||
if (!clients.contains(idx)) {
|
|
||||||
beammp_tracef("Creating connection to {}:{}", host, port);
|
|
||||||
clients.emplace(std::make_shared<httplib::SSLClient>(host, port));
|
|
||||||
auto client = clients.at(idx);
|
|
||||||
client->enable_server_certificate_verification(false);
|
|
||||||
client->set_address_family(AF_INET);
|
|
||||||
client->set_read_timeout(std::chrono::seconds(10));
|
|
||||||
return client;
|
|
||||||
} else {
|
|
||||||
beammp_tracef("Reusing connection to {}:{}", host, port);
|
|
||||||
return clients.at(idx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Http::GET(const std::string& host, int port, const std::string& target, unsigned int* status) {
|
std::string Http::GET(const std::string& host, int port, const std::string& target, unsigned int* status) {
|
||||||
auto client = GetOrCreateClient(host, port);
|
httplib::SSLClient client(host, port);
|
||||||
beammp_assert(client != nullptr);
|
client.enable_server_certificate_verification(false);
|
||||||
beammp_assert(client.is_valid());
|
client.set_address_family(AF_INET);
|
||||||
auto res = client->Get(target.c_str());
|
auto res = client.Get(target.c_str());
|
||||||
if (res) {
|
if (res) {
|
||||||
if (status) {
|
if (status) {
|
||||||
*status = res->status;
|
*status = res->status;
|
||||||
@@ -45,10 +30,12 @@ std::string Http::GET(const std::string& host, int port, const std::string& targ
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string Http::POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status, const httplib::Headers& headers) {
|
std::string Http::POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status, const httplib::Headers& headers) {
|
||||||
auto client = GetOrCreateClient(host, port);
|
httplib::SSLClient client(host, port);
|
||||||
beammp_assert(client != nullptr);
|
client.set_read_timeout(std::chrono::seconds(10));
|
||||||
beammp_assert(client.is_valid());
|
beammp_assert(client.is_valid());
|
||||||
auto res = client->Post(target.c_str(), headers, body.c_str(), body.size(), ContentType.c_str());
|
client.enable_server_certificate_verification(false);
|
||||||
|
client.set_address_family(AF_INET);
|
||||||
|
auto res = client.Post(target.c_str(), headers, body.c_str(), body.size(), ContentType.c_str());
|
||||||
if (res) {
|
if (res) {
|
||||||
if (status) {
|
if (status) {
|
||||||
*status = res->status;
|
*status = res->status;
|
||||||
|
|||||||
+16
-30
@@ -157,13 +157,13 @@ void TConsole::ChangeToLuaConsole(const std::string& LuaStateId) {
|
|||||||
mIsLuaConsole = true;
|
mIsLuaConsole = true;
|
||||||
if (mStateId != mDefaultStateId) {
|
if (mStateId != mDefaultStateId) {
|
||||||
Application::Console().WriteRaw("Attached to Lua state '" + mStateId + "'. For help, type `:help`. To detach, type `:exit`");
|
Application::Console().WriteRaw("Attached to Lua state '" + mStateId + "'. For help, type `:help`. To detach, type `:exit`");
|
||||||
mCommandline->set_prompt("lua @" + LuaStateId + "> ");
|
mCommandline.set_prompt("lua @" + LuaStateId + "> ");
|
||||||
} else {
|
} else {
|
||||||
Application::Console().WriteRaw("Attached to Lua. For help, type `:help`. To detach, type `:exit`");
|
Application::Console().WriteRaw("Attached to Lua. For help, type `:help`. To detach, type `:exit`");
|
||||||
mCommandline->set_prompt("lua> ");
|
mCommandline.set_prompt("lua> ");
|
||||||
}
|
}
|
||||||
mCachedRegularHistory = mCommandline->history();
|
mCachedRegularHistory = mCommandline.history();
|
||||||
mCommandline->set_history(mCachedLuaHistory);
|
mCommandline.set_history(mCachedLuaHistory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,9 +175,9 @@ void TConsole::ChangeToRegularConsole() {
|
|||||||
} else {
|
} else {
|
||||||
Application::Console().WriteRaw("Detached from Lua.");
|
Application::Console().WriteRaw("Detached from Lua.");
|
||||||
}
|
}
|
||||||
mCachedLuaHistory = mCommandline->history();
|
mCachedLuaHistory = mCommandline.history();
|
||||||
mCommandline->set_history(mCachedRegularHistory);
|
mCommandline.set_history(mCachedRegularHistory);
|
||||||
mCommandline->set_prompt("> ");
|
mCommandline.set_prompt("> ");
|
||||||
mStateId = mDefaultStateId;
|
mStateId = mDefaultStateId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,7 +257,7 @@ void TConsole::Command_Clear(const std::string&, const std::vector<std::string>&
|
|||||||
if (!EnsureArgsCount(args, 0, size_t(-1))) {
|
if (!EnsureArgsCount(args, 0, size_t(-1))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mCommandline->write("\x1b[;H\x1b[2J");
|
mCommandline.write("\x1b[;H\x1b[2J");
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::Command_Kick(const std::string&, const std::vector<std::string>& args) {
|
void TConsole::Command_Kick(const std::string&, const std::vector<std::string>& args) {
|
||||||
@@ -589,20 +589,16 @@ Commands
|
|||||||
}
|
}
|
||||||
|
|
||||||
TConsole::TConsole() {
|
TConsole::TConsole() {
|
||||||
}
|
mCommandline.enable_history();
|
||||||
|
mCommandline.set_history_limit(20);
|
||||||
void TConsole::InitializeCommandline() {
|
mCommandline.set_prompt("> ");
|
||||||
mCommandline = std::make_unique<Commandline>();
|
|
||||||
mCommandline->enable_history();
|
|
||||||
mCommandline->set_history_limit(20);
|
|
||||||
mCommandline->set_prompt("> ");
|
|
||||||
BackupOldLog();
|
BackupOldLog();
|
||||||
mCommandline->on_command = [this](Commandline& c) {
|
mCommandline.on_command = [this](Commandline& c) {
|
||||||
try {
|
try {
|
||||||
auto TrimmedCmd = c.get_command();
|
auto TrimmedCmd = c.get_command();
|
||||||
TrimmedCmd = TrimString(TrimmedCmd);
|
TrimmedCmd = TrimString(TrimmedCmd);
|
||||||
auto [cmd, args] = ParseCommand(TrimmedCmd);
|
auto [cmd, args] = ParseCommand(TrimmedCmd);
|
||||||
mCommandline->write(mCommandline->prompt() + TrimmedCmd);
|
mCommandline.write(mCommandline.prompt() + TrimmedCmd);
|
||||||
if (mIsLuaConsole) {
|
if (mIsLuaConsole) {
|
||||||
if (!mLuaEngine) {
|
if (!mLuaEngine) {
|
||||||
beammp_info("Lua not started yet, please try again in a second");
|
beammp_info("Lua not started yet, please try again in a second");
|
||||||
@@ -637,7 +633,7 @@ void TConsole::InitializeCommandline() {
|
|||||||
beammp_error("Console died with: " + std::string(e.what()) + ". This could be a fatal error and could cause the server to terminate.");
|
beammp_error("Console died with: " + std::string(e.what()) + ". This could be a fatal error and could cause the server to terminate.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
mCommandline->on_autocomplete = [this](Commandline&, std::string stub, int) {
|
mCommandline.on_autocomplete = [this](Commandline&, std::string stub, int) {
|
||||||
std::vector<std::string> suggestions;
|
std::vector<std::string> suggestions;
|
||||||
try {
|
try {
|
||||||
if (mIsLuaConsole) { // if lua
|
if (mIsLuaConsole) { // if lua
|
||||||
@@ -707,21 +703,11 @@ void TConsole::InitializeCommandline() {
|
|||||||
|
|
||||||
void TConsole::Write(const std::string& str) {
|
void TConsole::Write(const std::string& str) {
|
||||||
auto ToWrite = GetDate() + str;
|
auto ToWrite = GetDate() + str;
|
||||||
// allows writing to stdout without an initialized console
|
mCommandline.write(ToWrite);
|
||||||
if (mCommandline) {
|
|
||||||
mCommandline->write(ToWrite);
|
|
||||||
} else {
|
|
||||||
std::cout << ToWrite << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::WriteRaw(const std::string& str) {
|
void TConsole::WriteRaw(const std::string& str) {
|
||||||
// allows writing to stdout without an initialized console
|
mCommandline.write(str);
|
||||||
if (mCommandline) {
|
|
||||||
mCommandline->write(str);
|
|
||||||
} else {
|
|
||||||
std::cout << str << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::InitializeLuaConsole(TLuaEngine& Engine) {
|
void TConsole::InitializeLuaConsole(TLuaEngine& Engine) {
|
||||||
|
|||||||
+15
-14
@@ -69,6 +69,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
int BeamMPServerMain(MainArguments Arguments) {
|
int BeamMPServerMain(MainArguments Arguments) {
|
||||||
setlocale(LC_ALL, "C");
|
setlocale(LC_ALL, "C");
|
||||||
|
Application::InitializeConsole();
|
||||||
ArgsParser Parser;
|
ArgsParser Parser;
|
||||||
Parser.RegisterArgument({ "help" }, ArgsParser::NONE);
|
Parser.RegisterArgument({ "help" }, ArgsParser::NONE);
|
||||||
Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
|
Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
|
||||||
@@ -79,10 +80,12 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (Parser.FoundArgument({ "help" })) {
|
if (Parser.FoundArgument({ "help" })) {
|
||||||
|
Application::Console().Internal().set_prompt("");
|
||||||
Application::Console().WriteRaw(sCommandlineArguments);
|
Application::Console().WriteRaw(sCommandlineArguments);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (Parser.FoundArgument({ "version" })) {
|
if (Parser.FoundArgument({ "version" })) {
|
||||||
|
Application::Console().Internal().set_prompt("");
|
||||||
Application::Console().WriteRaw("BeamMP-Server v" + Application::ServerVersionString());
|
Application::Console().WriteRaw("BeamMP-Server v" + Application::ServerVersionString());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -107,21 +110,10 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TConfig Config(ConfigPath);
|
|
||||||
|
|
||||||
if (Config.Failed()) {
|
|
||||||
beammp_info("Closing in 10 seconds");
|
|
||||||
// loop to make it possible to ctrl+c instead
|
|
||||||
for (size_t i = 0; i < 20; ++i) {
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
Application::InitializeConsole();
|
|
||||||
Application::Console().StartLoggingToFile();
|
|
||||||
|
|
||||||
Application::SetSubsystemStatus("Main", Application::Status::Starting);
|
Application::SetSubsystemStatus("Main", Application::Status::Starting);
|
||||||
|
|
||||||
|
Application::Console().StartLoggingToFile();
|
||||||
|
|
||||||
SetupSignalHandlers();
|
SetupSignalHandlers();
|
||||||
|
|
||||||
bool Shutdown = false;
|
bool Shutdown = false;
|
||||||
@@ -136,11 +128,20 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
TServer Server(Arguments.List);
|
TServer Server(Arguments.List);
|
||||||
|
TConfig Config(ConfigPath);
|
||||||
auto LuaEngine = std::make_shared<TLuaEngine>();
|
auto LuaEngine = std::make_shared<TLuaEngine>();
|
||||||
LuaEngine->SetServer(&Server);
|
LuaEngine->SetServer(&Server);
|
||||||
Application::Console().InitializeLuaConsole(*LuaEngine);
|
Application::Console().InitializeLuaConsole(*LuaEngine);
|
||||||
|
|
||||||
|
if (Config.Failed()) {
|
||||||
|
beammp_info("Closing in 10 seconds");
|
||||||
|
// loop to make it possible to ctrl+c instead
|
||||||
|
for (size_t i = 0; i < 20; ++i) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
RegisterThread("Main");
|
RegisterThread("Main");
|
||||||
|
|
||||||
beammp_trace("Running in debug mode on a debug build");
|
beammp_trace("Running in debug mode on a debug build");
|
||||||
|
|||||||
Reference in new issue
Block a user