Skip to content

Commit 1c5389b

Browse files
author
Me No Dev
committed
some sending optimizations
1 parent 5bc486e commit 1c5389b

File tree

5 files changed

+28
-59
lines changed

5 files changed

+28
-59
lines changed

src/ESPAsyncTCP.cpp

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -216,38 +216,14 @@ size_t AsyncClient::write(const char* data) {
216216
return write(data, strlen(data));
217217
}
218218

219-
size_t AsyncClient::write(const char* data, size_t size) {
220-
if(!_pcb || size == 0 || data == NULL)
221-
return 0;
222-
if(!canSend())
219+
size_t AsyncClient::write(const char* data, size_t size, uint8_t apiflags) {
220+
size_t will_send = add(data, size, apiflags);
221+
if(!will_send || !send())
223222
return 0;
224-
#if ASYNC_TCP_SSL_ENABLED
225-
if(_pcb_secure){
226-
int sent = tcp_ssl_write(_pcb, (uint8_t*)data, size);
227-
if(sent >= 0)
228-
return sent;
229-
_close();
230-
return 0;
231-
}
232-
#endif
233-
size_t room = space();
234-
size_t will_send = (room < size) ? room : size;
235-
int8_t err = tcp_write(_pcb, data, will_send, 0);
236-
if(err != ERR_OK)
237-
return 0;
238-
err = tcp_output(_pcb);
239-
if(err != ERR_OK)
240-
return 0;
241-
_pcb_sent_at = millis();
242-
_pcb_busy = true;
243-
if(will_send < size){
244-
size_t left = size - will_send;
245-
return will_send + write(data+will_send, left);
246-
}
247-
return size;
223+
return will_send;
248224
}
249225

250-
size_t AsyncClient::add(const char* data, size_t size) {
226+
size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
251227
if(!_pcb || size == 0 || data == NULL)
252228
return 0;
253229
size_t room = space();
@@ -263,7 +239,7 @@ size_t AsyncClient::add(const char* data, size_t size) {
263239
}
264240
#endif
265241
size_t will_send = (room < size) ? room : size;
266-
int8_t err = tcp_write(_pcb, data, will_send, 0);
242+
int8_t err = tcp_write(_pcb, data, will_send, apiflags);
267243
if(err != ERR_OK)
268244
return 0;
269245
return will_send;
@@ -274,8 +250,6 @@ bool AsyncClient::send(){
274250
if(_pcb_secure)
275251
return true;
276252
#endif
277-
if(!canSend())
278-
return false;
279253
if(tcp_output(_pcb) == ERR_OK){
280254
_pcb_busy = true;
281255
_pcb_sent_at = millis();
@@ -375,26 +349,27 @@ void AsyncClient::_ssl_error(int8_t err){
375349

376350
int8_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) {
377351
_rx_last_packet = millis();
378-
//ets_printf("ack: %u\n", len);
352+
ASYNC_TCP_DEBUG("_sent: %u\n", len);
379353
_pcb_busy = false;
380354
if(_sent_cb)
381355
_sent_cb(_sent_cb_arg, this, len, (millis() - _pcb_sent_at));
382356
return ERR_OK;
383357
}
384358

385359
int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
386-
if(pb == 0){
387-
//ets_printf("_pb null! %d\n", err);
360+
if(pb == NULL){
361+
ASYNC_TCP_DEBUG("_recv: pb == NULL! Closing... %d\n", err);
388362
return _close();
389-
} //else ets_printf("_recv: %d\n", pb->tot_len);
363+
}
390364

391365
_rx_last_packet = millis();
392366
#if ASYNC_TCP_SSL_ENABLED
393367
if(_pcb_secure){
368+
ASYNC_TCP_DEBUG("_recv: %d\n", pb->tot_len);
394369
int read_bytes = tcp_ssl_read(pcb, pb);
395370
if(read_bytes < 0){
396371
if (read_bytes != SSL_CLOSE_NOTIFY) {
397-
ets_printf("_recv err: %d\n", read_bytes);
372+
ASYNC_TCP_DEBUG("_recv err: %d\n", read_bytes);
398373
_close();
399374
}
400375
return read_bytes;
@@ -406,13 +381,13 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
406381
//we should not ack before we assimilate the data
407382
_ack_pcb = true;
408383
pbuf *b = pb;
384+
ASYNC_TCP_DEBUG("_recv: %d\n", b->len);
409385
if(_recv_cb)
410386
_recv_cb(_recv_cb_arg, this, b->payload, b->len);
411387
if(!_ack_pcb)
412388
_rx_ack_len += b->len;
413389
else
414390
tcp_recved(pcb, b->len);
415-
//pb = pbuf_dechain(b);
416391
pb = b->next;
417392
b->next = NULL;
418393
pbuf_free(b);
@@ -697,7 +672,7 @@ size_t AsyncClient::space(){
697672
if((_pcb != NULL) && (_pcb->state == 4) && _handshake_done){
698673
uint16_t s = tcp_sndbuf(_pcb);
699674
if(_pcb_secure){
700-
#if AXTLS_2_0_0_SNDBUF
675+
#ifdef AXTLS_2_0_0_SNDBUF
701676
return tcp_ssl_sndbuf(_pcb);
702677
#else
703678
if(s >= 128) //safe approach
@@ -910,13 +885,13 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
910885
if(tcp_ssl_has_client() || _pending){
911886
struct pending_pcb * new_item = (struct pending_pcb*)malloc(sizeof(struct pending_pcb));
912887
if(!new_item){
913-
//ets_printf("### malloc new pending failed!\n");
888+
ASYNC_TCP_DEBUG("### malloc new pending failed!\n");
914889
if(tcp_close(pcb) != ERR_OK){
915890
tcp_abort(pcb);
916891
}
917892
return ERR_OK;
918893
}
919-
//ets_printf("### put to wait: %d\n", _clients_waiting);
894+
ASYNC_TCP_DEBUG("### put to wait: %d\n", _clients_waiting);
920895
new_item->pcb = pcb;
921896
new_item->pb = NULL;
922897
new_item->next = NULL;
@@ -978,7 +953,7 @@ int8_t AsyncServer::_poll(tcp_pcb* pcb){
978953
p->next = b->next;
979954
p = b;
980955
}
981-
//ets_printf("### remove from wait: %d\n", _clients_waiting);
956+
ASYNC_TCP_DEBUG("### remove from wait: %d\n", _clients_waiting);
982957
AsyncClient *c = new AsyncClient(pcb, _ssl_ctx);
983958
if(c){
984959
c->onConnect([this](void * arg, AsyncClient *c){
@@ -999,7 +974,7 @@ int8_t AsyncServer::_recv(struct tcp_pcb *pcb, struct pbuf *pb, int8_t err){
999974
struct pending_pcb * p;
1000975

1001976
if(!pb){
1002-
//ets_printf("### close from wait: %d\n", _clients_waiting);
977+
ASYNC_TCP_DEBUG("### close from wait: %d\n", _clients_waiting);
1003978
p = _pending;
1004979
if(p->pcb == pcb){
1005980
_pending = _pending->next;
@@ -1017,7 +992,7 @@ int8_t AsyncServer::_recv(struct tcp_pcb *pcb, struct pbuf *pb, int8_t err){
1017992
tcp_close(pcb);
1018993
tcp_abort(pcb);
1019994
} else {
1020-
//ets_printf("### wait _recv: %u %d\n", pb->tot_len, _clients_waiting);
995+
ASYNC_TCP_DEBUG("### wait _recv: %u %d\n", pb->tot_len, _clients_waiting);
1021996
p = _pending;
1022997
while(p && p->pcb != pcb)
1023998
p = p->next;

src/ESPAsyncTCP.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
class AsyncClient;
3030

3131
#define ASYNC_MAX_ACK_TIME 5000
32+
#define ASYNC_WRITE_FLAG_COPY 0x01 //will allocate new buffer to hold the data while sending (else will hold reference to the data given)
33+
#define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react.
3234

3335
typedef std::function<void(void*, AsyncClient*)> AcConnectHandler;
3436
typedef std::function<void(void*, AsyncClient*, size_t len, uint32_t time)> AcAckHandler;
@@ -132,7 +134,7 @@ class AsyncClient {
132134

133135
bool canSend();//ack is not pending
134136
size_t space();
135-
size_t add(const char* data, size_t size);//add for sending
137+
size_t add(const char* data, size_t size, uint8_t apiflags=0);//add for sending
136138
bool send();//send all data added with the method above
137139
size_t ack(size_t len); //ack data that you have not acked using the method below
138140
void ackLater(){ _ack_pcb = false; } //will not ack the current packet. Call from onData
@@ -142,7 +144,7 @@ class AsyncClient {
142144
#endif
143145

144146
size_t write(const char* data);
145-
size_t write(const char* data, size_t size); //only when canSend() == true
147+
size_t write(const char* data, size_t size, uint8_t apiflags=0); //only when canSend() == true
146148

147149
uint8_t state();
148150
bool connecting();

src/async_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define LIBRARIES_ESPASYNCTCP_SRC_ASYNC_CONFIG_H_
33

44
#define ASYNC_TCP_SSL_ENABLED 0
5-
6-
5+
#define ASYNC_TCP_DEBUG(...) //ets_printf(__VA_ARGS__)
6+
#define TCP_SSL_DEBUG(...) //ets_printf(__VA_ARGS__)
77

88
#endif /* LIBRARIES_ESPASYNCTCP_SRC_ASYNC_CONFIG_H_ */

src/tcp_axtls.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ int tcp_ssl_free(struct tcp_pcb *tcp) {
270270
return 0;
271271
}
272272

273-
#if AXTLS_2_0_0_SNDBUF
273+
#ifdef AXTLS_2_0_0_SNDBUF
274274
int tcp_ssl_sndbuf(struct tcp_pcb *tcp){
275275
int expected;
276276
int available;
@@ -314,7 +314,7 @@ int tcp_ssl_write(struct tcp_pcb *tcp, uint8_t *data, size_t len) {
314314
}
315315
tcp_ssl->last_wr = 0;
316316

317-
#if AXTLS_2_0_0_SNDBUF
317+
#ifdef AXTLS_2_0_0_SNDBUF
318318
int expected_len = ssl_calculate_write_length(tcp_ssl->ssl, len);
319319
int available_len = tcp_sndbuf(tcp);
320320
if(expected_len < 0 || expected_len > available_len){

src/tcp_axtls.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ extern "C" {
4242

4343
#include "include/ssl.h"
4444

45-
#ifndef AXTLS_2_0_0_SNDBUF
46-
#define AXTLS_2_0_0_SNDBUF 0
47-
#endif
48-
4945
#define ERR_TCP_SSL_INVALID_SSL -101
5046
#define ERR_TCP_SSL_INVALID_TCP -102
5147
#define ERR_TCP_SSL_INVALID_CLIENTFD -103
@@ -58,10 +54,6 @@ extern "C" {
5854
#define tcp_ssl_ssl_write(A, B, C) tcp_ssl_write(A, B, C)
5955
#define tcp_ssl_ssl_read(A, B) tcp_ssl_read(A, B)
6056

61-
#ifndef TCP_SSL_DEBUG
62-
#define TCP_SSL_DEBUG(...) //ets_printf(__VA_ARGS__)
63-
#endif
64-
6557
typedef void (* tcp_ssl_data_cb_t)(void *arg, struct tcp_pcb *tcp, uint8_t * data, size_t len);
6658
typedef void (* tcp_ssl_handshake_cb_t)(void *arg, struct tcp_pcb *tcp, SSL *ssl);
6759
typedef void (* tcp_ssl_error_cb_t)(void *arg, struct tcp_pcb *tcp, int8_t error);
@@ -78,7 +70,7 @@ int tcp_ssl_is_server(struct tcp_pcb *tcp);
7870
int tcp_ssl_free(struct tcp_pcb *tcp);
7971
int tcp_ssl_read(struct tcp_pcb *tcp, struct pbuf *p);
8072

81-
#if AXTLS_2_0_0_SNDBUF
73+
#ifdef AXTLS_2_0_0_SNDBUF
8274
int tcp_ssl_sndbuf(struct tcp_pcb *tcp);
8375
#endif
8476

0 commit comments

Comments
 (0)