From b38f7a4aa2595e6b36208490d7df12960e4d98dc Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 16 Jul 2026 17:09:56 +0200 Subject: [PATCH 1/2] tcp_server: allow a client to signal its status The _connected flag was never restored to 0 when the previous client was being disconnected. Would be better to implement using cpp and friend classes, leaving a reference in the Client if generated by accept() --- src/tcp_client.h | 15 +++++++++++++-- src/tcp_server.h | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/tcp_client.h b/src/tcp_client.h index 84df9d2..a97df35 100644 --- a/src/tcp_client.h +++ b/src/tcp_client.h @@ -27,6 +27,7 @@ #define DEFAULT_TCP_CLIENT_BUF_SIZE 512 #define TCP_RESPONSE_HEADER_SIZE 20 // max size of msg_id plus RCP headers for a TCP response +void disconnect_server(void* opaque_server); template class BridgeTCPClient : public Client { @@ -37,11 +38,16 @@ class BridgeTCPClient : public Client { RingBufferN temp_buffer; struct k_mutex client_mutex{}; bool _connected = false; + void* opaque_server = nullptr; // Pointer to the server that spawned this client, if any public: explicit BridgeTCPClient(BridgeClass& bridge): bridge(&bridge) {} - BridgeTCPClient(BridgeClass& bridge, uint32_t connection_id, bool connected=true): bridge(&bridge), connection_id(connection_id), _connected {connected} {} + BridgeTCPClient(BridgeClass& bridge, uint32_t connection_id, bool connected=false, void* opaque_server=nullptr): bridge(&bridge), connection_id(connection_id), _connected{connected}, opaque_server(opaque_server) {} + + bool operator==(const BridgeTCPClient& whs) { + return connection_id == whs.connection_id; + } bool begin() { k_mutex_init(&client_mutex); @@ -127,6 +133,9 @@ class BridgeTCPClient : public Client { int read() override { uint8_t c; + if (!temp_buffer.available()) { + return -1; + } read(&c, 1); return c; } @@ -165,6 +174,9 @@ class BridgeTCPClient : public Client { if (_connected) { _connected = !bridge->call(TCP_CLOSE_METHOD, connection_id).result(msg); } + if (opaque_server) { + disconnect_server(opaque_server); + } k_mutex_unlock(&client_mutex); } @@ -219,7 +231,6 @@ class BridgeTCPClient : public Client { k_mutex_unlock(&client_mutex); } - }; diff --git a/src/tcp_server.h b/src/tcp_server.h index dc863a6..d2de3eb 100644 --- a/src/tcp_server.h +++ b/src/tcp_server.h @@ -64,12 +64,12 @@ class BridgeTCPServer final: public Server { if (!_listening) { // Not listening -> return disconnected (invalid) client k_mutex_unlock(&server_mutex); - return BridgeTCPClient(*bridge, 0, false); + return BridgeTCPClient(*bridge, 0, false, this); } if (_connected) { // Connection already established return a client copy k_mutex_unlock(&server_mutex); - return BridgeTCPClient(*bridge, connection_id); + return BridgeTCPClient(*bridge, connection_id, true, this); } // Accept a connection @@ -77,8 +77,9 @@ class BridgeTCPServer final: public Server { _connected = ret; k_mutex_unlock(&server_mutex); + // If no connection established return a disconnected (invalid) client - return ret? BridgeTCPClient(*bridge, connection_id) : BridgeTCPClient(*bridge, 0, false); + return ret? BridgeTCPClient(*bridge, connection_id, true, this) : BridgeTCPClient(*bridge, 0, false, this); } size_t write(uint8_t c) override { @@ -152,4 +153,13 @@ class BridgeTCPServer final: public Server { }; +inline void disconnect_server(void* opaque_server) { + + if (opaque_server) { + auto server = static_cast*>(opaque_server); + server->disconnect(); + } +} + + #endif //BRIDGE_TCP_SERVER_H \ No newline at end of file From ea8e0cead09af12f9ef6968f3c6290aa915cd68e Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 17 Jul 2026 10:34:12 +0200 Subject: [PATCH 2/2] tcp_client: close connection on failed write() --- src/tcp_client.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/tcp_client.h b/src/tcp_client.h index a97df35..c4c3774 100644 --- a/src/tcp_client.h +++ b/src/tcp_client.h @@ -119,6 +119,9 @@ class BridgeTCPClient : public Client { k_mutex_lock(&client_mutex, K_FOREVER); const bool ok = bridge->call(TCP_WRITE_METHOD, connection_id, payload).result(written); k_mutex_unlock(&client_mutex); + if (!ok) { + stop(); + } return ok? written : 0; }