@@ -22,28 +22,30 @@ void arduino::MbedClient::readSocket() {
2222 int ret = NSAPI_ERROR_WOULD_BLOCK;
2323 do {
2424 mutex->lock ();
25- if (sock != nullptr && rxBuffer.availableForStore () == 0 ) {
25+ if (sock == nullptr ) {
26+ mutex->unlock ();
27+ goto cleanup;
28+ }
29+ if (rxBuffer.availableForStore () == 0 ) {
2630 mutex->unlock ();
2731 yield ();
2832 continue ;
29- } else if (sock == nullptr ) {
30- goto cleanup;
3133 }
3234 ret = sock->recv (data, rxBuffer.availableForStore ());
3335 if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
36+ mutex->unlock ();
3437 goto cleanup;
3538 }
3639 if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0 ) {
37- yield ();
3840 mutex->unlock ();
39- continue ;
41+ break ;
4042 }
4143 for (int i = 0 ; i < ret; i++) {
4244 rxBuffer.store_char (data[i]);
4345 }
4446 mutex->unlock ();
4547 _status = true ;
46- } while (ret == NSAPI_ERROR_WOULD_BLOCK || ret > 0 );
48+ } while (true );
4749 }
4850cleanup:
4951 _status = false ;
@@ -92,6 +94,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
9294 }
9395
9496 if (static_cast <TCPSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
97+ _status = false ;
9598 return 0 ;
9699 }
97100
@@ -111,6 +114,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
111114 configureSocket (sock);
112115 _status = true ;
113116 } else {
117+ sock->close ();
114118 _status = false ;
115119 }
116120
@@ -145,6 +149,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
145149 }
146150
147151 if (static_cast <TLSSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
152+ _status = false ;
148153 return 0 ;
149154 }
150155
@@ -176,6 +181,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
176181 configureSocket (sock);
177182 _status = true ;
178183 } else {
184+ sock->close ();
179185 _status = false ;
180186 }
181187
@@ -221,8 +227,9 @@ int arduino::MbedClient::available() {
221227}
222228
223229int arduino::MbedClient::read () {
224- if (sock == nullptr )
230+ if (mutex == nullptr ) {
225231 return -1 ;
232+ }
226233 mutex->lock ();
227234 if (!available ()) {
228235 mutex->unlock ();
@@ -235,8 +242,9 @@ int arduino::MbedClient::read() {
235242}
236243
237244int arduino::MbedClient::read (uint8_t *data, size_t len) {
238- if (sock == nullptr )
245+ if (mutex == nullptr ) {
239246 return 0 ;
247+ }
240248 mutex->lock ();
241249 int avail = available ();
242250
@@ -258,7 +266,14 @@ int arduino::MbedClient::read(uint8_t *data, size_t len) {
258266}
259267
260268int arduino::MbedClient::peek () {
261- return rxBuffer.peek ();
269+ if (mutex == nullptr ) {
270+ return 0 ;
271+ }
272+ mutex->lock ();
273+ int res = rxBuffer.peek ();
274+ mutex->unlock ();
275+
276+ return res;
262277}
263278
264279void arduino::MbedClient::flush () {
0 commit comments