fix(input): serialize queued packets per stream (#5818)

This commit is contained in:
Dave Lane authored and GitHub committed 2026-09-27 13:53:30 -04:00
1 parent 66abe63bf4
commit c69d370c35
3 files changed
+233 -6

No files matched your search

+102 -6
View File
@@ -177,6 +177,26 @@ namespace input {
static std::function<void(const testing::keyboard_event_t &)> sink;
return sink;
}
/**
* @brief Return the optional callback for observing queued packet dispatch in tests.
*
* @return Mutable test callback.
*/
std::function<void(std::uint32_t)> &input_packet_hook() {
static std::function<void(std::uint32_t)> hook;
return hook;
}
/**
* @brief Return the optional callback for observing input task scheduling in tests.
*
* @return Mutable test callback.
*/
std::function<void(std::shared_ptr<input_t>)> &input_task_sink() {
static std::function<void(std::shared_ptr<input_t>)> sink;
return sink;
}
#endif
static std::bitset<platf::MAX_GAMEPADS> gamepadMask {};
@@ -298,6 +318,7 @@ namespace input {
std::list<std::vector<uint8_t>> input_queue; ///< Validated input packets waiting for processing.
std::mutex input_queue_lock; ///< Input queue lock.
bool input_task_scheduled {}; ///< Whether one worker task owns packet dispatch for this stream; guarded by input_queue_lock.
thread_pool_util::ThreadPool::task_id_t mouse_left_button_timeout; ///< Mouse left button timeout.
@@ -2020,10 +2041,34 @@ namespace input {
}
/**
* @brief Called on a thread pool thread to process an input message.
* @param input The input context pointer.
* @brief Process a bounded batch of queued packets for one stream.
*
* @param input Retained stream input state.
*/
void passthrough_next_message(std::shared_ptr<input_t> input) {
void passthrough_queued_messages(std::shared_ptr<input_t> input);
/**
* @brief Queue one packet-processing task for a stream.
*
* @param input Retained stream input state.
*/
void schedule_input_packet_task(const std::shared_ptr<input_t> &input) {
#ifdef SUNSHINE_TESTS
if (const auto &sink = input_task_sink(); sink) {
sink(input);
return;
}
#endif
task_pool.push(passthrough_queued_messages, input);
}
/**
* @brief Process one queued input packet for a stream.
*
* @param input Retained stream input state.
* @return True if a packet was processed, or false when the stream queue is empty.
*/
bool passthrough_next_message(std::shared_ptr<input_t> &input) {
// 'entry' backs the 'payload' pointer, so they must remain in scope together
std::vector<uint8_t> entry;
PNV_INPUT_HEADER payload;
@@ -2036,7 +2081,8 @@ namespace input {
// If all entries have already been processed, nothing to do
if (input->input_queue.empty()) {
return;
input->input_task_scheduled = false;
return false;
}
// Pop off the first entry, which we will send
@@ -2067,6 +2113,12 @@ namespace input {
// Print the final input packet
input::print((void *) payload);
#ifdef SUNSHINE_TESTS
if (const auto &hook = input_packet_hook(); hook) {
hook(util::endian::little(payload->magic));
}
#endif
// Send the batched input to the OS
switch (util::endian::little(payload->magic)) {
case MOUSE_MOVE_REL_MAGIC_GEN5:
@@ -2114,6 +2166,31 @@ namespace input {
passthrough(input, (PSS_CONTROLLER_BATTERY_PACKET) payload);
break;
}
return true;
}
void passthrough_queued_messages(std::shared_ptr<input_t> input) {
// Bound each turn so other task-pool work can run during sustained input.
constexpr std::size_t packets_per_task = 32;
for (std::size_t processed = 0; processed < packets_per_task; ++processed) {
if (!passthrough_next_message(input)) {
return;
}
}
// Keep ownership until the next task is queued. New packets arriving meanwhile
// see input_task_scheduled and do not create a competing worker.
bool has_more;
{
std::lock_guard lock {input->input_queue_lock};
has_more = !input->input_queue.empty();
if (!has_more) {
input->input_task_scheduled = false;
}
}
if (has_more) {
schedule_input_packet_task(input);
}
}
/**
@@ -2136,11 +2213,18 @@ namespace input {
return;
}
bool schedule_task = false;
{
std::lock_guard<std::mutex> lg(input->input_queue_lock);
std::lock_guard lock {input->input_queue_lock};
input->input_queue.push_back(std::move(input_data));
if (!input->input_task_scheduled) {
input->input_task_scheduled = true;
schedule_task = true;
}
}
if (schedule_task) {
schedule_input_packet_task(input);
}
task_pool.push(passthrough_next_message, input);
}
/**
@@ -2360,6 +2444,18 @@ namespace input {
return input->gamepads[client_index].id;
}
void set_input_packet_hook(std::function<void(std::uint32_t)> hook) {
input_packet_hook() = std::move(hook);
}
void set_input_task_sink(std::function<void(std::shared_ptr<input_t>)> sink) {
input_task_sink() = std::move(sink);
}
void process_queued_messages(std::shared_ptr<input_t> input) {
::input::passthrough_queued_messages(std::move(input));
}
void set_keyboard_sink(std::function<void(const keyboard_event_t &)> sink) {
keyboard_sink() = std::move(sink);
}