Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions libraries/NetworkClientSecure/src/NetworkClientSecure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ size_t NetworkClientSecure::write(const uint8_t *buf, size_t size) {
return 0;
}

if (size == 0) {
return 0;
}

if (_stillinPlainStart) {
return send_net_data(sslclient.get(), buf, size);
}
Expand Down
30 changes: 23 additions & 7 deletions libraries/NetworkClientSecure/src/ssl_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,25 +409,41 @@ int data_to_read(sslclient_context *ssl_client) {
}

int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, size_t len) {
unsigned long write_start_time = millis();
int ret = -1;
if (len == 0) {
return 0; // Skipping zero-length write
}

const size_t kChunk = 4096;
unsigned long last_progress = millis(); // Timeout since last progress
size_t sent = 0;

while (sent < len) {
size_t to_send = len - sent;
if (to_send > kChunk) {
to_send = kChunk;
}

while ((ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data, len)) <= 0) {
if ((millis() - write_start_time) > ssl_client->socket_timeout) {
int ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data + sent, to_send);
if (ret > 0) {
sent += ret;
last_progress = millis(); // refresh timeout window
continue;
}

if ((millis() - last_progress) > ssl_client->socket_timeout) {
log_v("SSL write timed out.");
return -1;
}

if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret < 0) {
log_v("Handling error %d", ret); //for low level debug
log_v("Handling error %d", ret);
return handle_error(ret);
}

//wait for space to become available
vTaskDelay(2);
}

return ret;
return (int)sent;
}

// Some protocols, such as SMTP, XMPP, MySQL/Posgress and various others
Expand Down
Loading