diff --git a/libraries/SocketWrapper/src/MbedClient.cpp b/libraries/SocketWrapper/src/MbedClient.cpp index ad1a14cbc..538005799 100644 --- a/libraries/SocketWrapper/src/MbedClient.cpp +++ b/libraries/SocketWrapper/src/MbedClient.cpp @@ -21,7 +21,6 @@ void arduino::MbedClient::readSocket() { do { if (rxBuffer.availableForStore() == 0) { yield(); - delay(100); continue; } mutex->lock(); @@ -34,7 +33,6 @@ void arduino::MbedClient::readSocket() { } if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0) { yield(); - delay(100); mutex->unlock(); continue; } @@ -71,7 +69,7 @@ void arduino::MbedClient::configureSocket(Socket *_s) { } mutex->lock(); if (reader_th == nullptr) { - reader_th = new rtos::Thread; + reader_th = new rtos::Thread(osPriorityNormal - 2); reader_th->start(mbed::callback(this, &MbedClient::readSocket)); } mutex->unlock(); @@ -80,6 +78,15 @@ void arduino::MbedClient::configureSocket(Socket *_s) { } int arduino::MbedClient::connect(SocketAddress socketAddress) { + + if (sock && reader_th) { + // trying to reuse a connection, let's call stop() to cleanup the state + char c; + if (sock->recv(&c, 1) < 0) { + stop(); + } + } + if (sock == nullptr) { sock = new TCPSocket(); _own_socket = true; @@ -206,7 +213,7 @@ size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) { int ret = NSAPI_ERROR_WOULD_BLOCK; do { ret = sock->send(buf, size); - } while (ret != size && connected()); + } while ((ret != size && ret == NSAPI_ERROR_WOULD_BLOCK) && connected()); configureSocket(sock); return size; } diff --git a/libraries/SocketWrapper/src/MbedClient.h b/libraries/SocketWrapper/src/MbedClient.h index 07e615cae..a2132ebf3 100644 --- a/libraries/SocketWrapper/src/MbedClient.h +++ b/libraries/SocketWrapper/src/MbedClient.h @@ -73,7 +73,7 @@ class MbedClient : public arduino::Client { int connectSSL(IPAddress ip, uint16_t port); int connectSSL(const char* host, uint16_t port, bool disableSNI = false); size_t write(uint8_t); - size_t write(const uint8_t* buf, size_t size); + size_t write(const uint8_t* buf, size_t size) override; int available(); int read(); int read(uint8_t* buf, size_t size);