Skip to content

Commit acd815d

Browse files
authored
Merge pull request #439 from facchinm/increase_client_read
Drastically speedup client.read() operation
2 parents a15e027 + d30df6d commit acd815d

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

libraries/SocketWrapper/src/MbedClient.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ void arduino::MbedClient::readSocket() {
2121
do {
2222
if (rxBuffer.availableForStore() == 0) {
2323
yield();
24-
delay(100);
2524
continue;
2625
}
2726
mutex->lock();
@@ -34,7 +33,6 @@ void arduino::MbedClient::readSocket() {
3433
}
3534
if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0) {
3635
yield();
37-
delay(100);
3836
mutex->unlock();
3937
continue;
4038
}
@@ -71,7 +69,7 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
7169
}
7270
mutex->lock();
7371
if (reader_th == nullptr) {
74-
reader_th = new rtos::Thread;
72+
reader_th = new rtos::Thread(osPriorityNormal - 2);
7573
reader_th->start(mbed::callback(this, &MbedClient::readSocket));
7674
}
7775
mutex->unlock();
@@ -80,6 +78,15 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
8078
}
8179

8280
int arduino::MbedClient::connect(SocketAddress socketAddress) {
81+
82+
if (sock && reader_th) {
83+
// trying to reuse a connection, let's call stop() to cleanup the state
84+
char c;
85+
if (sock->recv(&c, 1) < 0) {
86+
stop();
87+
}
88+
}
89+
8390
if (sock == nullptr) {
8491
sock = new TCPSocket();
8592
_own_socket = true;
@@ -206,7 +213,7 @@ size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) {
206213
int ret = NSAPI_ERROR_WOULD_BLOCK;
207214
do {
208215
ret = sock->send(buf, size);
209-
} while (ret != size && connected());
216+
} while ((ret != size && ret == NSAPI_ERROR_WOULD_BLOCK) && connected());
210217
configureSocket(sock);
211218
return size;
212219
}

libraries/SocketWrapper/src/MbedClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class MbedClient : public arduino::Client {
7373
int connectSSL(IPAddress ip, uint16_t port);
7474
int connectSSL(const char* host, uint16_t port, bool disableSNI = false);
7575
size_t write(uint8_t);
76-
size_t write(const uint8_t* buf, size_t size);
76+
size_t write(const uint8_t* buf, size_t size) override;
7777
int available();
7878
int read();
7979
int read(uint8_t* buf, size_t size);

0 commit comments

Comments
 (0)