From f8af134dc95178368ba944c382eb9950c10e5422 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 8 Nov 2021 22:08:07 +0100 Subject: [PATCH] start writing http lua stuff, also heartbeat debug printing --- include/TLuaEngine.h | 1 + src/THeartbeatThread.cpp | 6 ++++-- src/TLuaEngine.cpp | 36 ++++++++++++++++++++++++++++++++---- src/main.cpp | 10 ++++++---- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 1c9de93..50d5235 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -137,6 +137,7 @@ private: sol::table Lua_GetPlayers(); std::string Lua_GetPlayerName(int ID); sol::table Lua_GetPlayerVehicles(int ID); + sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port); std::string mName; std::atomic_bool& mShutdown; diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 92e8585..574eda3 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -30,13 +30,15 @@ void THeartbeatThread::operator()() { Last = Body; LastNormalUpdateTime = Now; - if (!Application::Settings.CustomIP.empty()) + if (!Application::Settings.CustomIP.empty()) { Body += "&ip=" + Application::Settings.CustomIP; + } Body += "&pps=" + Application::PPS(); - auto SentryReportError = [&](const std::string& transaction, int status) { + beammp_trace("heartbeat body: '" + Body + "'"); + auto SentryReportError = [&](const std::string& transaction, int status) { auto Lock = Sentry.CreateExclusiveContext(); Sentry.SetContext("heartbeat", { { "response-body", T }, diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index c9bdd71..fc330bb 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -90,12 +91,12 @@ void TLuaEngine::operator()() { } } } - if (std::chrono::high_resolution_clock::duration Diff; - (Diff = std::chrono::high_resolution_clock::now() - Before) + std::chrono::high_resolution_clock::duration Diff; + if ((Diff = std::chrono::high_resolution_clock::now() - Before) < std::chrono::milliseconds(10)) { std::this_thread::sleep_for(Diff); } else { - beammp_trace("Event loop cannot keep up!"); + beammp_trace("Event loop cannot keep up! Running " + std::to_string(Diff.count()) + "s behind"); } Before = std::chrono::high_resolution_clock::now(); } @@ -366,6 +367,28 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerVehicles(int ID) { return sol::nil; } +sol::table TLuaEngine::StateThreadData::Lua_HttpCreateConnection(const std::string& host, uint16_t port) { + auto table = mStateView.create_table(); + constexpr const char* InternalClient = "__InternalClient"; + table["host"] = host; + table["port"] = port; + auto client = std::make_shared(host, port); + table[InternalClient] = client; + table.set_function("Get", [&InternalClient](const sol::table& table, const std::string& path, const sol::table& headers) { + httplib::Headers GetHeaders; + for (const auto& pair : headers) { + if (pair.first.is() && pair.second.is()) { + GetHeaders.insert(std::pair(pair.first.as(), pair.second.as())); + } else { + beammp_lua_error("Http:Get: Expected string-string pairs for headers, got something else, ignoring that header"); + } + } + auto client = table[InternalClient].get>(); + client->Get(path.c_str(), GetHeaders); + }); + return table; +} + TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomic_bool& Shutdown, TLuaStateId StateId, TLuaEngine& Engine) : mName(Name) , mShutdown(Shutdown) @@ -447,7 +470,9 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi }); MPTable.set_function("Set", &LuaAPI::MP::Set); auto HttpTable = StateView.create_named_table("Http"); - //HttpTable.set_function("CreateConnection", &LuaAPI::Http::CreateConnection); + HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) { + return Lua_HttpCreateConnection(host, port); + }); MPTable.create_named("Settings", "Debug", 0, @@ -658,6 +683,9 @@ TPluginMonitor::TPluginMonitor(const fs::path& Path, TLuaEngine& Engine, std::at : mEngine(Engine) , mPath(Path) , mShutdown(Shutdown) { + if (!fs::exists(mPath)) { + fs::create_directories(mPath); + } for (const auto& Entry : fs::recursive_directory_iterator(mPath)) { // TODO: trigger an event when a subfolder file changes if (Entry.is_regular_file()) { diff --git a/src/main.cpp b/src/main.cpp index a2b8aa1..7af0ae9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,7 +21,8 @@ // global, yes, this is ugly, no, it cant be done another way TSentry Sentry {}; -int main(int argc, char** argv) try { +int main(int argc, char** argv) //try { +{ setlocale(LC_ALL, "C"); SetupSignalHandlers(); @@ -67,7 +68,8 @@ int main(int argc, char** argv) try { } beammp_info("Shutdown."); return 0; -} catch (const std::exception& e) { - beammp_error(e.what()); - Sentry.LogException(e, _file_basename, _line); +//} catch (const std::exception& e) { +// beammp_error(e.what()); +// Sentry.LogException(e, _file_basename, _line); +//} }