From dbe0b071ed2f1ef5f676a38c17acc6fe62bdf8c4 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Sat, 29 Jun 2024 15:13:06 +0200 Subject: [PATCH] SocketWrapper MbedClient debugging readSocket --- libraries/SocketWrapper/src/MbedClient.cpp | 35 +++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/libraries/SocketWrapper/src/MbedClient.cpp b/libraries/SocketWrapper/src/MbedClient.cpp index ad74db1ca..0035f875e 100644 --- a/libraries/SocketWrapper/src/MbedClient.cpp +++ b/libraries/SocketWrapper/src/MbedClient.cpp @@ -22,28 +22,30 @@ void arduino::MbedClient::readSocket() { int ret = NSAPI_ERROR_WOULD_BLOCK; do { mutex->lock(); - if (sock != nullptr && rxBuffer.availableForStore() == 0) { + if (sock == nullptr) { + mutex->unlock(); + goto cleanup; + } + if (rxBuffer.availableForStore() == 0) { mutex->unlock(); yield(); continue; - } else if (sock == nullptr) { - goto cleanup; } ret = sock->recv(data, rxBuffer.availableForStore()); if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) { + mutex->unlock(); goto cleanup; } if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0) { - yield(); mutex->unlock(); - continue; + break; } for (int i = 0; i < ret; i++) { rxBuffer.store_char(data[i]); } mutex->unlock(); _status = true; - } while (ret == NSAPI_ERROR_WOULD_BLOCK || ret > 0); + } while (true); } cleanup: _status = false; @@ -98,6 +100,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) { } if (static_cast(sock)->open(getNetwork()) != NSAPI_ERROR_OK) { + _status = false; return 0; } @@ -117,6 +120,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) { configureSocket(sock); _status = true; } else { + sock->close(); _status = false; } @@ -148,6 +152,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) { } if (static_cast(sock)->open(getNetwork()) != NSAPI_ERROR_OK) { + _status = false; return 0; } @@ -179,6 +184,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) { configureSocket(sock); _status = true; } else { + sock->close(); _status = false; } @@ -211,7 +217,7 @@ size_t arduino::MbedClient::write(uint8_t c) { size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) { if (sock == nullptr) return 0; - + sock->set_timeout(_timeout); int ret = sock->send(buf, size); sock->set_blocking(false); @@ -224,8 +230,9 @@ int arduino::MbedClient::available() { } int arduino::MbedClient::read() { - if (sock == nullptr) + if (mutex == nullptr) { return -1; + } mutex->lock(); if (!available()) { mutex->unlock(); @@ -238,8 +245,9 @@ int arduino::MbedClient::read() { } int arduino::MbedClient::read(uint8_t *data, size_t len) { - if (sock == nullptr) + if (mutex == nullptr) { return 0; + } mutex->lock(); int avail = available(); @@ -261,7 +269,14 @@ int arduino::MbedClient::read(uint8_t *data, size_t len) { } int arduino::MbedClient::peek() { - return rxBuffer.peek(); + if (mutex == nullptr) { + return 0; + } + mutex->lock(); + int res = rxBuffer.peek(); + mutex->unlock(); + + return res; } void arduino::MbedClient::flush() {