Skip to content

Commit 0618b74

Browse files
committed
Use separate RX and TX buffer sizes in HTTP client
optimizes download by allowing up to 4K packets to be received
1 parent 4eb52c9 commit 0618b74

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

Diff for: libraries/HTTPClient/src/HTTPClient.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
740740
return returnError(HTTPC_ERROR_SEND_HEADER_FAILED);
741741
}
742742

743-
int buff_size = HTTP_TCP_BUFFER_SIZE;
743+
int buff_size = HTTP_TCP_TX_BUFFER_SIZE;
744744

745745
int len = size;
746746
int bytesWritten = 0;
@@ -749,8 +749,8 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
749749
len = -1;
750750
}
751751

752-
// if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
753-
if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) {
752+
// if possible create smaller buffer then HTTP_TCP_TX_BUFFER_SIZE
753+
if((len > 0) && (len < buff_size)) {
754754
buff_size = len;
755755
}
756756

@@ -843,7 +843,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
843843
}
844844

845845
} else {
846-
log_d("too less ram! need %d", HTTP_TCP_BUFFER_SIZE);
846+
log_d("too less ram! need %d", buff_size);
847847
return returnError(HTTPC_ERROR_TOO_LESS_RAM);
848848
}
849849

@@ -933,7 +933,7 @@ int HTTPClient::writeToStream(Stream * stream)
933933
// read size of chunk
934934
len = (uint32_t) strtol((const char *) chunkHeader.c_str(), NULL, 16);
935935
size += len;
936-
log_d(" read chunk len: %d", len);
936+
log_v(" read chunk len: %d", len);
937937

938938
// data left?
939939
if(len > 0) {
@@ -1364,12 +1364,12 @@ int HTTPClient::handleHeaderResponse()
13641364
*/
13651365
int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
13661366
{
1367-
int buff_size = HTTP_TCP_BUFFER_SIZE;
1367+
int buff_size = HTTP_TCP_RX_BUFFER_SIZE;
13681368
int len = size;
13691369
int bytesWritten = 0;
13701370

1371-
// if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
1372-
if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) {
1371+
// if possible create smaller buffer then HTTP_TCP_RX_BUFFER_SIZE
1372+
if((len > 0) && (len < buff_size)) {
13731373
buff_size = len;
13741374
}
13751375

@@ -1381,7 +1381,10 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
13811381
while(connected() && (len > 0 || len == -1)) {
13821382

13831383
// get available data size
1384-
size_t sizeAvailable = _client->available();
1384+
size_t sizeAvailable = buff_size;
1385+
if(len < 0){
1386+
sizeAvailable = _client->available();
1387+
}
13851388

13861389
if(sizeAvailable) {
13871390

@@ -1457,15 +1460,15 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
14571460

14581461
free(buff);
14591462

1460-
log_d("connection closed or file end (written: %d).", bytesWritten);
1463+
log_v("connection closed or file end (written: %d).", bytesWritten);
14611464

14621465
if((size > 0) && (size != bytesWritten)) {
14631466
log_d("bytesWritten %d and size %d mismatch!.", bytesWritten, size);
14641467
return HTTPC_ERROR_STREAM_WRITE;
14651468
}
14661469

14671470
} else {
1468-
log_w("too less ram! need %d", HTTP_TCP_BUFFER_SIZE);
1471+
log_w("too less ram! need %d", buff_size);
14691472
return HTTPC_ERROR_TOO_LESS_RAM;
14701473
}
14711474

Diff for: libraries/HTTPClient/src/HTTPClient.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
#define HTTPC_ERROR_READ_TIMEOUT (-11)
5656

5757
/// size for the stream handling
58-
#define HTTP_TCP_BUFFER_SIZE (1460)
58+
#define HTTP_TCP_RX_BUFFER_SIZE (4096)
59+
#define HTTP_TCP_TX_BUFFER_SIZE (1460)
5960

6061
/// HTTP codes see RFC7231
6162
typedef enum {

0 commit comments

Comments
 (0)