mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-07-01 23:46:59 +00:00
dos2unix, cache http connections for reuse
This commit is contained in:
parent
60b6d6f9ac
commit
29b4fa9b81
@ -1,41 +1,41 @@
|
||||
#include "Config.h"
|
||||
|
||||
#include <boost/iostreams/device/mapped_file.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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<int>();
|
||||
}
|
||||
if (d["Build"].is_string()) {
|
||||
branch = d["Build"].get<std::string>();
|
||||
for (char& c : branch) {
|
||||
c = char(tolower(c));
|
||||
}
|
||||
}
|
||||
if (d["GameDir"].is_string()) {
|
||||
game_dir = d["GameDir"].get<std::string>();
|
||||
}
|
||||
} 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 <boost/iostreams/device/mapped_file.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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<int>();
|
||||
}
|
||||
if (d["Build"].is_string()) {
|
||||
branch = d["Build"].get<std::string>();
|
||||
for (char& c : branch) {
|
||||
c = char(tolower(c));
|
||||
}
|
||||
}
|
||||
if (d["GameDir"].is_string()) {
|
||||
game_dir = d["GameDir"].get<std::string>();
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
|
219
src/Http.cpp
219
src/Http.cpp
@ -1,103 +1,116 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "Http.h"
|
||||
#include <mutex>
|
||||
#include <cmath>
|
||||
#include <httplib.h>
|
||||
|
||||
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 <cmath>
|
||||
#include <fstream>
|
||||
#include <httplib.h>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
static httplib::Client& get_client(const std::string& host) {
|
||||
static thread_local std::unordered_map<std::string /* host */, httplib::Client> 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user