diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index f62d4558..fb845aa1 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -65,6 +65,7 @@ endif() add_executable(wivrn-server + accept_connection.cpp active_runtime.cpp avahi_publisher.cpp hostname.cpp diff --git a/server/accept_connection.cpp b/server/accept_connection.cpp new file mode 100644 index 00000000..46ff0612 --- /dev/null +++ b/server/accept_connection.cpp @@ -0,0 +1,68 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022-2024 Guillaume Meunier + * Copyright (C) 2022-2024 Patrick Nicolas + * + * 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 . + */ + +#include "accept_connection.h" + +#include "avahi_publisher.h" +#include "driver/configuration.h" +#include "hostname.h" +#include "version.h" +#include "wivrn_packets.h" +#include "wivrn_sockets.h" + +#include +#include + +static void avahi_set_bool_callback(AvahiWatch * w, int fd, AvahiWatchEvent event, void * userdata) +{ + bool * flag = (bool *)userdata; + *flag = true; +} + +std::unique_ptr accept_connection(int watch_fd) +{ + char protocol_string[17]; + sprintf(protocol_string, "%016lx", xrt::drivers::wivrn::protocol_version); + + std::map TXT = { + {"protocol", protocol_string}, + {"version", xrt::drivers::wivrn::git_version}, + {"cookie", server_cookie()}, + }; + + avahi_publisher publisher(hostname().c_str(), "_wivrn._tcp", xrt::drivers::wivrn::default_port, TXT); + + xrt::drivers::wivrn::TCPListener listener(xrt::drivers::wivrn::default_port); + bool client_connected = false; + bool fd_triggered = false; + + AvahiWatch * watch_listener = publisher.watch_new(listener.get_fd(), AVAHI_WATCH_IN, &avahi_set_bool_callback, &client_connected); + AvahiWatch * watch_user = publisher.watch_new(watch_fd, AVAHI_WATCH_IN, &avahi_set_bool_callback, &fd_triggered); + + while (publisher.iterate() && !client_connected && !fd_triggered) + ; + + publisher.watch_free(watch_listener); + publisher.watch_free(watch_user); + + if (client_connected) + return std::make_unique(listener.accept().first); + + return nullptr; +} diff --git a/server/accept_connection.h b/server/accept_connection.h new file mode 100644 index 00000000..7a6bf9b4 --- /dev/null +++ b/server/accept_connection.h @@ -0,0 +1,24 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022-2024 Guillaume Meunier + * Copyright (C) 2022-2024 Patrick Nicolas + * + * 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 . + */ + +#pragma once + +#include + +std::unique_ptr accept_connection(int watch_fd); diff --git a/server/main.cpp b/server/main.cpp index 7a029dd2..ffb9bd16 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -10,24 +10,23 @@ #include "util/u_trace_marker.h" +#include "accept_connection.h" +#include "active_runtime.h" #include "driver/configuration.h" +#include "pidfd.h" +#include "version.h" +#include "wivrn_config.h" #include "wivrn_ipc.h" -#include "wivrn_packets.h" #include "wivrn_sockets.h" + #include #include +#include #include #include #include #include -#include "pidfd.h" -#include "version.h" -#include "wivrn_config.h" - -#include "active_runtime.h" -#include "avahi_publisher.h" -#include "hostname.h" #include #include @@ -44,12 +43,6 @@ using namespace xrt::drivers::wivrn; std::unique_ptr tcp; -void avahi_set_bool_callback(AvahiWatch * w, int fd, AvahiWatchEvent event, void * userdata) -{ - bool * flag = (bool *)userdata; - *flag = true; -} - #ifdef WIVRN_USE_SYSTEMD std::string socket_path() { @@ -207,29 +200,10 @@ int inner_main(int argc, char * argv[]) { try { - avahi_publisher publisher(hostname().c_str(), "_wivrn._tcp", default_port, TXT); - - TCPListener listener(default_port); - bool client_connected = false; - bool sigint_received = false; - - AvahiWatch * watch_listener = publisher.watch_new(listener.get_fd(), AVAHI_WATCH_IN, &avahi_set_bool_callback, &client_connected); - AvahiWatch * watch_sigint = publisher.watch_new(sigint_fd, AVAHI_WATCH_IN, &avahi_set_bool_callback, &sigint_received); - - while (publisher.iterate() && !client_connected && !sigint_received) - ; - - publisher.watch_free(watch_listener); - publisher.watch_free(watch_sigint); - - if (client_connected) - { - tcp = std::make_unique(listener.accept().first); - + tcp = accept_connection(sigint_fd); + if (tcp) init_cleanup_functions(); - } - - if (sigint_received) + else break; } catch (std::exception & e)