@@ -22,28 +22,30 @@ void arduino::MbedClient::readSocket() {
2222int ret = NSAPI_ERROR_WOULD_BLOCK;
2323do {
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 ();
2731yield ();
2832continue ;
29- }else if (sock ==nullptr ) {
30- goto cleanup;
3133 }
3234 ret = sock->recv (data, rxBuffer.availableForStore ());
3335if (ret <0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
36+ mutex->unlock ();
3437goto cleanup;
3538 }
3639if (ret == NSAPI_ERROR_WOULD_BLOCK || ret ==0 ) {
37- yield ();
3840 mutex->unlock ();
39- continue ;
41+ break ;
4042 }
4143for (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 ;
@@ -98,6 +100,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
98100 }
99101
100102if (static_cast <TCPSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
103+ _status =false ;
101104return 0 ;
102105 }
103106
@@ -117,6 +120,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
117120configureSocket (sock);
118121 _status =true ;
119122 }else {
123+ sock->close ();
120124 _status =false ;
121125 }
122126
@@ -148,6 +152,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
148152 }
149153
150154if (static_cast <TLSSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
155+ _status =false ;
151156return 0 ;
152157 }
153158
@@ -179,6 +184,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
179184configureSocket (sock);
180185 _status =true ;
181186 }else {
187+ sock->close ();
182188 _status =false ;
183189 }
184190
@@ -224,8 +230,9 @@ int arduino::MbedClient::available() {
224230}
225231
226232int arduino::MbedClient::read () {
227- if (sock ==nullptr )
233+ if (mutex ==nullptr ) {
228234return -1 ;
235+ }
229236 mutex->lock ();
230237if (!available ()) {
231238 mutex->unlock ();
@@ -238,8 +245,9 @@ int arduino::MbedClient::read() {
238245}
239246
240247int arduino::MbedClient::read (uint8_t *data,size_t len) {
241- if (sock ==nullptr )
248+ if (mutex ==nullptr ) {
242249return 0 ;
250+ }
243251 mutex->lock ();
244252int avail =available ();
245253
@@ -261,7 +269,14 @@ int arduino::MbedClient::read(uint8_t *data, size_t len) {
261269}
262270
263271int arduino::MbedClient::peek () {
264- return rxBuffer.peek ();
272+ if (mutex ==nullptr ) {
273+ return 0 ;
274+ }
275+ mutex->lock ();
276+ int res = rxBuffer.peek ();
277+ mutex->unlock ();
278+
279+ return res;
265280}
266281
267282void arduino::MbedClient::flush () {