Pass session and stop token through reconnect flow

This commit is contained in:
Sapphire
2025-12-31 00:59:36 -06:00
parent 416297f232
commit a3c7a604e0
6 changed files with 28 additions and 30 deletions
+8 -3
View File
@@ -22,13 +22,14 @@
#include "util/u_logging.h" #include "util/u_logging.h"
#include "utils/overloaded.h" #include "utils/overloaded.h"
#include "driver/wivrn_session.h"
#include "wivrn_config.h" #include "wivrn_config.h"
#include "wivrn_ipc.h" #include "wivrn_ipc.h"
#include "wivrn_sockets.h" #include "wivrn_sockets.h"
#include <sys/poll.h> #include <sys/poll.h>
std::unique_ptr<wivrn::TCP> wivrn::accept_connection(std::function<bool()> quit) std::unique_ptr<wivrn::TCP> wivrn::accept_connection(wivrn_session & cnx, std::stop_token stop, std::function<void(wivrn_session &)> tick)
{ {
wivrn_ipc_socket_monado->send(from_monado::headset_disconnected{}); wivrn_ipc_socket_monado->send(from_monado::headset_disconnected{});
@@ -39,7 +40,7 @@ std::unique_ptr<wivrn::TCP> wivrn::accept_connection(std::function<bool()> quit)
{.fd = wivrn_ipc_socket_monado->get_fd(), .events = POLLIN}, {.fd = wivrn_ipc_socket_monado->get_fd(), .events = POLLIN},
}; };
while (not(quit and quit())) while (not stop.stop_requested())
{ {
if (poll(fds, std::size(fds), 100) < 0) if (poll(fds, std::size(fds), 100) < 0)
{ {
@@ -58,9 +59,10 @@ std::unique_ptr<wivrn::TCP> wivrn::accept_connection(std::function<bool()> quit)
auto packet = receive_from_main(); auto packet = receive_from_main();
if (packet) if (packet)
std::visit(utils::overloaded{ std::visit(utils::overloaded{
[](to_monado::stop) { [&cnx](to_monado::stop) {
// gets handled in wivrn_session::reconnect since we return nullptr // gets handled in wivrn_session::reconnect since we return nullptr
U_LOG_I("Received stop packet during reconnect, stopping"); U_LOG_I("Received stop packet during reconnect, stopping");
cnx.request_stop();
}, },
[](auto &&) { [](auto &&) {
// Ignore request when no headset is connected // Ignore request when no headset is connected
@@ -68,6 +70,9 @@ std::unique_ptr<wivrn::TCP> wivrn::accept_connection(std::function<bool()> quit)
}, },
*packet); *packet);
} }
if (tick)
tick(cnx);
} }
return {}; return {};
+3 -1
View File
@@ -21,9 +21,11 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <stop_token>
namespace wivrn namespace wivrn
{ {
class TCP; class TCP;
std::unique_ptr<wivrn::TCP> accept_connection(std::function<bool()> quit = std::function<bool()>()); class wivrn_session;
std::unique_ptr<wivrn::TCP> accept_connection(wivrn_session & cnx, std::stop_token stop, std::function<void(wivrn_session &)> tick = {});
} // namespace wivrn } // namespace wivrn
+2 -2
View File
@@ -293,13 +293,13 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
wivrn::update_last_connection_timestamp(clean_key(headset_key.public_key())); wivrn::update_last_connection_timestamp(clean_key(headset_key.public_key()));
} }
void wivrn::wivrn_connection::reset(TCP && tcp, std::function<void()> tick) void wivrn::wivrn_connection::reset(std::stop_token stop, TCP && tcp, std::function<void()> tick)
{ {
if (stream) if (stream)
stream = decltype(stream)(); stream = decltype(stream)();
control = std::move(tcp); control = std::move(tcp);
init({}, tick); init(stop, tick);
} }
void wivrn::wivrn_connection::shutdown() void wivrn::wivrn_connection::shutdown()
+1 -1
View File
@@ -73,7 +73,7 @@ public:
{ {
return active; return active;
} }
void reset(TCP && tcp, std::function<void()> tick = []() {}); void reset(std::stop_token stop, TCP && tcp, std::function<void()> tick = {});
void shutdown(); void shutdown();
template <typename T> template <typename T>
+12 -22
View File
@@ -2,6 +2,7 @@
* WiVRn VR streaming * WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net> * Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net> * Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
* Copyright (C) 2025 Sapphire <imsapphire0@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -1052,7 +1053,7 @@ void wivrn_session::run(std::stop_token stop)
catch (const std::exception & e) catch (const std::exception & e)
{ {
U_LOG_E("Exception in network thread: %s", e.what()); U_LOG_E("Exception in network thread: %s", e.what());
reconnect(); reconnect(stop);
refresh.reset(); refresh.reset();
} }
} }
@@ -1077,18 +1078,17 @@ void wivrn_session::dump_time(const std::string & event, uint64_t frame, int64_t
} }
} }
static bool quit_if_no_client(u_system & xrt_system) void wivrn_session::quit_if_no_client()
{ {
scoped_lock lock(xrt_system.sessions.mutex);
if (xrt_system.sessions.count == 0)
{ {
scoped_lock lock(xrt_system.sessions.mutex); U_LOG_I("No OpenXR client connected, exiting");
if (xrt_system.sessions.count) request_stop();
return false;
} }
U_LOG_I("No OpenXR client connected, exiting");
return true;
} }
void wivrn_session::reconnect() void wivrn_session::reconnect(std::stop_token stop)
{ {
assert(mnd_ipc_server); assert(mnd_ipc_server);
// Notify clients about disconnected status // Notify clients about disconnected status
@@ -1106,23 +1106,18 @@ void wivrn_session::reconnect()
} }
U_LOG_I("Waiting for new connection"); U_LOG_I("Waiting for new connection");
auto tcp = accept_connection([this]() { return quit_if_no_client(xrt_system); }); auto tcp = accept_connection(*this, stop, &wivrn_session::quit_if_no_client);
if (stop.stop_requested())
return;
if (not tcp) if (not tcp)
{ {
request_stop(); request_stop();
return; return;
} }
struct no_client_connected
{};
try try
{ {
offset_est.reset(); offset_est.reset();
connection->reset(std::move(*tcp), [this]() { connection->reset(stop, std::move(*tcp), [this]() { quit_if_no_client(); });
if (quit_if_no_client(xrt_system))
throw no_client_connected{};
});
const auto & info = connection->info(); const auto & info = connection->info();
(*this)(info.settings); (*this)(info.settings);
@@ -1143,11 +1138,6 @@ void wivrn_session::reconnect()
U_LOG_W("Failed to notify session state change"); U_LOG_W("Failed to notify session state change");
} }
} }
catch (no_client_connected)
{
U_LOG_I("No OpenXR application connected");
request_stop();
}
catch (const std::exception & e) catch (const std::exception & e)
{ {
U_LOG_E("Reconnection failed: %s", e.what()); U_LOG_E("Reconnection failed: %s", e.what());
+2 -1
View File
@@ -162,6 +162,7 @@ public:
void stop(); void stop();
bool request_stop(); bool request_stop();
void quit_if_no_client();
clock_offset get_offset(); clock_offset get_offset();
bool connected(); bool connected();
@@ -247,7 +248,7 @@ public:
private: private:
void run(std::stop_token stop); void run(std::stop_token stop);
void reconnect(); void reconnect(std::stop_token stop);
void poll_session_loss(); void poll_session_loss();