@@ -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
@@ -209,12 +215,13 @@ size_t arduino::MbedClient::write(uint8_t c) {
209215}
210216
211217size_t arduino::MbedClient::write (const uint8_t *buf,size_t size) {
212- if (sock ==nullptr )
218+ if (mutex ==nullptr )
213219return 0 ;
214-
220+ mutex-> lock ();
215221 sock->set_timeout (_timeout);
216222int ret = sock->send (buf, size);
217223 sock->set_blocking (false );
224+ mutex->unlock ();
218225return ret >=0 ? ret :0 ;
219226}
220227
@@ -224,7 +231,7 @@ int arduino::MbedClient::available() {
224231}
225232
226233int arduino::MbedClient::read () {
227- if (sock ==nullptr )
234+ if (mutex ==nullptr )
228235return -1 ;
229236 mutex->lock ();
230237if (!available ()) {
@@ -238,7 +245,7 @@ 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 ;
243250 mutex->lock ();
244251int avail =available ();