@@ -22,28 +22,30 @@ void arduino::MbedClient::readSocket() {
22
22
int ret = NSAPI_ERROR_WOULD_BLOCK;
23
23
do {
24
24
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 ) {
26
30
mutex->unlock ();
27
31
yield ();
28
32
continue ;
29
- } else if (sock == nullptr ) {
30
- goto cleanup;
31
33
}
32
34
ret = sock->recv (data, rxBuffer.availableForStore ());
33
35
if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
36
+ mutex->unlock ();
34
37
goto cleanup;
35
38
}
36
39
if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0 ) {
37
- yield ();
38
40
mutex->unlock ();
39
- continue ;
41
+ break ;
40
42
}
41
43
for (int i = 0 ; i < ret; i++) {
42
44
rxBuffer.store_char (data[i]);
43
45
}
44
46
mutex->unlock ();
45
47
_status = true ;
46
- } while (ret == NSAPI_ERROR_WOULD_BLOCK || ret > 0 );
48
+ } while (true );
47
49
}
48
50
cleanup:
49
51
_status = false ;
@@ -98,6 +100,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
98
100
}
99
101
100
102
if (static_cast <TCPSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
103
+ _status = false ;
101
104
return 0 ;
102
105
}
103
106
@@ -117,6 +120,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
117
120
configureSocket (sock);
118
121
_status = true ;
119
122
} else {
123
+ sock->close ();
120
124
_status = false ;
121
125
}
122
126
@@ -148,6 +152,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
148
152
}
149
153
150
154
if (static_cast <TLSSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
155
+ _status = false ;
151
156
return 0 ;
152
157
}
153
158
@@ -179,6 +184,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
179
184
configureSocket (sock);
180
185
_status = true ;
181
186
} else {
187
+ sock->close ();
182
188
_status = false ;
183
189
}
184
190
@@ -211,7 +217,7 @@ size_t arduino::MbedClient::write(uint8_t c) {
211
217
size_t arduino::MbedClient::write (const uint8_t *buf, size_t size) {
212
218
if (sock == nullptr )
213
219
return 0 ;
214
-
220
+
215
221
sock->set_timeout (_timeout);
216
222
int ret = sock->send (buf, size);
217
223
sock->set_blocking (false );
@@ -224,8 +230,9 @@ int arduino::MbedClient::available() {
224
230
}
225
231
226
232
int arduino::MbedClient::read () {
227
- if (sock == nullptr )
233
+ if (mutex == nullptr ) {
228
234
return -1 ;
235
+ }
229
236
mutex->lock ();
230
237
if (!available ()) {
231
238
mutex->unlock ();
@@ -238,8 +245,9 @@ int arduino::MbedClient::read() {
238
245
}
239
246
240
247
int arduino::MbedClient::read (uint8_t *data, size_t len) {
241
- if (sock == nullptr )
248
+ if (mutex == nullptr ) {
242
249
return 0 ;
250
+ }
243
251
mutex->lock ();
244
252
int avail = available ();
245
253
@@ -261,7 +269,14 @@ int arduino::MbedClient::read(uint8_t *data, size_t len) {
261
269
}
262
270
263
271
int 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;
265
280
}
266
281
267
282
void arduino::MbedClient::flush () {
0 commit comments