From f7f0ae60bb5ffe8bc2279ee52486f341ae4f4d40 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 24 Jan 2025 21:53:22 +0100 Subject: [PATCH] Allow service publication to be set in json --- docs/configuration.md | 7 +++++ server/driver/configuration.cpp | 52 ++++++++++++++++++++------------- server/driver/configuration.h | 8 +++++ server/main.cpp | 38 +++++++++++++++--------- 4 files changed, 71 insertions(+), 34 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 6e8ea30a..389861af 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -170,3 +170,10 @@ If `false` or unset, WiVRn will use both TCP and UDP. "tcp_only": true } ``` + +## `publish-service` +Default value: `avahi` + +How to publish the service over the network, `avahi` or null. + +If set to null, service will not be published and address has to be entered manually on the headset. diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index ec6bfd41..23b0ed28 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -50,6 +50,15 @@ NLOHMANN_JSON_SERIALIZE_ENUM( {av1, "AV1"}, }) +NLOHMANN_JSON_SERIALIZE_ENUM( + service_publication, + { + {service_publication(-1), ""}, + {service_publication::none, nullptr}, + {service_publication::none, "none"}, + {service_publication::avahi, "avahi"}, + }) + void configuration::set_config_file(const std::filesystem::path & path) { config_file = path; @@ -93,44 +102,47 @@ configuration configuration::read_user_configuration() { auto json = nlohmann::json::parse(file); - if (json.contains("scale")) + if (auto it = json.find("scale"); it != json.end()) { - if (json["scale"].is_number()) - result.scale = std::array{json["scale"], json["scale"]}; + if (it->is_number()) + result.scale = std::array{*it, *it}; else - result.scale = json["scale"]; + result.scale = *it; } - if (json.contains("bitrate")) - { - result.bitrate = json["bitrate"]; - } + if (auto it = json.find("bitrate"); it != json.end()) + result.bitrate = *it; - if (json.contains("encoders")) + if (auto it = json.find("encoders"); it != json.end()) { - for (const auto & encoder: json["encoders"]) + for (const auto & encoder: *it) result.encoders.push_back(parse_encoder(encoder)); } - if (json.contains("encoder-passthrough")) - result.encoder_passthrough = parse_encoder(json["encoder-passthrough"]); + if (auto it = json.find("encoder-passthrough"); it != json.end()) + result.encoder_passthrough = parse_encoder(*it); - if (json.contains("application")) + if (auto it = json.find("application"); it != json.end()) { - if (json["application"].is_string()) - result.application.push_back(json["application"]); + if (it->is_string()) + result.application.push_back(*it); else { - for (const auto & i: json["application"]) - { + for (const auto & i: *it) result.application.push_back(i); - } } } - if (json.contains("tcp_only")) + if (auto it = json.find("tcp-only"); it != json.end()) + result.tcp_only = *it; + else if (auto it = json.find("tcp_only"); it != json.end()) + result.tcp_only = *it; + + if (auto it = json.find("publish-service"); it != json.end()) { - result.tcp_only = json["tcp_only"]; + result.publication = *it; + if (result.publication == service_publication(-1)) + throw std::runtime_error("invalid service publication " + it->get()); } } catch (const std::exception & e) diff --git a/server/driver/configuration.h b/server/driver/configuration.h index a185299e..60bfd9c8 100644 --- a/server/driver/configuration.h +++ b/server/driver/configuration.h @@ -29,6 +29,13 @@ namespace wivrn { + +enum class service_publication +{ + none, + avahi, +}; + struct configuration { struct encoder @@ -50,6 +57,7 @@ struct configuration std::optional> scale; std::vector application; bool tcp_only = false; + service_publication publication = service_publication::avahi; static void set_config_file(const std::filesystem::path &); static const std::filesystem::path & get_config_file(); diff --git a/server/main.cpp b/server/main.cpp index 0ad5b806..7f372184 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -205,7 +205,7 @@ pid_t app_pid; bool quitting_main_loop; bool do_fork; bool do_active_runtime; -bool avahi_publish; +wivrn::service_publication publication; guint listener_watch; @@ -384,19 +384,25 @@ void stop_listening() void start_publishing() { - if (not avahi_publish) - return; - if (publisher) - return; + switch (publication) + { + case wivrn::service_publication::none: + return; - char protocol_string[17]; - sprintf(protocol_string, "%016" PRIx64, wivrn::protocol_version); - std::map TXT = { - {"protocol", protocol_string}, - {"version", wivrn::git_version}, - {"cookie", server_cookie()}, - }; - publisher.emplace(poll_api, hostname(), "_wivrn._tcp", wivrn::default_port, TXT); + case wivrn::service_publication::avahi: { + if (publisher) + return; + + char protocol_string[17]; + sprintf(protocol_string, "%016" PRIx64, wivrn::protocol_version); + std::map TXT = { + {"protocol", protocol_string}, + {"version", wivrn::git_version}, + {"cookie", server_cookie()}, + }; + publisher.emplace(poll_api, hostname(), "_wivrn._tcp", wivrn::default_port, TXT); + } + } } void stop_publishing() @@ -985,13 +991,17 @@ int main(int argc, char * argv[]) do_active_runtime = not *no_active_runtime; do_fork = not *no_fork; - avahi_publish = not *no_publish; if (*no_encrypt) enc_state = wivrn_connection::encryption_state::disabled; if (not config_file.empty()) configuration::set_config_file(config_file); + if (*no_publish) + publication = wivrn::service_publication::none; + else + publication = configuration::read_user_configuration().publication; + #if WIVRN_USE_SYSTEMD if (*app_flag) return exec_application(configuration::read_user_configuration());