Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a40c63f

Browse files
SocketWrapper MbedClient debugging readSocket
1 parent 2ece915 commit a40c63f

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

‎libraries/SocketWrapper/src/MbedClient.cpp‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,30 @@ void arduino::MbedClient::readSocket() {
2222
int ret = NSAPI_ERROR_WOULD_BLOCK;
2323
do {
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();
2731
yield();
2832
continue;
29-
} else if (sock == nullptr) {
30-
goto cleanup;
3133
}
3234
ret = sock->recv(data, rxBuffer.availableForStore());
3335
if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
36+
mutex->unlock();
3437
goto cleanup;
3538
}
3639
if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0) {
37-
yield();
3840
mutex->unlock();
39-
continue;
41+
break;
4042
}
4143
for (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
}
4850
cleanup:
4951
_status = false;
@@ -98,6 +100,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
98100
}
99101

100102
if (static_cast<TCPSocket *>(sock)->open(getNetwork()) != NSAPI_ERROR_OK) {
103+
_status = false;
101104
return 0;
102105
}
103106

@@ -117,6 +120,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
117120
configureSocket(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

150154
if (static_cast<TLSSocket *>(sock)->open(getNetwork()) != NSAPI_ERROR_OK) {
155+
_status = false;
151156
return 0;
152157
}
153158

@@ -179,6 +184,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
179184
configureSocket(sock);
180185
_status = true;
181186
} else {
187+
sock->close();
182188
_status = false;
183189
}
184190

@@ -209,12 +215,14 @@ size_t arduino::MbedClient::write(uint8_t c) {
209215
}
210216

211217
size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) {
212-
if (sock == nullptr)
218+
if (mutex == nullptr) {
213219
return 0;
214-
220+
}
221+
mutex->lock();
215222
sock->set_timeout(_timeout);
216223
int ret = sock->send(buf, size);
217224
sock->set_blocking(false);
225+
mutex->unlock();
218226
return ret >= 0 ? ret : 0;
219227
}
220228

@@ -224,8 +232,9 @@ int arduino::MbedClient::available() {
224232
}
225233

226234
int arduino::MbedClient::read() {
227-
if (sock == nullptr)
235+
if (mutex == nullptr) {
228236
return -1;
237+
}
229238
mutex->lock();
230239
if (!available()) {
231240
mutex->unlock();
@@ -238,8 +247,9 @@ int arduino::MbedClient::read() {
238247
}
239248

240249
int arduino::MbedClient::read(uint8_t *data, size_t len) {
241-
if (sock == nullptr)
250+
if (mutex == nullptr) {
242251
return 0;
252+
}
243253
mutex->lock();
244254
int avail = available();
245255

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /