allow gzip compressed server list

This commit is contained in:
Lion Kortlepel 2024-03-10 12:14:47 +01:00
parent 120a51e2c7
commit 60b6d6f9ac
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
4 changed files with 18 additions and 13 deletions

View File

@ -61,7 +61,7 @@ set(PRJ_INCLUDE_DIRS src)
# set compile features (e.g. standard version)
set(PRJ_COMPILE_FEATURES cxx_std_20)
# set #defines (test enable/disable not included here)
set(PRJ_DEFINITIONS CPPHTTPLIB_OPENSSL_SUPPORT)
set(PRJ_DEFINITIONS CPPHTTPLIB_OPENSSL_SUPPORT CPPHTTPLIB_ZLIB_SUPPORT)
# add all libraries used by the project (WARNING: also set them in vcpkg.json!)
set(PRJ_LIBRARIES
fmt::fmt

View File

@ -276,7 +276,7 @@ void ClientNetwork::handle_browsing(bmp::ClientPacket& packet) {
if (list.has_value()) {
bmp::ClientPacket response {
.purpose = bmp::ClientPurpose::ServerListResponse,
.raw_data = json_to_vec(list.value()),
.raw_data = list.value(),
};
client_tcp_write(response);
} else {
@ -385,12 +385,12 @@ void ClientNetwork::client_tcp_read(std::function<void(bmp::ClientPacket&&)> han
void ClientNetwork::client_tcp_write(bmp::ClientPacket& packet) {
auto header = packet.finalize();
// serialize header
std::vector<uint8_t> data(bmp::ClientHeader::SERIALIZED_SIZE + packet.raw_data.size());
auto offset = header.serialize_to(data);
auto data = std::make_shared<std::vector<uint8_t>>(bmp::ClientHeader::SERIALIZED_SIZE + packet.raw_data.size());
auto offset = header.serialize_to(*data);
// copy packet data (yes i know ugh) to the `data` in order to send it in one go
std::copy(packet.raw_data.begin(), packet.raw_data.end(), data.begin() + long(offset));
boost::asio::async_write(m_game_socket, buffer(data),
[this, packet](auto ec, auto) {
std::copy(packet.raw_data.begin(), packet.raw_data.end(), data->begin() + long(offset));
boost::asio::async_write(m_game_socket, buffer(*data),
[this, packet, data](auto ec, auto) {
if (ec) {
spdlog::error("Failed to write a packet: {}", ec.message());
disconnect("Failed to send data to game");
@ -428,14 +428,13 @@ void ClientNetwork::start_browsing() {
m_client_state = bmp::ClientState::Browsing;
}
Result<nlohmann::json, std::string> ClientNetwork::load_server_list() noexcept {
Result<std::vector<uint8_t>, std::string> ClientNetwork::load_server_list() noexcept {
try {
auto list = HTTP::Get("https://backend.beammp.com/servers-info");
auto list = HTTP::Get("https://backend.beammp.com/servers-info/");
if (list == "-1") {
return outcome::failure("Failed to fetch server list, see launcher log for more information.");
}
json result = json::parse(list);
return outcome::success(result);
return outcome::success(std::vector<uint8_t>(list.begin(), list.end()));
} catch (const std::exception& e) {
return outcome::failure(fmt::format("Failed to fetch server list from backend: {}", e.what()));
}

View File

@ -47,7 +47,7 @@ private:
void start_quick_join();
void start_browsing();
Result<nlohmann::json, std::string> load_server_list() noexcept;
Result<std::vector<uint8_t>, std::string> load_server_list() noexcept;
static std::vector<uint8_t> json_to_vec(const nlohmann::json& json);
static nlohmann::json vec_to_json(const std::vector<uint8_t>& vec);

View File

@ -13,10 +13,16 @@ std::string HTTP::Get(const std::string &IP) {
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);
auto res = cli.Get(IP.substr(pos).c_str());
httplib::Headers headers {
{ "Accept-Encoding", "gzip" }
};
auto res = cli.Get(IP.substr(pos).c_str(), headers);
std::string Ret;
if(res){