/* * 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 "wivrn_ipc.h" #include "wivrn_packets.h" #include "wivrn_sockets.h" #include static void handle_event_from_main_loop(to_monado::disconnect) { // Ignore disconnect request when no headset is connected } std::unique_ptr accept_connection(int watch_fd, std::function quit) { wivrn_ipc_socket_monado->send(from_monado::headsdet_disconnected{}); xrt::drivers::wivrn::TCPListener listener(xrt::drivers::wivrn::default_port); pollfd fds[3]{ {.fd = watch_fd, .events = POLLIN}, {.fd = listener.get_fd(), .events = POLLIN}, {.fd = wivrn_ipc_socket_monado->get_fd(), .events = POLLIN}, }; while (not(quit and quit())) { if (poll(fds, std::size(fds), 100) < 0) { perror("poll"); return {}; } if (fds[0].revents & POLLIN) return {}; if (fds[1].revents & POLLIN) { wivrn_ipc_socket_monado->send(from_monado::headsdet_connected{}); return std::make_unique(listener.accept().first); } if (fds[2].revents & POLLIN) { auto packet = receive_from_main(); if (packet) std::visit([](auto && x) { handle_event_from_main_loop(x); }, *packet); } } return {}; }