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

View File

@@ -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;

View File

@@ -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 },

View File

@@ -7,6 +7,7 @@
#include <chrono>
#include <condition_variable>
#include <httplib.h>
#include <random>
#include <thread>
#include <tuple>
@@ -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<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)
: 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()) {

View File

@@ -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);
//}
}