Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit a627ef4

Browse files
author
Me No Dev
committed
enable no-delay for ssl and optimize sending
1 parent dedc3c4 commit a627ef4

File tree

3 files changed

+79
-25
lines changed

3 files changed

+79
-25
lines changed

src/ESPAsyncTCP.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -640,14 +640,15 @@ void AsyncClient::onPoll(AcConnectHandler cb, void* arg){
640640

641641
size_t AsyncClient::space(){
642642
if((_pcb != NULL) && (_pcb->state == 4) && _handshake_done){
643-
uint16_t s = _pcb->snd_buf;
643+
uint16_t s = tcp_sndbuf(_pcb);
644644
if(_pcb_secure){
645-
if(s > 1400)
646-
return 1400;
647-
if(s >= 53){
648-
return s - 53;
649-
}
645+
#if AXTLS_2_0_0_SNDBUF
646+
return tcp_ssl_sndbuf(_pcb);
647+
#else
648+
if(s >= 128) //safe approach
649+
return s - 128;
650650
return 0;
651+
#endif
651652
}
652653
return s;
653654
}
@@ -826,7 +827,7 @@ uint8_t AsyncServer::status(){
826827

827828
int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
828829
if(_connect_cb){
829-
if (_noDelay)
830+
if (_noDelay || _ssl_ctx)
830831
tcp_nagle_disable(pcb);
831832
else
832833
tcp_nagle_enable(pcb);

src/tcp_axtls.c

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
#include <stdarg.h>
3232
#include <tcp_axtls.h>
3333

34+
uint8_t * default_private_key = NULL;
35+
uint16_t default_private_key_len = 0;
36+
37+
uint8_t * default_certificate = NULL;
38+
uint16_t default_certificate_len = 0;
3439

3540
SSL_CTX * tcp_ssl_new_server_ctx(const char *cert, const char *private_key_file, const char *password){
3641
uint32_t options = SSL_CONNECT_IN_PARTS;
@@ -254,18 +259,62 @@ int tcp_ssl_free(struct tcp_pcb *tcp) {
254259
return 0;
255260
}
256261

262+
#if AXTLS_2_0_0_SNDBUF
263+
int tcp_ssl_sndbuf(struct tcp_pcb *tcp){
264+
int expected;
265+
int available;
266+
int result = -1;
267+
268+
if(tcp == NULL) {
269+
return result;
270+
}
271+
tcp_ssl_t * tcp_ssl = tcp_ssl_get(tcp);
272+
if(!tcp_ssl){
273+
TCP_SSL_DEBUG("tcp_ssl_sndbuf: tcp_ssl is NULL\n");
274+
return result;
275+
}
276+
available = tcp_sndbuf(tcp);
277+
if(!available){
278+
TCP_SSL_DEBUG("tcp_ssl_sndbuf: tcp_sndbuf is zero\n");
279+
return 0;
280+
}
281+
result = available;
282+
while((expected = ssl_calculate_write_length(tcp_ssl->ssl, result)) > available){
283+
result -= (expected - available) + 4;
284+
}
285+
286+
if(expected > 0){
287+
//TCP_SSL_DEBUG("tcp_ssl_sndbuf: tcp_sndbuf is %d from %d\n", result, available);
288+
return result;
289+
}
290+
291+
return 0;
292+
}
293+
#endif
294+
257295
int tcp_ssl_write(struct tcp_pcb *tcp, uint8_t *data, size_t len) {
258296
if(tcp == NULL) {
259297
return -1;
260298
}
261-
tcp_ssl_t * axl = tcp_ssl_get(tcp);
262-
if(!axl){
299+
tcp_ssl_t * tcp_ssl = tcp_ssl_get(tcp);
300+
if(!tcp_ssl){
263301
TCP_SSL_DEBUG("tcp_ssl_write: tcp_ssl is NULL\n");
264302
return 0;
265303
}
266-
axl->last_wr = 0;
304+
tcp_ssl->last_wr = 0;
305+
306+
#if AXTLS_2_0_0_SNDBUF
307+
int expected_len = ssl_calculate_write_length(tcp_ssl->ssl, len);
308+
int available_len = tcp_sndbuf(tcp);
309+
if(expected_len < 0 || expected_len > available_len){
310+
TCP_SSL_DEBUG("tcp_ssl_write: data will not fit! %u < %d(%u)\r\n", available_len, expected_len, len);
311+
return -1;
312+
}
313+
#endif
314+
315+
int rc = ssl_write(tcp_ssl->ssl, data, len);
267316

268-
int rc = ssl_write(axl->ssl, data, len);
317+
//TCP_SSL_DEBUG("tcp_ssl_write: %u -> %d (%d)\r\n", len, tcp_ssl->last_wr, rc);
269318

270319
if (rc < 0){
271320
if(rc != SSL_CLOSE_NOTIFY) {
@@ -274,9 +323,7 @@ int tcp_ssl_write(struct tcp_pcb *tcp, uint8_t *data, size_t len) {
274323
return rc;
275324
}
276325

277-
//TCP_SSL_DEBUG("tcp_ssl_write: %u -> %d\r\n", len, axl->last_wr);
278-
279-
return axl->last_wr;
326+
return tcp_ssl->last_wr;
280327
}
281328

282329
/**
@@ -437,20 +484,19 @@ int ax_port_write(int fd, uint8_t *data, uint16_t len) {
437484
fd_data = tcp_ssl_get_by_fd(fd);
438485
if(fd_data == NULL) {
439486
//TCP_SSL_DEBUG("ax_port_write: tcp_ssl[%d] is NULL\n", fd);
440-
return ERR_TCP_SSL_INVALID_CLIENTFD;
487+
return ERR_MEM;
441488
}
442489

443-
if (fd_data->tcp == NULL || data == NULL || len == 0) {
490+
if (data == NULL || len == 0) {
444491
return 0;
445492
}
446493

447494
if (tcp_sndbuf(fd_data->tcp) < len) {
448495
tcp_len = tcp_sndbuf(fd_data->tcp);
449496
if(tcp_len == 0) {
450497
TCP_SSL_DEBUG("ax_port_write: tcp_sndbuf is zero: %d\n", len);
451-
return -1;
498+
return ERR_MEM;
452499
}
453-
454500
} else {
455501
tcp_len = len;
456502
}
@@ -460,21 +506,19 @@ int ax_port_write(int fd, uint8_t *data, uint16_t len) {
460506
}
461507

462508
err = tcp_write(fd_data->tcp, data, tcp_len, TCP_WRITE_FLAG_COPY);
463-
if(err < SSL_OK) {
509+
if(err < ERR_OK) {
464510
if (err == ERR_MEM) {
465511
TCP_SSL_DEBUG("ax_port_write: No memory %d (%d)\n", tcp_len, len);
466512
return err;
467513
}
468514
TCP_SSL_DEBUG("ax_port_write: tcp_write error: %d\n", err);
469-
}
470-
471-
472-
if (err == ERR_OK) {
473-
//TCP_SSL_DEBUG("ax_port_write: tcp_output length %d / %d\n", tcp_len, len);
515+
return err;
516+
} else if (err == ERR_OK) {
517+
//TCP_SSL_DEBUG("ax_port_write: tcp_output: %d / %d\n", tcp_len, len);
474518
err = tcp_output(fd_data->tcp);
475519
if(err != ERR_OK) {
476520
TCP_SSL_DEBUG("ax_port_write: tcp_output err: %d\n", err);
477-
return 0;
521+
return err;
478522
}
479523
}
480524

src/tcp_axtls.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ extern "C" {
3838

3939
#include "include/ssl.h"
4040

41+
#ifndef AXTLS_2_0_0_SNDBUF
42+
#define AXTLS_2_0_0_SNDBUF 0
43+
#endif
44+
4145
#define ERR_TCP_SSL_INVALID_SSL -101
4246
#define ERR_TCP_SSL_INVALID_TCP -102
4347
#define ERR_TCP_SSL_INVALID_CLIENTFD -103
@@ -67,6 +71,11 @@ int tcp_ssl_is_server(struct tcp_pcb *tcp);
6771

6872
int tcp_ssl_free(struct tcp_pcb *tcp);
6973
int tcp_ssl_read(struct tcp_pcb *tcp, struct pbuf *p);
74+
75+
#if AXTLS_2_0_0_SNDBUF
76+
int tcp_ssl_sndbuf(struct tcp_pcb *tcp);
77+
#endif
78+
7079
int tcp_ssl_write(struct tcp_pcb *tcp, uint8_t *data, size_t len);
7180

7281
void tcp_ssl_file(tcp_ssl_file_cb_t cb, void * arg);

0 commit comments

Comments
 (0)