mirror of
https://github.com/WiVRn/WiVRn.git
synced 2026-11-20 00:51:32 -08:00
72 lines
2.0 KiB
C++
72 lines
2.0 KiB
C++
/*
|
|
* WiVRn VR streaming
|
|
* Copyright (C) 2022-2024 Guillaume Meunier <[email protected]>
|
|
* Copyright (C) 2022-2024 Patrick Nicolas <[email protected]>
|
|
*
|
|
* 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 "accept_connection.h"
|
|
|
|
#include "wivrn_ipc.h"
|
|
#include "wivrn_packets.h"
|
|
#include "wivrn_sockets.h"
|
|
|
|
#include <sys/poll.h>
|
|
|
|
static void handle_event_from_main_loop(to_monado::disconnect)
|
|
{
|
|
// Ignore disconnect request when no headset is connected
|
|
}
|
|
|
|
std::unique_ptr<wivrn::TCP> wivrn::accept_connection(int watch_fd, std::function<bool()> quit)
|
|
{
|
|
wivrn_ipc_socket_monado->send(from_monado::headset_disconnected{});
|
|
|
|
wivrn::TCPListener listener(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::headset_connected{});
|
|
return std::make_unique<wivrn::TCP>(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 {};
|
|
}
|