From 29b4fa9b810d86be6e42f3ca55d4b5ceadbe01e9 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 10 Mar 2024 12:21:53 +0100 Subject: [PATCH] dos2unix, cache http connections for reuse --- src/Config.cpp | 82 +++++++++--------- src/Http.cpp | 219 ++++++++++++++++++++++++++----------------------- 2 files changed, 157 insertions(+), 144 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index d742701..1c31933 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,41 +1,41 @@ -#include "Config.h" - -#include -#include -#include -#include - -Config::Config() { - if (std::filesystem::exists("Launcher.cfg")) { - boost::iostreams::mapped_file cfg("Launcher.cfg", boost::iostreams::mapped_file::mapmode::readonly); - nlohmann::json d = nlohmann::json::parse(cfg.const_data(), nullptr, false); - if (d.is_discarded()) { - is_valid = false; - } - // parse config - if (d["Port"].is_number()) { - port = d["Port"].get(); - } - if (d["Build"].is_string()) { - branch = d["Build"].get(); - for (char& c : branch) { - c = char(tolower(c)); - } - } - if (d["GameDir"].is_string()) { - game_dir = d["GameDir"].get(); - } - } else { - write_to_file(); - } -} - -void Config::write_to_file() const { - nlohmann::json d { - { "Port", port }, - { "Branch", branch }, - { "GameDir", game_dir }, - }; - std::ofstream of("Launcher.cfg", std::ios::trunc); - of << d.dump(4); -} +#include "Config.h" + +#include +#include +#include +#include + +Config::Config() { + if (std::filesystem::exists("Launcher.cfg")) { + boost::iostreams::mapped_file cfg("Launcher.cfg", boost::iostreams::mapped_file::mapmode::readonly); + nlohmann::json d = nlohmann::json::parse(cfg.const_data(), nullptr, false); + if (d.is_discarded()) { + is_valid = false; + } + // parse config + if (d["Port"].is_number()) { + port = d["Port"].get(); + } + if (d["Build"].is_string()) { + branch = d["Build"].get(); + for (char& c : branch) { + c = char(tolower(c)); + } + } + if (d["GameDir"].is_string()) { + game_dir = d["GameDir"].get(); + } + } else { + write_to_file(); + } +} + +void Config::write_to_file() const { + nlohmann::json d { + { "Port", port }, + { "Branch", branch }, + { "GameDir", game_dir }, + }; + std::ofstream of("Launcher.cfg", std::ios::trunc); + of << d.dump(4); +} diff --git a/src/Http.cpp b/src/Http.cpp index e504c66..eb77e14 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -1,103 +1,116 @@ -#include -#include -#include -#include "Http.h" -#include -#include -#include - -bool HTTP::isDownload = false; -std::string HTTP::Get(const std::string &IP) { - static std::mutex Lock; - std::scoped_lock Guard(Lock); - - auto pos = IP.find('/',10); - - - httplib::Client cli(IP.substr(0, pos).c_str()); - cli.set_connection_timeout(std::chrono::seconds(10)); - cli.set_follow_location(true); - - httplib::Headers headers { - { "Accept-Encoding", "gzip" } - }; - - auto res = cli.Get(IP.substr(pos).c_str(), headers); - std::string Ret; - - if(res){ - if(res->status == 200){ - Ret = res->body; - }else spdlog::error(res->reason); - - }else{ - if(isDownload) { - std::cout << "\n"; - } - spdlog::error("HTTP Get failed on " + to_string(res.error())); - } - - return Ret; -} - -std::string HTTP::Post(const std::string& IP, const std::string& Fields) { - static std::mutex Lock; - std::scoped_lock Guard(Lock); - - auto pos = IP.find('/',10); - - httplib::Client cli(IP.substr(0, pos).c_str()); - cli.set_connection_timeout(std::chrono::seconds(10)); - std::string Ret; - - if(!Fields.empty()) { - httplib::Result res = cli.Post(IP.substr(pos).c_str(), Fields, "application/json"); - - if(res) { - if (res->status != 200) { - spdlog::error(res->reason); - } - Ret = res->body; - }else{ - spdlog::error("HTTP Post failed on " + to_string(res.error())); - } - }else{ - httplib::Result res = cli.Post(IP.substr(pos).c_str()); - if(res) { - if (res->status != 200) { - spdlog::error(res->reason); - } - Ret = res->body; - }else{ - spdlog::error("HTTP Post failed on " + to_string(res.error())); - } - } - - if(Ret.empty())return "-1"; - else return Ret; -} - - -bool HTTP::Download(const std::string &IP, const std::string &Path) { - static std::mutex Lock; - std::scoped_lock Guard(Lock); - - isDownload = true; - std::string Ret = Get(IP); - isDownload = false; - - if(Ret.empty())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; -} +#include "Http.h" +#include +#include +#include +#include +#include +#include + +static httplib::Client& get_client(const std::string& host) { + static thread_local std::unordered_map s_clients_cache {}; + if (!s_clients_cache.contains(host)) { + spdlog::debug("Caching connection to {}", host); + s_clients_cache.emplace(host, httplib::Client(host)); + } else { + spdlog::debug("Reusing cached connection to {}", host); + } + return s_clients_cache.at(host); +} + +bool HTTP::isDownload = false; +std::string HTTP::Get(const std::string& host_and_target) { + static std::mutex Lock; + std::scoped_lock Guard(Lock); + + auto pos = host_and_target.find('/', 10); + + auto& cli = get_client(host_and_target.substr(0, pos).c_str()); + cli.set_connection_timeout(std::chrono::seconds(10)); + cli.set_follow_location(true); + + httplib::Headers headers { + { "Accept-Encoding", "gzip" } + }; + + auto res = cli.Get(host_and_target.substr(pos).c_str(), headers); + std::string Ret; + + if (res) { + if (res->status == 200) { + Ret = res->body; + } else + spdlog::error(res->reason); + + } else { + if (isDownload) { + std::cout << "\n"; + } + spdlog::error("HTTP Get failed on " + to_string(res.error())); + } + + return Ret; +} + +std::string HTTP::Post(const std::string& host_and_target, const std::string& Fields) { + static std::mutex Lock; + std::scoped_lock Guard(Lock); + + auto pos = host_and_target.find('/', 10); + + auto& cli = get_client(host_and_target.substr(0, pos).c_str()); + cli.set_connection_timeout(std::chrono::seconds(10)); + std::string Ret; + + if (!Fields.empty()) { + httplib::Result res = cli.Post(host_and_target.substr(pos).c_str(), Fields, "application/json"); + + if (res) { + if (res->status != 200) { + spdlog::error(res->reason); + } + Ret = res->body; + } else { + spdlog::error("HTTP Post failed on " + to_string(res.error())); + } + } else { + httplib::Result res = cli.Post(host_and_target.substr(pos).c_str()); + if (res) { + if (res->status != 200) { + spdlog::error(res->reason); + } + Ret = res->body; + } else { + spdlog::error("HTTP Post failed on " + to_string(res.error())); + } + } + + if (Ret.empty()) + return "-1"; + else + return Ret; +} + +bool HTTP::Download(const std::string& IP, const std::string& Path) { + static std::mutex Lock; + std::scoped_lock Guard(Lock); + + isDownload = true; + std::string Ret = Get(IP); + isDownload = false; + + if (Ret.empty()) + 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; +}