@@ -22,28 +22,31 @@ 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+ yield ();
42+ break ;
4043 }
4144for (int i =0 ; i < ret; i++) {
4245 rxBuffer.store_char (data[i]);
4346 }
4447 mutex->unlock ();
4548 _status =true ;
46- }while (ret == NSAPI_ERROR_WOULD_BLOCK || ret > 0 );
49+ }while (true );
4750 }
4851cleanup:
4952 _status =false ;
@@ -98,6 +101,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
98101 }
99102
100103if (static_cast <TCPSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
104+ _status =false ;
101105return 0 ;
102106 }
103107
@@ -117,6 +121,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
117121configureSocket (sock);
118122 _status =true ;
119123 }else {
124+ sock->close ();
120125 _status =false ;
121126 }
122127
@@ -148,6 +153,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
148153 }
149154
150155if (static_cast <TLSSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
156+ _status =false ;
151157return 0 ;
152158 }
153159
@@ -179,6 +185,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
179185configureSocket (sock);
180186 _status =true ;
181187 }else {
188+ sock->close ();
182189 _status =false ;
183190 }
184191
@@ -209,12 +216,14 @@ size_t arduino::MbedClient::write(uint8_t c) {
209216}
210217
211218size_t arduino::MbedClient::write (const uint8_t *buf,size_t size) {
212- if (sock ==nullptr )
219+ if (_status ==false )
213220return 0 ;
214221
222+ mutex->lock ();
215223 sock->set_timeout (_timeout);
216224int ret = sock->send (buf, size);
217225 sock->set_blocking (false );
226+ mutex->unlock ();
218227return ret >=0 ? ret :0 ;
219228}
220229
@@ -224,7 +233,7 @@ int arduino::MbedClient::available() {
224233}
225234
226235int arduino::MbedClient::read () {
227- if (sock ==nullptr )
236+ if (_status ==false )
228237return -1 ;
229238 mutex->lock ();
230239if (!available ()) {
@@ -238,7 +247,7 @@ int arduino::MbedClient::read() {
238247}
239248
240249int arduino::MbedClient::read (uint8_t *data,size_t len) {
241- if (sock ==nullptr )
250+ if (_status ==false )
242251return 0 ;
243252 mutex->lock ();
244253int avail =available ();