@@ -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 ;
@@ -92,6 +94,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
92
94
}
93
95
94
96
if (static_cast <TCPSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
97
+ _status = false ;
95
98
return 0 ;
96
99
}
97
100
@@ -111,6 +114,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
111
114
configureSocket (sock);
112
115
_status = true ;
113
116
} else {
117
+ sock->close ();
114
118
_status = false ;
115
119
}
116
120
@@ -145,6 +149,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
145
149
}
146
150
147
151
if (static_cast <TLSSocket *>(sock)->open (getNetwork ()) != NSAPI_ERROR_OK) {
152
+ _status = false ;
148
153
return 0 ;
149
154
}
150
155
@@ -176,6 +181,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
176
181
configureSocket (sock);
177
182
_status = true ;
178
183
} else {
184
+ sock->close ();
179
185
_status = false ;
180
186
}
181
187
@@ -221,8 +227,9 @@ int arduino::MbedClient::available() {
221
227
}
222
228
223
229
int arduino::MbedClient::read () {
224
- if (sock == nullptr )
230
+ if (mutex == nullptr ) {
225
231
return -1 ;
232
+ }
226
233
mutex->lock ();
227
234
if (!available ()) {
228
235
mutex->unlock ();
@@ -235,8 +242,9 @@ int arduino::MbedClient::read() {
235
242
}
236
243
237
244
int arduino::MbedClient::read (uint8_t *data, size_t len) {
238
- if (sock == nullptr )
245
+ if (mutex == nullptr ) {
239
246
return 0 ;
247
+ }
240
248
mutex->lock ();
241
249
int avail = available ();
242
250
@@ -258,7 +266,14 @@ int arduino::MbedClient::read(uint8_t *data, size_t len) {
258
266
}
259
267
260
268
int 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;
262
277
}
263
278
264
279
void arduino::MbedClient::flush () {
0 commit comments