From 15c4b9b6a38a2d1d90cf93395afb1b53e8e074d6 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 09:11:49 +0000 Subject: [PATCH] Ethernet2UART gateway: never lose a sensor query around a reconnect The sensor answers only when queried, and it re-sends its query after every attempt without reply (idle mode: getState2() in stage 1 again). The gateway must deliver each of these queries: - Queued requests are no longer dropped by closePort(). A query queued while the I/O thread ended for a reconnect is sent on the new connection (the previous commit dropped it, so the sensor stayed unqueried for that attempt). - sendData() queues while the connection is up, while the I/O thread is still starting (TH_ARMED: the first query right after a connect was refused with "Trying to Send Data Before Thread is Started!"), while the thread has ended and the reconnect is pending, and while create_SOL() is connecting. It refuses only without any connection. - At most E2UART_MAX_QUEUED (16) requests wait; the oldest are dropped. A failed reconnect clears the queue, because those queries are stale. - The "NO reply" debug line shows the request bytes (first 12), so a repeated query can be recognised in the log. Tested with a fake device that goes silent after 3 replies: the query sent while the thread was starting and the query queued during the reconnect were both answered, and nothing was refused (ASan clean). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/deviceNodeEthernet2Serial.cpp | 71 +++++++++++++++----- lib/App/common/deviceNodeEthernet2Serial.h | 6 ++ 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/lib/App/common/deviceNodeEthernet2Serial.cpp b/lib/App/common/deviceNodeEthernet2Serial.cpp index e4c941f9..366ffc4f 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.cpp +++ b/lib/App/common/deviceNodeEthernet2Serial.cpp @@ -52,6 +52,7 @@ deviceETH2UART::deviceETH2UART(uint32_t nodeID) : m_iInFID = -1; m_iOutFID = -1; m_bRegistered = false; + m_bConnecting = false; #ifdef SERIAL_PING_PONG m_pLastParser = NULL; #endif @@ -87,6 +88,15 @@ deviceETH2UART::~deviceETH2UART() } std::lock_guard tLock(m_mConnLock); closePort(); +#ifdef SERIAL_PING_PONG + m_mBufferProtectorMutex.lock(); + while (! m_qIO.empty()) + { + free(m_qIO.front()); + m_qIO.pop(); + }; + m_mBufferProtectorMutex.unlock(); +#endif } bool deviceETH2UART::create(void) @@ -181,6 +191,16 @@ bool deviceETH2UART::connectSOL(void) m_bLastPingOk = true; return true; }; +#ifdef SERIAL_PING_PONG + // No connection: the queued requests are stale by the next attempt (the protocols query again) + m_mBufferProtectorMutex.lock(); + while (! m_qIO.empty()) + { + free(m_qIO.front()); + m_qIO.pop(); + }; + m_mBufferProtectorMutex.unlock(); +#endif m_nConnectBackoffMs = (m_nConnectBackoffMs == 0) ? E2UART_RECONNECT_MIN_MS : m_nConnectBackoffMs * 2; if (m_nConnectBackoffMs > E2UART_RECONNECT_MAX_MS) { @@ -428,6 +448,12 @@ bool deviceETH2UART::create_SOL() { closePort(); }; + struct connectingFlag + { + volatile bool & f; + explicit connectingFlag(volatile bool & x) : f(x) { f = true; } + ~connectingFlag() { f = false; } + } tConnecting(m_bConnecting); switch (m_sUartData.mode) { @@ -810,16 +836,8 @@ bool deviceETH2UART::closePort() { uninit(); }; -#ifdef SERIAL_PING_PONG - // Requests not sent on the closed connection: drop them (the protocols re-send after a reconnect) - m_mBufferProtectorMutex.lock(); - while (! m_qIO.empty()) - { - free(m_qIO.front()); - m_qIO.pop(); - }; - m_mBufferProtectorMutex.unlock(); -#endif + // Requests still queued are kept: a query of the sensor queued during a reconnect is sent on the new + // connection (dropping it left the sensor without a query - it only answers when asked) if (m_iDeviceFD != -1) { switch (m_sUartData.mode) @@ -975,6 +993,17 @@ void deviceETH2UART::m_fThreadProc(void) //Sending Data to line #ifdef DEBUG_COMMUNICATION int tSentBytes = tIOData->out_size; + char tReqHex[3 * 12 + 4]; + { + int tN = (tSentBytes < 12) ? tSentBytes : 12; + for (int k = 0; k < tN; k++){ + snprintf(tReqHex + 3 * k, 4, "%02X ", tIOData->out_buffer[k]); + }; + tReqHex[tN ? 3 * tN - 1 : 0] = 0; + if (tSentBytes > 12){ + strcat(tReqHex, ".."); + }; + } uint64_t tSendMs = e2uNowMs(); tReqNo++; tStReq++; @@ -1051,7 +1080,7 @@ void deviceETH2UART::m_fThreadProc(void) tStNoReply++; char tTcp[160]; tcpInfoText(tTcp, sizeof(tTcp)); - snprintf(tDbg, sizeof(tDbg), "IO: request #%u (%d B): NO reply in %llu ms - %s", tReqNo, tSentBytes, (unsigned long long) (tDoneMs - tSendMs), tTcp); + snprintf(tDbg, sizeof(tDbg), "IO: request #%u [%s]: NO reply in %llu ms - %s", tReqNo, tReqHex, (unsigned long long) (tDoneMs - tSendMs), tTcp); LOG(tDbg); } else { uint64_t tFirst = tFirstByteMs - tSendMs; @@ -1089,12 +1118,11 @@ void deviceETH2UART::m_fThreadProc(void) } bool deviceETH2UART::sendData(uint8_t * Data, int DataSize, protocolParser *src ){ - if ( m_bThreadStatus != TH_WORKING ) { - if( m_bThreadStatus < TH_WORKING ) { - LOG_ERROR("Trying to Send Data Before Thread is Started!"); - } else { - LOG_ERROR("Trying to Send Data After Thread is Dead!"); - } + // Queued (sent as soon as the I/O thread runs) while the connection is up, starting (TH_ARMED: the + // first query right after a connect was refused), ended for a reconnect that validate() will do, + // or being opened. Refused only without any connection. + if ( m_bThreadStatus == TH_PRE_INIT && ! m_bConnecting ) { + LOG_ERROR("Trying to Send Data without connection to the gateway!"); return false; }; if (DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH) { @@ -1106,9 +1134,18 @@ bool deviceETH2UART::sendData(uint8_t * Data, int DataSize, protocolParser *src tData->out_size = DataSize; tData->boundParser = src; memcpy( tData->out_buffer, Data, DataSize ); + int tDropped = 0; m_mBufferProtectorMutex.lock(); + while ((int) m_qIO.size() >= E2UART_MAX_QUEUED){ + free(m_qIO.front()); + m_qIO.pop(); + tDropped++; + }; m_qIO.push(tData); m_mBufferProtectorMutex.unlock(); + if (tDropped){ + LOG_ERROR("Request queue full - oldest request dropped"); + }; return true; } else { LOG_ERROR("MemAlloc Error!"); diff --git a/lib/App/common/deviceNodeEthernet2Serial.h b/lib/App/common/deviceNodeEthernet2Serial.h index 90daaacf..5c904a02 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.h +++ b/lib/App/common/deviceNodeEthernet2Serial.h @@ -44,6 +44,11 @@ #endif // Requests in a row without any reply byte: reconnect to the gateway (the device went silent while // the TCP connection stayed up - a new connection resets the gateway session / NAT mapping) +// Requests waiting for the I/O thread (e.g. queries of the sensor during a reconnect): the oldest are +// dropped beyond this (a long outage must not grow the queue without limit) +#ifndef E2UART_MAX_QUEUED + #define E2UART_MAX_QUEUED 16 +#endif #ifndef E2UART_NO_REPLY_RECONNECT #define E2UART_NO_REPLY_RECONNECT 5 #endif @@ -71,6 +76,7 @@ // hardware manager thread (the class is a deviceNode and a hardwareResource): one at a time std::recursive_mutex m_mConnLock; bool m_bRegistered; // assigned to the hardware manager (once) + volatile bool m_bConnecting; // create_SOL() is opening the connection (requests are queued meanwhile) bool m_bLastPingOk; uint64_t m_nNextPingMs; uint64_t m_nNextConnectMs;