From 713138b7811da451c7954c8d8d354a4a0798c4c6 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 24 Jan 2025 20:31:59 +0100 Subject: [PATCH] Allow setting the PIN in the intent url --- CMakeLists.txt | 8 +++--- client/CMakeLists.txt | 2 +- client/application.cpp | 58 ++++++++++++++++++++++------------------- client/application.h | 2 +- client/scenes/lobby.cpp | 7 ++--- client/wivrn_discover.h | 1 + 6 files changed, 42 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b851fa2..2b4ad52d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,15 +79,15 @@ include(VulkanVersion) if (WIVRN_USE_SYSTEM_BOOST STREQUAL "AUTO") if(WIVRN_BUILD_DISSECTOR) - find_package(Boost 1.84.0 COMPONENTS locale) + find_package(Boost 1.84.0 COMPONENTS locale url) else() - find_package(Boost 1.75.0 COMPONENTS locale) + find_package(Boost 1.75.0 COMPONENTS locale url) endif() elseif(WIVRN_USE_SYSTEM_BOOST STREQUAL "ON") if(WIVRN_BUILD_DISSECTOR) - find_package(Boost 1.84.0 REQUIRED COMPONENTS locale) + find_package(Boost 1.84.0 REQUIRED COMPONENTS locale url) else() - find_package(Boost 1.75.0 REQUIRED COMPONENTS locale) + find_package(Boost 1.75.0 REQUIRED COMPONENTS locale url) endif() endif() diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index da0fac04..0ddb578d 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -89,7 +89,7 @@ file(GLOB_RECURSE VULKAN_SHADERS CONFIGURE_DEPENDS "*.glsl") target_sources(wivrn PRIVATE ${LOCAL_SOURCE} ${VULKAN_SHADERS}) wivrn_compile_glsl(wivrn ${VULKAN_SHADERS}) -target_link_libraries(wivrn Vulkan::Vulkan spdlog::spdlog glm::glm fastgltf FreetypeHarfbuzz stb Boost::locale) +target_link_libraries(wivrn Vulkan::Vulkan spdlog::spdlog glm::glm fastgltf FreetypeHarfbuzz stb Boost::url Boost::locale) if (WIVRN_USE_LIBKTX) target_link_libraries(wivrn ktx_read) diff --git a/client/application.cpp b/client/application.cpp index 7b2fe785..d5fd5fc9 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -33,6 +33,7 @@ #include "xr/xr.h" #include #include +#include #include #include #include @@ -975,37 +976,33 @@ extern "C" __attribute__((visibility("default"))) void Java_org_meumeu_wivrn_Mai } #endif +static bool is_tcp_scheme(boost::core::string_view scheme) +{ + if (scheme.empty() or scheme == "wivrn") + return false; + if (scheme == "wivrn+tcp") + return true; + throw std::runtime_error("invalid URI scheme " + std::string(scheme)); +} + void application::set_server_uri(std::string uri) { - std::string server_address; - bool server_tcp_only = false; - - if (uri.starts_with("wivrn://")) - { - server_address = uri.substr(strlen("wivrn://")); - } - else if (uri.starts_with("wivrn+tcp://")) - { - server_address = uri.substr(strlen("wivrn+tcp://")); - server_tcp_only = true; - } - - auto colon = server_address.rfind(":"); - int port = wivrn::default_port; - if (colon != std::string::npos) - { - port = std::stoi(server_address.substr(colon + 1)); - server_address = server_address.substr(0, colon); - } + auto parse_result = boost::urls::parse_uri(uri); + if (parse_result.has_error()) + throw boost::system::system_error(parse_result.error(), "failed to parse uri"); + if (not parse_result.has_value()) + throw std::runtime_error("failed to parse uri, empty result"); { std::unique_lock _{server_intent_mutex}; - server_intent = wivrn_discover::service{ - .name = "", - .hostname = server_address, - .port = port, - .tcp_only = server_tcp_only, - }; + server_intent = + wivrn_discover::service{ + .name = "", + .hostname = parse_result->host(), + .port = parse_result->has_port() ? parse_result->port_number() : wivrn::default_port, + .tcp_only = is_tcp_scheme(parse_result->scheme()), + .pin = parse_result->password(), + }; } } @@ -1027,7 +1024,14 @@ application::application(application_info info) : if (auto data_string = intent.call("getDataString")) { spdlog::info("Started with intent {}", (std::string)data_string); - set_server_uri(data_string); + try + { + set_server_uri(data_string); + } + catch (std::exception & e) + { + spdlog::warn("failed to set server uri: {}", e.what()); + } } auto files_dir = ctx.call>("getFilesDir"); diff --git a/client/application.h b/client/application.h index 73df114f..ebd3a3e8 100644 --- a/client/application.h +++ b/client/application.h @@ -355,7 +355,7 @@ public: { std::unique_lock _{instance().server_intent_mutex}; - std::optional intent = instance().server_intent; + auto intent = instance().server_intent; instance().server_intent.reset(); return intent; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 225fcb7b..e3994429 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -277,12 +277,12 @@ std::unique_ptr scenes::lobby::connect_to_session(wivrn_discover: { spdlog::debug("Connection to {}", address_string); - return std::visit([this, port = service.port, tcp_only = service.tcp_only](auto & address) { - return std::make_unique(address, port, tcp_only, keypair, [&](int fd) { + return std::visit([this, &service](auto & address) { + return std::make_unique(address, service.port, service.tcp_only, keypair, [&](int fd) { auto request = pin_request.lock(); request->pin_requested = true; request->pin_cancelled = false; - request->pin = ""; + request->pin = service.pin; pin_buffer = ""; while (not request.wait_for(500ms, [&]() { return request->pin != "" or request->pin_cancelled; })) @@ -705,6 +705,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) { if (auto intent = application::get_intent()) { + pin_request.lock()->pin = intent->pin; connect(configuration::server_data{ .manual = true, .service = *intent, diff --git a/client/wivrn_discover.h b/client/wivrn_discover.h index cd32a231..97ec77f3 100644 --- a/client/wivrn_discover.h +++ b/client/wivrn_discover.h @@ -24,6 +24,7 @@ public: std::vector> addresses; std::map txt; std::chrono::steady_clock::time_point ttl; + std::string pin; }; static inline const std::chrono::milliseconds poll_min_time{500};