mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-07-01 23:46:59 +00:00
allow gzip compressed server list
This commit is contained in:
parent
120a51e2c7
commit
60b6d6f9ac
@ -61,7 +61,7 @@ set(PRJ_INCLUDE_DIRS src)
|
|||||||
# set compile features (e.g. standard version)
|
# set compile features (e.g. standard version)
|
||||||
set(PRJ_COMPILE_FEATURES cxx_std_20)
|
set(PRJ_COMPILE_FEATURES cxx_std_20)
|
||||||
# set #defines (test enable/disable not included here)
|
# 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!)
|
# add all libraries used by the project (WARNING: also set them in vcpkg.json!)
|
||||||
set(PRJ_LIBRARIES
|
set(PRJ_LIBRARIES
|
||||||
fmt::fmt
|
fmt::fmt
|
||||||
|
@ -276,7 +276,7 @@ void ClientNetwork::handle_browsing(bmp::ClientPacket& packet) {
|
|||||||
if (list.has_value()) {
|
if (list.has_value()) {
|
||||||
bmp::ClientPacket response {
|
bmp::ClientPacket response {
|
||||||
.purpose = bmp::ClientPurpose::ServerListResponse,
|
.purpose = bmp::ClientPurpose::ServerListResponse,
|
||||||
.raw_data = json_to_vec(list.value()),
|
.raw_data = list.value(),
|
||||||
};
|
};
|
||||||
client_tcp_write(response);
|
client_tcp_write(response);
|
||||||
} else {
|
} else {
|
||||||
@ -385,12 +385,12 @@ void ClientNetwork::client_tcp_read(std::function<void(bmp::ClientPacket&&)> han
|
|||||||
void ClientNetwork::client_tcp_write(bmp::ClientPacket& packet) {
|
void ClientNetwork::client_tcp_write(bmp::ClientPacket& packet) {
|
||||||
auto header = packet.finalize();
|
auto header = packet.finalize();
|
||||||
// serialize header
|
// serialize header
|
||||||
std::vector<uint8_t> data(bmp::ClientHeader::SERIALIZED_SIZE + packet.raw_data.size());
|
auto data = std::make_shared<std::vector<uint8_t>>(bmp::ClientHeader::SERIALIZED_SIZE + packet.raw_data.size());
|
||||||
auto offset = header.serialize_to(data);
|
auto offset = header.serialize_to(*data);
|
||||||
// copy packet data (yes i know ugh) to the `data` in order to send it in one go
|
// 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));
|
std::copy(packet.raw_data.begin(), packet.raw_data.end(), data->begin() + long(offset));
|
||||||
boost::asio::async_write(m_game_socket, buffer(data),
|
boost::asio::async_write(m_game_socket, buffer(*data),
|
||||||
[this, packet](auto ec, auto) {
|
[this, packet, data](auto ec, auto) {
|
||||||
if (ec) {
|
if (ec) {
|
||||||
spdlog::error("Failed to write a packet: {}", ec.message());
|
spdlog::error("Failed to write a packet: {}", ec.message());
|
||||||
disconnect("Failed to send data to game");
|
disconnect("Failed to send data to game");
|
||||||
@ -428,14 +428,13 @@ void ClientNetwork::start_browsing() {
|
|||||||
m_client_state = bmp::ClientState::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 {
|
try {
|
||||||
auto list = HTTP::Get("https://backend.beammp.com/servers-info");
|
auto list = HTTP::Get("https://backend.beammp.com/servers-info/");
|
||||||
if (list == "-1") {
|
if (list == "-1") {
|
||||||
return outcome::failure("Failed to fetch server list, see launcher log for more information.");
|
return outcome::failure("Failed to fetch server list, see launcher log for more information.");
|
||||||
}
|
}
|
||||||
json result = json::parse(list);
|
return outcome::success(std::vector<uint8_t>(list.begin(), list.end()));
|
||||||
return outcome::success(result);
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
return outcome::failure(fmt::format("Failed to fetch server list from backend: {}", e.what()));
|
return outcome::failure(fmt::format("Failed to fetch server list from backend: {}", e.what()));
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ private:
|
|||||||
void start_quick_join();
|
void start_quick_join();
|
||||||
void start_browsing();
|
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 std::vector<uint8_t> json_to_vec(const nlohmann::json& json);
|
||||||
static nlohmann::json vec_to_json(const std::vector<uint8_t>& vec);
|
static nlohmann::json vec_to_json(const std::vector<uint8_t>& vec);
|
||||||
|
@ -13,10 +13,16 @@ std::string HTTP::Get(const std::string &IP) {
|
|||||||
|
|
||||||
auto pos = IP.find('/',10);
|
auto pos = IP.find('/',10);
|
||||||
|
|
||||||
|
|
||||||
httplib::Client cli(IP.substr(0, pos).c_str());
|
httplib::Client cli(IP.substr(0, pos).c_str());
|
||||||
cli.set_connection_timeout(std::chrono::seconds(10));
|
cli.set_connection_timeout(std::chrono::seconds(10));
|
||||||
cli.set_follow_location(true);
|
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;
|
std::string Ret;
|
||||||
|
|
||||||
if(res){
|
if(res){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user