From d11559374f72d232932177df057cc168b9168665 Mon Sep 17 00:00:00 2001 From: Rob Dobson Date: Sun, 15 Nov 2026 23:50:58 +0000 Subject: [PATCH] Comms loop: handle inbound messages before sending queued outbound ones A response to a request was queued in one pass of the loop and sent in the next, because the outbound queue was processed before the inbound one: every response waited a main-loop period (about 2 ms) on every channel. Inbound is now handled first, so the response goes out in the pass that handled the request. Measured on an ESP32-S3: the response was queued for sending 2.0 to 2.3 ms after the handler ran, now 0.12 to 0.16 ms; a USB serial request went from 5.8 ms to 3.9 ms. Co-Authored-By: Claude Fable 5.2 --- .../comms/CommsChannels/CommsChannelManager.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/components/comms/CommsChannels/CommsChannelManager.cpp b/components/comms/CommsChannels/CommsChannelManager.cpp index 38144fb..de2fd87 100644 --- a/components/comms/CommsChannels/CommsChannelManager.cpp +++ b/components/comms/CommsChannels/CommsChannelManager.cpp @@ -89,6 +89,15 @@ void CommsChannelManager::loop() // (inbound messages from other tasks are just queued and don't need the codec) ensureProtocolCodecExists(channelID); + // Inbound messages - possibly multiple messages. These are handled before the outbound queue is serviced + // so that the response to a request is sent in this pass (if the channel can take it) and not the next, + // which is a whole main-loop period later + for (int msgIdx = 0; msgIdx < MAX_INBOUND_MSGS_IN_LOOP; msgIdx++) + { + if (!pChannel->processInboundQueue()) + break; + } + // Check for an outbound message (its type, without copying it) CommsChannelMsg msg; CommsMsgTypeCode nextMsgType = MSG_TYPE_COMMAND; @@ -138,12 +147,6 @@ void CommsChannelManager::loop() } } - // Inbound messages - possibly multiple messages - for (int msgIdx = 0; msgIdx < MAX_INBOUND_MSGS_IN_LOOP; msgIdx++) - { - if (!pChannel->processInboundQueue()) - break; - } } // Service bridges