Add libcurl
This commit is contained in:
@@ -182,7 +182,7 @@ jobs:
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev libxcb-glx0-dev gettext libfontconfig-dev libboost-all-dev librsvg2-bin libssl-dev sway grim mesa-vulkan-drivers wayland-protocols fonts-noto-cjk fonts-roboto
|
||||
sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev libxcb-glx0-dev gettext libfontconfig-dev libboost-all-dev librsvg2-bin libssl-dev sway grim mesa-vulkan-drivers wayland-protocols fonts-noto-cjk fonts-roboto libcurl4-openssl-dev
|
||||
|
||||
wget https://github.com/KhronosGroup/KTX-Software/releases/download/v4.4.0/KTX-Software-4.4.0-Linux-x86_64.deb
|
||||
sudo dpkg --install KTX-Software-4.4.0-Linux-x86_64.deb
|
||||
|
||||
+11
-1
@@ -10,6 +10,10 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
|
||||
|
||||
if(ANDROID)
|
||||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/android)
|
||||
endif()
|
||||
|
||||
# Default to PIC code
|
||||
include(CheckPIESupported)
|
||||
check_pie_supported()
|
||||
@@ -82,7 +86,7 @@ endif()
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(Vulkan REQUIRED)
|
||||
find_package(Git)
|
||||
if (NEED_COMMON AND NOT ANDROID)
|
||||
if (NEED_COMMON)
|
||||
find_package(OpenSSL REQUIRED)
|
||||
endif()
|
||||
include(VulkanVersion)
|
||||
@@ -180,6 +184,10 @@ if (WIVRN_BUILD_CLIENT)
|
||||
if (WIVRN_COMPRESS_GLB)
|
||||
find_program(GLTF_TRANSFORM gltf-transform REQUIRED)
|
||||
endif()
|
||||
|
||||
if(NOT ANDROID)
|
||||
find_package(CURL REQUIRED COMPONENTS HTTP HTTPS)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (WIVRN_BUILD_CLIENT AND NOT ANDROID)
|
||||
@@ -243,6 +251,8 @@ FetchContent_Declare(uni-algo EXCLUDE_FROM_ALL SYSTEM URL https://github.co
|
||||
URL_HASH SHA256=f2a1539cd8635bc6088d05144a73ecfe7b4d74ee0361fabed6f87f9f19e74ca9)
|
||||
FetchContent_Declare(entt EXCLUDE_FROM_ALL SYSTEM URL https://github.com/skypjack/entt/archive/refs/tags/v3.15.0.tar.gz
|
||||
URL_HASH SHA256=01466fcbf77618a79b62891510c0bbf25ac2804af5751c84982b413852234d66)
|
||||
FetchContent_Declare(curl EXCLUDE_FROM_ALL SYSTEM URL https://github.com/curl/curl/releases/download/curl-8_15_0/curl-8.15.0.tar.xz
|
||||
URL_HASH SHA256=6cd0a8a5b126ddfda61c94dc2c3fc53481ba7a35461cf7c5ab66aa9d6775b609)
|
||||
|
||||
file(GLOB MONADO_PATCHES CONFIGURE_DEPENDS patches/monado/*)
|
||||
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS monado-rev)
|
||||
|
||||
+11
-6
@@ -9,10 +9,6 @@ set(FASTGLTF_COMPILE_AS_CPP20 ON)
|
||||
|
||||
FetchContent_MakeAvailable(simdjson spdlog glm fastgltf imgui stb implot uni-algo entt libktx)
|
||||
|
||||
if (NOT WIVRN_USE_SYSTEM_BOOST)
|
||||
FetchContent_MakeAvailable(boost)
|
||||
endif()
|
||||
|
||||
include(FreetypeHarfbuzz)
|
||||
include(CompileGLSL)
|
||||
include(GenerateAssets)
|
||||
@@ -34,6 +30,16 @@ if(ANDROID)
|
||||
FetchContent_MakeAvailable(openxr_loader)
|
||||
target_link_libraries(wivrn openxr_loader)
|
||||
|
||||
# libcurl
|
||||
set(BUILD_CURL_EXE OFF)
|
||||
set(BUILD_TESTING OFF)
|
||||
set(CURL_DISABLE_INSTALL ON)
|
||||
set(CURL_USE_LIBPSL OFF)
|
||||
set(CURL_USE_LIBSSH2 OFF)
|
||||
set(BUILD_EXAMPLES OFF)
|
||||
|
||||
FetchContent_MakeAvailable(curl)
|
||||
target_link_libraries(wivrn libcurl_static)
|
||||
|
||||
target_link_libraries(wivrn native_app_glue)
|
||||
target_compile_definitions(wivrn PRIVATE -DXR_USE_PLATFORM_ANDROID -DVK_USE_PLATFORM_ANDROID_KHR)
|
||||
@@ -55,8 +61,7 @@ else()
|
||||
target_link_libraries(wivrn openxr_loader)
|
||||
endif()
|
||||
|
||||
target_link_libraries(wivrn PkgConfig::LIBAV Fontconfig::Fontconfig)
|
||||
|
||||
target_link_libraries(wivrn PkgConfig::LIBAV Fontconfig::Fontconfig CURL::libcurl)
|
||||
|
||||
file(GLOB PLATFORM_SOURCE CONFIGURE_DEPENDS
|
||||
"decoder/ffmpeg/*.cpp")
|
||||
|
||||
+3
-1
@@ -39,7 +39,8 @@ asset::asset(const std::filesystem::path & path)
|
||||
}
|
||||
|
||||
asset::asset(asset && other) :
|
||||
android_asset(other.android_asset)
|
||||
android_asset(other.android_asset),
|
||||
bytes(other.bytes)
|
||||
{
|
||||
other.android_asset = nullptr;
|
||||
}
|
||||
@@ -47,6 +48,7 @@ asset::asset(asset && other) :
|
||||
asset & asset::operator=(asset && other)
|
||||
{
|
||||
std::swap(android_asset, other.android_asset);
|
||||
std::swap(bytes, other.bytes);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,368 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "libcurl.h"
|
||||
|
||||
#include "utils/mapped_file.h"
|
||||
#include "utils/named_thread.h"
|
||||
#include "version.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <curl/curl.h>
|
||||
#include <curl/easy.h>
|
||||
#include <curl/multi.h>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <magic_enum.hpp>
|
||||
#include <mutex>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
|
||||
libcurl::transfer::~transfer() {}
|
||||
libcurl::transfer_file::~transfer_file() {}
|
||||
libcurl::transfer_buffer::~transfer_buffer() {}
|
||||
|
||||
libcurl::libcurl()
|
||||
{
|
||||
if (CURLcode rc = curl_global_init(CURL_GLOBAL_DEFAULT); rc != CURLE_OK)
|
||||
{
|
||||
throw std::runtime_error{fmt::format("Cannot initialize libcurl: {}", curl_easy_strerror(rc))};
|
||||
|
||||
curl_version_info_data * data = curl_version_info(CURLVERSION_NOW);
|
||||
if (not(data->features & CURL_VERSION_THREADSAFE))
|
||||
spdlog::warn("libcurl features do not include CURL_VERSION_THREADSAFE");
|
||||
}
|
||||
|
||||
multi = curl_multi_init();
|
||||
if (not multi)
|
||||
{
|
||||
spdlog::error("curl_multi_init failed");
|
||||
return;
|
||||
}
|
||||
|
||||
curl_thread = utils::named_thread("curl_thread", &libcurl::curl_thread_fn, this);
|
||||
}
|
||||
|
||||
void libcurl::curl_thread_fn()
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
// For Android, read all the certificates at start-up, the file names are not
|
||||
// in the correct format for the current verion of OpenSSL.
|
||||
// See https://stackoverflow.com/questions/25253823/how-to-make-ssl-peer-verify-work-on-android
|
||||
std::string ca_bundle;
|
||||
for (const std::filesystem::directory_entry & entry: std::filesystem::directory_iterator{"/system/etc/security/cacerts"})
|
||||
{
|
||||
if (not entry.is_regular_file())
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
utils::mapped_file cert{entry.path()};
|
||||
ca_bundle += cert;
|
||||
|
||||
// Make sure there is a newline before the next certificate
|
||||
if (ca_bundle != "" and ca_bundle.back() != '\n')
|
||||
ca_bundle += '\n';
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Ignore errors
|
||||
}
|
||||
}
|
||||
|
||||
curl_blob ca_bundle_info{
|
||||
.data = ca_bundle.data(),
|
||||
.len = ca_bundle.size(),
|
||||
.flags = CURL_BLOB_NOCOPY,
|
||||
};
|
||||
#endif
|
||||
|
||||
std::string user_agent = std::string("WiVRn/") + wivrn::git_version;
|
||||
|
||||
// We can't use auto here because of curl_easy_setopt
|
||||
size_t (*write_callback)(void *, size_t, size_t, void *) = [](void * buffer, size_t size, size_t nmemb, void * userp) -> size_t {
|
||||
auto & xfer = *reinterpret_cast<libcurl::transfer *>(userp);
|
||||
|
||||
if (xfer.content_length < 0)
|
||||
{
|
||||
curl_off_t content_length;
|
||||
curl_easy_getinfo(xfer.curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &content_length);
|
||||
xfer.content_length = content_length;
|
||||
}
|
||||
|
||||
xfer.progress += size * nmemb;
|
||||
return xfer.write(buffer, size * nmemb);
|
||||
};
|
||||
|
||||
std::vector<std::shared_ptr<transfer>> current_transfers;
|
||||
while (not quit)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Wait for something to happen (network, new download, cancelled download)
|
||||
int numfds;
|
||||
if (CURLMcode rc = curl_multi_poll(multi, NULL, 0, 10000, &numfds); rc != CURLM_OK)
|
||||
throw std::runtime_error{fmt::format("curl_multi_poll failed: ", curl_multi_strerror(rc))};
|
||||
|
||||
// Only lock the mutex after curl_multi_poll
|
||||
std::unique_lock _{lock};
|
||||
|
||||
int still_running;
|
||||
if (CURLMcode rc = curl_multi_perform(multi, &still_running); rc != CURLM_OK)
|
||||
throw std::runtime_error{fmt::format("curl_multi_perform failed: ", curl_multi_strerror(rc))};
|
||||
|
||||
CURLMsg * msg;
|
||||
int msgs_in_queue;
|
||||
while ((msg = curl_multi_info_read(multi, &msgs_in_queue)) != nullptr)
|
||||
{
|
||||
auto iter = std::ranges::find_if(
|
||||
current_transfers,
|
||||
[&](std::shared_ptr<transfer> & x) {
|
||||
return x->curl == msg->easy_handle;
|
||||
});
|
||||
|
||||
if (iter == current_transfers.end())
|
||||
{
|
||||
spdlog::error("Received {} ({}) for an unknown transfer", magic_enum::enum_name(msg->msg), (int)msg->msg);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto & xfer = **iter;
|
||||
switch (msg->msg)
|
||||
{
|
||||
case CURLMSG_NONE: // Not used
|
||||
case CURLMSG_LAST: // Not used
|
||||
break;
|
||||
|
||||
case CURLMSG_DONE: // msg->easy_handle has finished
|
||||
xfer.curl_code = msg->data.result;
|
||||
curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &xfer.response_code);
|
||||
|
||||
if (msg->data.result == CURLE_OK)
|
||||
{
|
||||
xfer.current_state = state::done;
|
||||
xfer.finish();
|
||||
}
|
||||
else
|
||||
{
|
||||
xfer.current_state = state::error;
|
||||
xfer.cancel();
|
||||
}
|
||||
|
||||
curl_multi_remove_handle(multi, msg->easy_handle);
|
||||
curl_easy_cleanup(msg->easy_handle);
|
||||
current_transfers.erase(iter);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
for (std::shared_ptr<transfer> & xfer: pending_transfers)
|
||||
{
|
||||
// See https://curl.se/libcurl/c/libcurl-tutorial.html and https://curl.se/libcurl/c/curl_easy_setopt.html
|
||||
curl_easy_setopt(xfer->curl, CURLOPT_URL, xfer->url.c_str());
|
||||
curl_easy_setopt(xfer->curl, CURLOPT_FOLLOWLOCATION, 1); // Older versions of libcurl don't have the constant CURLFOLLOW_ALL
|
||||
curl_easy_setopt(xfer->curl, CURLOPT_FILETIME, 1); // Request remote file time
|
||||
curl_easy_setopt(xfer->curl, CURLOPT_FAILONERROR, 1); // CURLE_HTTP_RETURNED_ERROR on HTTP error
|
||||
curl_easy_setopt(xfer->curl, CURLOPT_WRITEFUNCTION, write_callback);
|
||||
curl_easy_setopt(xfer->curl, CURLOPT_WRITEDATA, xfer.get());
|
||||
curl_easy_setopt(xfer->curl, CURLOPT_USERAGENT, user_agent.c_str());
|
||||
|
||||
#ifdef __ANDROID__
|
||||
curl_easy_setopt(xfer->curl, CURLOPT_CAINFO_BLOB, &ca_bundle_info);
|
||||
#endif
|
||||
|
||||
curl_multi_add_handle(multi, xfer->curl);
|
||||
current_transfers.push_back(std::move(xfer));
|
||||
}
|
||||
|
||||
for (std::shared_ptr<transfer> handle: pending_cancellations)
|
||||
{
|
||||
auto iter = std::ranges::find(current_transfers, handle);
|
||||
|
||||
// If the transfer cannot be found, it means it has already finished, this is not an error
|
||||
if (iter != current_transfers.end())
|
||||
{
|
||||
auto & xfer = **iter;
|
||||
xfer.current_state = state::cancelled;
|
||||
xfer.cancel();
|
||||
|
||||
curl_multi_remove_handle(multi, handle->curl);
|
||||
curl_easy_cleanup(handle->curl);
|
||||
current_transfers.erase(iter);
|
||||
|
||||
spdlog::info("Cancelled transfer from {}", xfer.url);
|
||||
}
|
||||
}
|
||||
|
||||
pending_transfers.clear();
|
||||
pending_cancellations.clear();
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
spdlog::error("Exception in curl thread: {}", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_lock _{lock};
|
||||
for (auto & xfer: current_transfers)
|
||||
{
|
||||
xfer->current_state = state::cancelled;
|
||||
xfer->cancel();
|
||||
curl_multi_remove_handle(multi, xfer->curl);
|
||||
curl_easy_cleanup(xfer->curl);
|
||||
}
|
||||
|
||||
if (CURLMcode rc = curl_multi_cleanup(multi); rc != CURLM_OK)
|
||||
spdlog::warn("curl_multi_cleanup failed: {}", curl_multi_strerror(rc));
|
||||
}
|
||||
|
||||
libcurl::~libcurl()
|
||||
{
|
||||
quit = true;
|
||||
curl_multi_wakeup(multi);
|
||||
|
||||
if (curl_thread.joinable())
|
||||
curl_thread.join();
|
||||
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
void libcurl::curl_handle::sync()
|
||||
{
|
||||
if (lib and handle)
|
||||
{
|
||||
std::unique_lock _{lib->lock};
|
||||
|
||||
current_state = handle->current_state;
|
||||
curl_code = handle->curl_code;
|
||||
response_code = handle->response_code;
|
||||
content_length = handle->content_length;
|
||||
progress = handle->progress;
|
||||
|
||||
if (current_state == state::done)
|
||||
{
|
||||
if (auto h = dynamic_cast<transfer_buffer *>(handle.get()))
|
||||
{
|
||||
response = h->buffer;
|
||||
}
|
||||
else if (auto h = dynamic_cast<transfer_file *>(handle.get()))
|
||||
{
|
||||
path = h->final_path;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void libcurl::curl_handle::cancel()
|
||||
{
|
||||
if (lib and handle)
|
||||
{
|
||||
{
|
||||
std::unique_lock _{lib->lock};
|
||||
lib->pending_cancellations.emplace_back(handle);
|
||||
if (handle->current_state == state::transferring)
|
||||
handle->current_state = state::cancelling;
|
||||
}
|
||||
|
||||
curl_multi_wakeup(lib->multi);
|
||||
}
|
||||
}
|
||||
|
||||
size_t libcurl::transfer_file::write(void * data, size_t size) noexcept
|
||||
{
|
||||
stream.write(reinterpret_cast<const char *>(data), size);
|
||||
if (not stream)
|
||||
return CURL_WRITEFUNC_ERROR;
|
||||
return size;
|
||||
}
|
||||
|
||||
void libcurl::transfer_file::finish() noexcept
|
||||
{
|
||||
stream.close();
|
||||
std::filesystem::rename(temporary_path, final_path);
|
||||
|
||||
curl_off_t remote_timestamp;
|
||||
if (curl_easy_getinfo(curl, CURLINFO_FILETIME_T, &remote_timestamp) == CURLE_OK)
|
||||
{
|
||||
auto time = std::chrono::file_clock::from_sys(std::chrono::system_clock::from_time_t(remote_timestamp));
|
||||
std::filesystem::last_write_time(final_path, time);
|
||||
}
|
||||
}
|
||||
|
||||
void libcurl::transfer_file::cancel() noexcept
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(temporary_path, ec);
|
||||
}
|
||||
|
||||
libcurl::curl_handle libcurl::download(std::string url, std::filesystem::path path)
|
||||
{
|
||||
auto xfer = std::make_shared<transfer_file>();
|
||||
xfer->curl = curl_easy_init();
|
||||
xfer->url = url;
|
||||
xfer->final_path = path;
|
||||
xfer->temporary_path = path.native() + ".partial";
|
||||
xfer->stream.open(xfer->temporary_path, std::ios::out);
|
||||
xfer->current_state = state::transferring;
|
||||
|
||||
std::unique_lock _{lock};
|
||||
pending_transfers.push_back(xfer);
|
||||
|
||||
// The curl thread needs to be woken up to pick up the newly added handle without waiting for the timeout
|
||||
curl_multi_wakeup(multi);
|
||||
|
||||
return {this, xfer, path};
|
||||
}
|
||||
|
||||
size_t libcurl::transfer_buffer::write(void * data, size_t size) noexcept
|
||||
{
|
||||
buffer.append(reinterpret_cast<const char *>(data), size);
|
||||
return size;
|
||||
}
|
||||
|
||||
void libcurl::transfer_buffer::finish() noexcept
|
||||
{
|
||||
}
|
||||
|
||||
void libcurl::transfer_buffer::cancel() noexcept
|
||||
{
|
||||
}
|
||||
|
||||
libcurl::curl_handle libcurl::download(std::string url)
|
||||
{
|
||||
auto xfer = std::make_shared<transfer_buffer>();
|
||||
xfer->curl = curl_easy_init();
|
||||
xfer->url = url;
|
||||
xfer->current_state = state::transferring;
|
||||
|
||||
std::unique_lock _{lock};
|
||||
pending_transfers.push_back(xfer);
|
||||
|
||||
// The curl thread needs to be woken up to pick up the newly added handle without waiting for the timeout
|
||||
curl_multi_wakeup(multi);
|
||||
|
||||
return {this, xfer};
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <curl/curl.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
class libcurl
|
||||
{
|
||||
friend class curl_handle;
|
||||
|
||||
public:
|
||||
enum class state
|
||||
{
|
||||
reset,
|
||||
transferring,
|
||||
cancelling,
|
||||
cancelled,
|
||||
error,
|
||||
done,
|
||||
};
|
||||
|
||||
private:
|
||||
struct transfer
|
||||
{
|
||||
CURL * curl;
|
||||
std::string url;
|
||||
|
||||
state current_state = state::reset;
|
||||
CURLcode curl_code = CURLE_OK;
|
||||
long response_code = 0;
|
||||
int64_t content_length = -1;
|
||||
int64_t progress = 0;
|
||||
|
||||
transfer() = default;
|
||||
|
||||
// A pointer to a transfer struct is used as a user pointer for curl callbacks,
|
||||
// make sure it never moves
|
||||
transfer(const transfer &) = delete;
|
||||
transfer(transfer &&) = delete;
|
||||
transfer operator=(const transfer &) = delete;
|
||||
transfer operator=(transfer &&) = delete;
|
||||
virtual ~transfer();
|
||||
|
||||
virtual size_t write(void * data, size_t size) noexcept = 0;
|
||||
virtual void finish() noexcept = 0;
|
||||
virtual void cancel() noexcept = 0;
|
||||
};
|
||||
|
||||
struct transfer_file : transfer
|
||||
{
|
||||
virtual ~transfer_file();
|
||||
|
||||
std::filesystem::path temporary_path;
|
||||
std::filesystem::path final_path;
|
||||
std::fstream stream;
|
||||
|
||||
size_t write(void * data, size_t size) noexcept override;
|
||||
void finish() noexcept override;
|
||||
void cancel() noexcept override;
|
||||
};
|
||||
|
||||
struct transfer_buffer : transfer
|
||||
{
|
||||
virtual ~transfer_buffer();
|
||||
|
||||
std::string buffer;
|
||||
|
||||
size_t write(void * data, size_t size) noexcept override;
|
||||
void finish() noexcept override;
|
||||
void cancel() noexcept override;
|
||||
};
|
||||
|
||||
public:
|
||||
libcurl();
|
||||
libcurl(const libcurl &) = delete;
|
||||
libcurl(libcurl &&) = delete;
|
||||
libcurl operator=(const libcurl &) = delete;
|
||||
libcurl operator=(libcurl &&) = delete;
|
||||
~libcurl();
|
||||
|
||||
// Handle returned by the download() functions, it allows cancelling an ongoing transfer
|
||||
// by calling cancel() or destroying the handle
|
||||
class curl_handle
|
||||
{
|
||||
private:
|
||||
libcurl * lib = nullptr;
|
||||
std::shared_ptr<transfer> handle;
|
||||
|
||||
std::filesystem::path path;
|
||||
std::string response;
|
||||
|
||||
state current_state = state::reset;
|
||||
CURLcode curl_code = CURLE_OK;
|
||||
int response_code = 0;
|
||||
int64_t content_length = -1;
|
||||
int64_t progress = 0;
|
||||
|
||||
friend class libcurl;
|
||||
|
||||
curl_handle(libcurl * lib, std::shared_ptr<transfer> handle, std::filesystem::path path = {}) :
|
||||
lib(lib), handle(handle), path(std::move(path)) {}
|
||||
|
||||
public:
|
||||
curl_handle() = default;
|
||||
curl_handle(const curl_handle &) = delete;
|
||||
curl_handle & operator=(const curl_handle &) = delete;
|
||||
curl_handle(curl_handle && other) :
|
||||
lib(other.lib),
|
||||
handle(other.handle),
|
||||
path(std::move(other.path)),
|
||||
response(std::move(other.response)),
|
||||
current_state(other.current_state),
|
||||
curl_code(other.curl_code),
|
||||
response_code(other.response_code),
|
||||
content_length(other.content_length),
|
||||
progress(other.progress)
|
||||
{
|
||||
other.lib = nullptr;
|
||||
other.handle = nullptr;
|
||||
}
|
||||
|
||||
curl_handle & operator=(curl_handle && other)
|
||||
{
|
||||
std::swap(lib, other.lib);
|
||||
std::swap(handle, other.handle);
|
||||
path = std::move(other.path);
|
||||
response = std::move(other.response);
|
||||
current_state = other.current_state;
|
||||
curl_code = other.curl_code;
|
||||
response_code = other.response_code;
|
||||
content_length = other.content_length;
|
||||
progress = other.progress;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
~curl_handle()
|
||||
{
|
||||
cancel();
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
*this = curl_handle{};
|
||||
}
|
||||
|
||||
void sync();
|
||||
void cancel();
|
||||
|
||||
state get_state() const
|
||||
{
|
||||
return current_state;
|
||||
}
|
||||
|
||||
CURLcode get_curl_code() const
|
||||
{
|
||||
return curl_code;
|
||||
}
|
||||
|
||||
int get_response_code() const
|
||||
{
|
||||
return response_code;
|
||||
}
|
||||
|
||||
int64_t get_content_length() const
|
||||
{
|
||||
return content_length;
|
||||
}
|
||||
|
||||
int64_t get_progress() const
|
||||
{
|
||||
return progress;
|
||||
}
|
||||
|
||||
const std::string & get_response() const // Only if started by download(url) and state is done
|
||||
{
|
||||
assert(current_state == state::done);
|
||||
return response;
|
||||
}
|
||||
|
||||
std::span<const std::byte> get_response_bytes() const // Only if started by download(url) and state is done
|
||||
{
|
||||
assert(current_state == state::done);
|
||||
return {reinterpret_cast<const std::byte *>(response.data()), response.size()};
|
||||
}
|
||||
|
||||
const std::filesystem::path & get_path() const // Only if started by download(url, path) and state is done
|
||||
{
|
||||
assert(current_state == state::done);
|
||||
return path;
|
||||
}
|
||||
|
||||
const std::string & get_url() const
|
||||
{
|
||||
return handle->url;
|
||||
}
|
||||
};
|
||||
|
||||
// Start a download, data is saved on the filesystem
|
||||
// Keep the returned handle or the transfer will be immediately cancelled
|
||||
curl_handle download(std::string url, std::filesystem::path path);
|
||||
|
||||
// Start a download, data is buffered and can be fetched through the get_response() function of the curl_handle
|
||||
// Keep the returned handle or the transfer will be immediately cancelled
|
||||
curl_handle download(std::string url);
|
||||
|
||||
private:
|
||||
std::thread curl_thread;
|
||||
void curl_thread_fn();
|
||||
|
||||
std::atomic<bool> quit{false};
|
||||
CURLM * multi;
|
||||
|
||||
std::mutex lock; // Locks pending_transfers, pending_cancellations and all struct transfers created by this instance
|
||||
std::vector<std::shared_ptr<transfer>> pending_transfers;
|
||||
std::vector<std::shared_ptr<transfer>> pending_cancellations;
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
if (NOT TARGET OpenSSL::Crypto)
|
||||
# The NDK does not include OpenSSL, download it
|
||||
set(OPENSSL_VERSION "3.4.0")
|
||||
if(NOT EXISTS ${CMAKE_BINARY_DIR}/openssl-src)
|
||||
if(NOT EXISTS ${CMAKE_BINARY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz)
|
||||
if (EXISTS ${CMAKE_SOURCE_DIR}/openssl-${OPENSSL_VERSION}.tar.gz)
|
||||
file(CREATE_LINK ${CMAKE_SOURCE_DIR}/openssl-${OPENSSL_VERSION}.tar.gz ${CMAKE_BINARY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz SYMBOLIC)
|
||||
else()
|
||||
file(DOWNLOAD https://github.com/openssl/openssl/archive/refs/tags/openssl-${OPENSSL_VERSION}.tar.gz ${CMAKE_BINARY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz
|
||||
EXPECTED_HASH SHA256=1ca043a26fbea74cdf7faf623a6f14032a01117d141c4a5208ccac819ccc896b)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
file(ARCHIVE_EXTRACT INPUT ${CMAKE_BINARY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz DESTINATION ${CMAKE_BINARY_DIR})
|
||||
file(RENAME ${CMAKE_BINARY_DIR}/openssl-openssl-${OPENSSL_VERSION} ${CMAKE_BINARY_DIR}/openssl-src)
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E env ANDROID_NDK_ROOT=${CMAKE_ANDROID_NDK} PATH=${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin:$ENV{PATH} ./Configure
|
||||
android-arm64
|
||||
--prefix=${CMAKE_BINARY_DIR}/openssl
|
||||
--openssldir=${CMAKE_BINARY_DIR}/openssl
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/openssl-src
|
||||
OUTPUT_FILE ${CMAKE_BINARY_DIR}/openssl-config-out
|
||||
ERROR_FILE ${CMAKE_BINARY_DIR}/openssl-config-err
|
||||
)
|
||||
|
||||
if (NOT EXISTS ${CMAKE_BINARY_DIR}/openssl/lib/libcrypto.so)
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E env ANDROID_NDK_ROOT=${CMAKE_ANDROID_NDK} PATH=${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin:$ENV{PATH} make -j${CMAKE_BUILD_PARALLEL_LEVEL}
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/openssl-src
|
||||
OUTPUT_FILE ${CMAKE_BINARY_DIR}/openssl-make-out
|
||||
ERROR_FILE ${CMAKE_BINARY_DIR}/openssl-make-err
|
||||
)
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E env ANDROID_NDK_ROOT=${CMAKE_ANDROID_NDK} PATH=${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin:$ENV{PATH} make install_sw
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/openssl-src
|
||||
OUTPUT_FILE ${CMAKE_BINARY_DIR}/openssl-install-out
|
||||
ERROR_FILE ${CMAKE_BINARY_DIR}/openssl-install-err
|
||||
)
|
||||
endif()
|
||||
|
||||
add_library(OpenSSL::Crypto STATIC IMPORTED)
|
||||
set_property(TARGET OpenSSL::Crypto PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/openssl/lib/libcrypto.so)
|
||||
target_include_directories(OpenSSL::Crypto INTERFACE ${CMAKE_BINARY_DIR}/openssl/include)
|
||||
|
||||
add_library(OpenSSL::SSL STATIC IMPORTED)
|
||||
set_property(TARGET OpenSSL::SSL PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/openssl/lib/libssl.so)
|
||||
target_include_directories(OpenSSL::SSL INTERFACE ${CMAKE_BINARY_DIR}/openssl/include)
|
||||
endif()
|
||||
+1
-46
@@ -1,6 +1,7 @@
|
||||
if (WIVRN_USE_SYSTEM_BOOST)
|
||||
add_library(Boost::pfr ALIAS Boost::headers)
|
||||
elseif (WIVRN_BUILD_CLIENT)
|
||||
set(BOOST_INCLUDE_LIBRARIES locale url pfr)
|
||||
FetchContent_MakeAvailable(boost)
|
||||
else()
|
||||
FetchContent_MakeAvailable(boostpfr)
|
||||
@@ -79,52 +80,6 @@ if(ANDROID)
|
||||
|
||||
target_include_directories(wivrn-common SYSTEM PUBLIC ${CMAKE_BINARY_DIR}/Vulkan-Headers-vulkan-sdk-${VULKAN_VERSION}/include)
|
||||
target_compile_definitions(wivrn-common PUBLIC VMA_VULKAN_VERSION=1001000)
|
||||
|
||||
|
||||
# The NDK does not include OpenSSL, download it
|
||||
set(OPENSSL_VERSION "3.4.0")
|
||||
if(NOT EXISTS ${CMAKE_BINARY_DIR}/openssl-src)
|
||||
if(NOT EXISTS ${CMAKE_BINARY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz)
|
||||
if (EXISTS ${CMAKE_SOURCE_DIR}/openssl-${OPENSSL_VERSION}.tar.gz)
|
||||
file(CREATE_LINK ${CMAKE_SOURCE_DIR}/openssl-${OPENSSL_VERSION}.tar.gz ${CMAKE_BINARY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz SYMBOLIC)
|
||||
else()
|
||||
file(DOWNLOAD https://github.com/openssl/openssl/archive/refs/tags/openssl-${OPENSSL_VERSION}.tar.gz ${CMAKE_BINARY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz
|
||||
EXPECTED_HASH SHA256=1ca043a26fbea74cdf7faf623a6f14032a01117d141c4a5208ccac819ccc896b)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
file(ARCHIVE_EXTRACT INPUT ${CMAKE_BINARY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz DESTINATION ${CMAKE_BINARY_DIR})
|
||||
file(RENAME ${CMAKE_BINARY_DIR}/openssl-openssl-${OPENSSL_VERSION} ${CMAKE_BINARY_DIR}/openssl-src)
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E env ANDROID_NDK_ROOT=${CMAKE_ANDROID_NDK} PATH=${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin:$ENV{PATH} ./Configure
|
||||
android-arm64
|
||||
--prefix=${CMAKE_BINARY_DIR}/openssl
|
||||
--openssldir=${CMAKE_BINARY_DIR}/openssl
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/openssl-src
|
||||
OUTPUT_FILE ${CMAKE_BINARY_DIR}/openssl-config-out
|
||||
ERROR_FILE ${CMAKE_BINARY_DIR}/openssl-config-err
|
||||
)
|
||||
|
||||
if (NOT EXISTS ${CMAKE_BINARY_DIR}/openssl/lib/libcrypto.so)
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E env ANDROID_NDK_ROOT=${CMAKE_ANDROID_NDK} PATH=${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin:$ENV{PATH} make -j${CMAKE_BUILD_PARALLEL_LEVEL}
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/openssl-src
|
||||
OUTPUT_FILE ${CMAKE_BINARY_DIR}/openssl-make-out
|
||||
ERROR_FILE ${CMAKE_BINARY_DIR}/openssl-make-err
|
||||
)
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E env ANDROID_NDK_ROOT=${CMAKE_ANDROID_NDK} PATH=${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin:$ENV{PATH} make install_sw
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/openssl-src
|
||||
OUTPUT_FILE ${CMAKE_BINARY_DIR}/openssl-install-out
|
||||
ERROR_FILE ${CMAKE_BINARY_DIR}/openssl-install-err
|
||||
)
|
||||
endif()
|
||||
|
||||
add_library(OpenSSL::Crypto STATIC IMPORTED)
|
||||
set_property(TARGET OpenSSL::Crypto PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/openssl/lib/libcrypto.so)
|
||||
target_include_directories(OpenSSL::Crypto INTERFACE ${CMAKE_BINARY_DIR}/openssl/include)
|
||||
else()
|
||||
target_link_libraries(wivrn-common PUBLIC Vulkan::Headers)
|
||||
endif()
|
||||
|
||||
Reference in New Issue
Block a user