Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drastically speedup client.read() operation #439

Merged
merged 3 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions libraries/SocketWrapper/src/MbedClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void arduino::MbedClient::readSocket() {
do {
if (rxBuffer.availableForStore() == 0) {
yield();
delay(100);
continue;
}
mutex->lock();
Expand All @@ -34,7 +33,6 @@ void arduino::MbedClient::readSocket() {
}
if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0) {
yield();
delay(100);
mutex->unlock();
continue;
}
Expand Down Expand Up @@ -71,7 +69,7 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
}
mutex->lock();
if (reader_th == nullptr) {
reader_th = new rtos::Thread;
reader_th = new rtos::Thread(osPriorityNormal - 2);
reader_th->start(mbed::callback(this, &MbedClient::readSocket));
}
mutex->unlock();
Expand All @@ -80,6 +78,15 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
}

int arduino::MbedClient::connect(SocketAddress socketAddress) {

if (sock && reader_th) {
// trying to reuse a connection, let's call stop() to cleanup the state
char c;
if (sock->recv(&c, 1) < 0) {
stop();
}
}

if (sock == nullptr) {
sock = new TCPSocket();
_own_socket = true;
Expand Down Expand Up @@ -206,7 +213,7 @@ size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) {
int ret = NSAPI_ERROR_WOULD_BLOCK;
do {
ret = sock->send(buf, size);
} while (ret != size && connected());
} while ((ret != size && ret == NSAPI_ERROR_WOULD_BLOCK) && connected());
configureSocket(sock);
return size;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/SocketWrapper/src/MbedClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MbedClient : public arduino::Client {
int connectSSL(IPAddress ip, uint16_t port);
int connectSSL(const char* host, uint16_t port, bool disableSNI = false);
size_t write(uint8_t);
size_t write(const uint8_t* buf, size_t size);
size_t write(const uint8_t* buf, size_t size) override;
int available();
int read();
int read(uint8_t* buf, size_t size);
Expand Down