dos2unix, cache http connections for reuse

This commit is contained in:
Lion Kortlepel 2024-03-10 12:21:53 +01:00
parent 60b6d6f9ac
commit 29b4fa9b81
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 157 additions and 144 deletions

View File

@ -1,41 +1,41 @@
#include "Config.h" #include "Config.h"
#include <boost/iostreams/device/mapped_file.hpp> #include <boost/iostreams/device/mapped_file.hpp>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
Config::Config() { Config::Config() {
if (std::filesystem::exists("Launcher.cfg")) { if (std::filesystem::exists("Launcher.cfg")) {
boost::iostreams::mapped_file cfg("Launcher.cfg", boost::iostreams::mapped_file::mapmode::readonly); boost::iostreams::mapped_file cfg("Launcher.cfg", boost::iostreams::mapped_file::mapmode::readonly);
nlohmann::json d = nlohmann::json::parse(cfg.const_data(), nullptr, false); nlohmann::json d = nlohmann::json::parse(cfg.const_data(), nullptr, false);
if (d.is_discarded()) { if (d.is_discarded()) {
is_valid = false; is_valid = false;
} }
// parse config // parse config
if (d["Port"].is_number()) { if (d["Port"].is_number()) {
port = d["Port"].get<int>(); port = d["Port"].get<int>();
} }
if (d["Build"].is_string()) { if (d["Build"].is_string()) {
branch = d["Build"].get<std::string>(); branch = d["Build"].get<std::string>();
for (char& c : branch) { for (char& c : branch) {
c = char(tolower(c)); c = char(tolower(c));
} }
} }
if (d["GameDir"].is_string()) { if (d["GameDir"].is_string()) {
game_dir = d["GameDir"].get<std::string>(); game_dir = d["GameDir"].get<std::string>();
} }
} else { } else {
write_to_file(); write_to_file();
} }
} }
void Config::write_to_file() const { void Config::write_to_file() const {
nlohmann::json d { nlohmann::json d {
{ "Port", port }, { "Port", port },
{ "Branch", branch }, { "Branch", branch },
{ "GameDir", game_dir }, { "GameDir", game_dir },
}; };
std::ofstream of("Launcher.cfg", std::ios::trunc); std::ofstream of("Launcher.cfg", std::ios::trunc);
of << d.dump(4); of << d.dump(4);
} }

View File

@ -1,103 +1,116 @@
#include <iostream> #include "Http.h"
#include <fstream> #include <cmath>
#include <spdlog/spdlog.h> #include <fstream>
#include "Http.h" #include <httplib.h>
#include <mutex> #include <iostream>
#include <cmath> #include <mutex>
#include <httplib.h> #include <spdlog/spdlog.h>
bool HTTP::isDownload = false; static httplib::Client& get_client(const std::string& host) {
std::string HTTP::Get(const std::string &IP) { static thread_local std::unordered_map<std::string /* host */, httplib::Client> s_clients_cache {};
static std::mutex Lock; if (!s_clients_cache.contains(host)) {
std::scoped_lock Guard(Lock); spdlog::debug("Caching connection to {}", host);
s_clients_cache.emplace(host, httplib::Client(host));
auto pos = IP.find('/',10); } else {
spdlog::debug("Reusing cached connection to {}", host);
}
httplib::Client cli(IP.substr(0, pos).c_str()); return s_clients_cache.at(host);
cli.set_connection_timeout(std::chrono::seconds(10)); }
cli.set_follow_location(true);
bool HTTP::isDownload = false;
httplib::Headers headers { std::string HTTP::Get(const std::string& host_and_target) {
{ "Accept-Encoding", "gzip" } static std::mutex Lock;
}; std::scoped_lock Guard(Lock);
auto res = cli.Get(IP.substr(pos).c_str(), headers); auto pos = host_and_target.find('/', 10);
std::string Ret;
auto& cli = get_client(host_and_target.substr(0, pos).c_str());
if(res){ cli.set_connection_timeout(std::chrono::seconds(10));
if(res->status == 200){ cli.set_follow_location(true);
Ret = res->body;
}else spdlog::error(res->reason); httplib::Headers headers {
{ "Accept-Encoding", "gzip" }
}else{ };
if(isDownload) {
std::cout << "\n"; auto res = cli.Get(host_and_target.substr(pos).c_str(), headers);
} std::string Ret;
spdlog::error("HTTP Get failed on " + to_string(res.error()));
} if (res) {
if (res->status == 200) {
return Ret; Ret = res->body;
} } else
spdlog::error(res->reason);
std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
static std::mutex Lock; } else {
std::scoped_lock Guard(Lock); if (isDownload) {
std::cout << "\n";
auto pos = IP.find('/',10); }
spdlog::error("HTTP Get failed on " + to_string(res.error()));
httplib::Client cli(IP.substr(0, pos).c_str()); }
cli.set_connection_timeout(std::chrono::seconds(10));
std::string Ret; return Ret;
}
if(!Fields.empty()) {
httplib::Result res = cli.Post(IP.substr(pos).c_str(), Fields, "application/json"); std::string HTTP::Post(const std::string& host_and_target, const std::string& Fields) {
static std::mutex Lock;
if(res) { std::scoped_lock Guard(Lock);
if (res->status != 200) {
spdlog::error(res->reason); auto pos = host_and_target.find('/', 10);
}
Ret = res->body; auto& cli = get_client(host_and_target.substr(0, pos).c_str());
}else{ cli.set_connection_timeout(std::chrono::seconds(10));
spdlog::error("HTTP Post failed on " + to_string(res.error())); std::string Ret;
}
}else{ if (!Fields.empty()) {
httplib::Result res = cli.Post(IP.substr(pos).c_str()); httplib::Result res = cli.Post(host_and_target.substr(pos).c_str(), Fields, "application/json");
if(res) {
if (res->status != 200) { if (res) {
spdlog::error(res->reason); if (res->status != 200) {
} spdlog::error(res->reason);
Ret = res->body; }
}else{ Ret = res->body;
spdlog::error("HTTP Post failed on " + to_string(res.error())); } else {
} spdlog::error("HTTP Post failed on " + to_string(res.error()));
} }
} else {
if(Ret.empty())return "-1"; httplib::Result res = cli.Post(host_and_target.substr(pos).c_str());
else return Ret; if (res) {
} if (res->status != 200) {
spdlog::error(res->reason);
}
bool HTTP::Download(const std::string &IP, const std::string &Path) { Ret = res->body;
static std::mutex Lock; } else {
std::scoped_lock Guard(Lock); spdlog::error("HTTP Post failed on " + to_string(res.error()));
}
isDownload = true; }
std::string Ret = Get(IP);
isDownload = false; if (Ret.empty())
return "-1";
if(Ret.empty())return false; else
return Ret;
std::ofstream File(Path, std::ios::binary); }
if(File.is_open()) {
File << Ret; bool HTTP::Download(const std::string& IP, const std::string& Path) {
File.close(); static std::mutex Lock;
std::cout << "\n"; std::scoped_lock Guard(Lock);
spdlog::info("Download Complete!");
}else{ isDownload = true;
spdlog::error("Failed to open file directory: " + Path); std::string Ret = Get(IP);
return false; isDownload = false;
}
if (Ret.empty())
return true; return false;
}
std::ofstream File(Path, std::ios::binary);
if (File.is_open()) {
File << Ret;
File.close();
std::cout << "\n";
spdlog::info("Download Complete!");
} else {
spdlog::error("Failed to open file directory: " + Path);
return false;
}
return true;
}