start writing http lua stuff, also heartbeat debug printing

This commit is contained in:
Lion Kortlepel
2021-11-08 22:08:07 +01:00
parent 3e7aa763ed
commit f8af134dc9
4 changed files with 43 additions and 10 deletions
+1
View File
@@ -137,6 +137,7 @@ private:
sol::table Lua_GetPlayers(); sol::table Lua_GetPlayers();
std::string Lua_GetPlayerName(int ID); std::string Lua_GetPlayerName(int ID);
sol::table Lua_GetPlayerVehicles(int ID); sol::table Lua_GetPlayerVehicles(int ID);
sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port);
std::string mName; std::string mName;
std::atomic_bool& mShutdown; std::atomic_bool& mShutdown;
+4 -2
View File
@@ -30,13 +30,15 @@ void THeartbeatThread::operator()() {
Last = Body; Last = Body;
LastNormalUpdateTime = Now; LastNormalUpdateTime = Now;
if (!Application::Settings.CustomIP.empty()) if (!Application::Settings.CustomIP.empty()) {
Body += "&ip=" + Application::Settings.CustomIP; Body += "&ip=" + Application::Settings.CustomIP;
}
Body += "&pps=" + Application::PPS(); 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(); auto Lock = Sentry.CreateExclusiveContext();
Sentry.SetContext("heartbeat", Sentry.SetContext("heartbeat",
{ { "response-body", T }, { { "response-body", T },
+32 -4
View File
@@ -7,6 +7,7 @@
#include <chrono> #include <chrono>
#include <condition_variable> #include <condition_variable>
#include <httplib.h>
#include <random> #include <random>
#include <thread> #include <thread>
#include <tuple> #include <tuple>
@@ -90,12 +91,12 @@ void TLuaEngine::operator()() {
} }
} }
} }
if (std::chrono::high_resolution_clock::duration Diff; std::chrono::high_resolution_clock::duration Diff;
(Diff = std::chrono::high_resolution_clock::now() - Before) if ((Diff = std::chrono::high_resolution_clock::now() - Before)
< std::chrono::milliseconds(10)) { < std::chrono::milliseconds(10)) {
std::this_thread::sleep_for(Diff); std::this_thread::sleep_for(Diff);
} else { } 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(); Before = std::chrono::high_resolution_clock::now();
} }
@@ -366,6 +367,28 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerVehicles(int ID) {
return sol::nil; 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<httplib::Client>(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<std::string>() && pair.second.is<std::string>()) {
GetHeaders.insert(std::pair(pair.first.as<std::string>(), pair.second.as<std::string>()));
} else {
beammp_lua_error("Http:Get: Expected string-string pairs for headers, got something else, ignoring that header");
}
}
auto client = table[InternalClient].get<std::shared_ptr<httplib::Client>>();
client->Get(path.c_str(), GetHeaders);
});
return table;
}
TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomic_bool& Shutdown, TLuaStateId StateId, TLuaEngine& Engine) TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomic_bool& Shutdown, TLuaStateId StateId, TLuaEngine& Engine)
: mName(Name) : mName(Name)
, mShutdown(Shutdown) , mShutdown(Shutdown)
@@ -447,7 +470,9 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
}); });
MPTable.set_function("Set", &LuaAPI::MP::Set); MPTable.set_function("Set", &LuaAPI::MP::Set);
auto HttpTable = StateView.create_named_table("Http"); 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", MPTable.create_named("Settings",
"Debug", 0, "Debug", 0,
@@ -658,6 +683,9 @@ TPluginMonitor::TPluginMonitor(const fs::path& Path, TLuaEngine& Engine, std::at
: mEngine(Engine) : mEngine(Engine)
, mPath(Path) , mPath(Path)
, mShutdown(Shutdown) { , mShutdown(Shutdown) {
if (!fs::exists(mPath)) {
fs::create_directories(mPath);
}
for (const auto& Entry : fs::recursive_directory_iterator(mPath)) { for (const auto& Entry : fs::recursive_directory_iterator(mPath)) {
// TODO: trigger an event when a subfolder file changes // TODO: trigger an event when a subfolder file changes
if (Entry.is_regular_file()) { if (Entry.is_regular_file()) {
+6 -4
View File
@@ -21,7 +21,8 @@
// global, yes, this is ugly, no, it cant be done another way // global, yes, this is ugly, no, it cant be done another way
TSentry Sentry {}; TSentry Sentry {};
int main(int argc, char** argv) try { int main(int argc, char** argv) //try {
{
setlocale(LC_ALL, "C"); setlocale(LC_ALL, "C");
SetupSignalHandlers(); SetupSignalHandlers();
@@ -67,7 +68,8 @@ int main(int argc, char** argv) try {
} }
beammp_info("Shutdown."); beammp_info("Shutdown.");
return 0; return 0;
} catch (const std::exception& e) { //} catch (const std::exception& e) {
beammp_error(e.what()); // beammp_error(e.what());
Sentry.LogException(e, _file_basename, _line); // Sentry.LogException(e, _file_basename, _line);
//}
} }