From dad8f23800dcc32c44ec0c0cb85fa21e4e54a95d Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Tue, 16 Apr 2024 16:20:26 +0200 Subject: [PATCH 1/3] fixing reader_th function when stop() is called on a client when the rxBuffer is not empty the reader thread won't be joined to the main thread due to the logic implemented in the readSocket function --- libraries/SocketWrapper/src/MbedClient.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libraries/SocketWrapper/src/MbedClient.cpp b/libraries/SocketWrapper/src/MbedClient.cpp index ee2947304..1007a4ab3 100644 --- a/libraries/SocketWrapper/src/MbedClient.cpp +++ b/libraries/SocketWrapper/src/MbedClient.cpp @@ -13,20 +13,24 @@ uint8_t arduino::MbedClient::status() { return _status; } + void arduino::MbedClient::readSocket() { - while (1) { + uint8_t data[SOCKET_BUFFER_SIZE]; + + while (sock != nullptr) { event->wait_any(0xFF, 100); - uint8_t data[SOCKET_BUFFER_SIZE]; int ret = NSAPI_ERROR_WOULD_BLOCK; do { + mutex->lock(); + if (sock == nullptr) { + goto cleanup; + } + mutex->unlock(); if (rxBuffer.availableForStore() == 0) { yield(); continue; } mutex->lock(); - if (sock == nullptr) { - goto cleanup; - } ret = sock->recv(data, rxBuffer.availableForStore()); if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) { goto cleanup; From 12096f8df74612a31759d3f23160658d0259cf41 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Tue, 16 Apr 2024 16:24:20 +0200 Subject: [PATCH 2/3] adding rxBuffer.clear() in stop() --- libraries/SocketWrapper/src/MbedClient.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/SocketWrapper/src/MbedClient.cpp b/libraries/SocketWrapper/src/MbedClient.cpp index 1007a4ab3..55d0393f0 100644 --- a/libraries/SocketWrapper/src/MbedClient.cpp +++ b/libraries/SocketWrapper/src/MbedClient.cpp @@ -299,6 +299,7 @@ void arduino::MbedClient::stop() { mutex = nullptr; } _status = false; + rxBuffer.clear(); } uint8_t arduino::MbedClient::connected() { From abaef3f145576ff105cbfd89acc1e903d1bc3bfd Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Tue, 16 Apr 2024 16:24:34 +0200 Subject: [PATCH 3/3] removing spaces in file --- libraries/SocketWrapper/src/MbedClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SocketWrapper/src/MbedClient.cpp b/libraries/SocketWrapper/src/MbedClient.cpp index 55d0393f0..78e9f63d1 100644 --- a/libraries/SocketWrapper/src/MbedClient.cpp +++ b/libraries/SocketWrapper/src/MbedClient.cpp @@ -65,7 +65,7 @@ void arduino::MbedClient::configureSocket(Socket *_s) { _s->set_timeout(_timeout); _s->set_blocking(false); _s->getpeername(&address); - + if (event == nullptr) { event = new rtos::EventFlags; }