@@ -216,38 +216,14 @@ size_t AsyncClient::write(const char* data) {
216
216
return write (data, strlen (data));
217
217
}
218
218
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 ())
223
222
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;
248
224
}
249
225
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 ) {
251
227
if (!_pcb || size == 0 || data == NULL )
252
228
return 0 ;
253
229
size_t room = space ();
@@ -263,7 +239,7 @@ size_t AsyncClient::add(const char* data, size_t size) {
263
239
}
264
240
#endif
265
241
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 );
267
243
if (err != ERR_OK)
268
244
return 0 ;
269
245
return will_send;
@@ -274,8 +250,6 @@ bool AsyncClient::send(){
274
250
if (_pcb_secure)
275
251
return true ;
276
252
#endif
277
- if (!canSend ())
278
- return false ;
279
253
if (tcp_output (_pcb) == ERR_OK){
280
254
_pcb_busy = true ;
281
255
_pcb_sent_at = millis ();
@@ -375,26 +349,27 @@ void AsyncClient::_ssl_error(int8_t err){
375
349
376
350
int8_t AsyncClient::_sent (tcp_pcb* pcb, uint16_t len) {
377
351
_rx_last_packet = millis ();
378
- // ets_printf("ack : %u\n", len);
352
+ ASYNC_TCP_DEBUG ( " _sent : %u\n " , len);
379
353
_pcb_busy = false ;
380
354
if (_sent_cb)
381
355
_sent_cb (_sent_cb_arg, this , len, (millis () - _pcb_sent_at));
382
356
return ERR_OK;
383
357
}
384
358
385
359
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);
388
362
return _close ();
389
- } // else ets_printf("_recv: %d\n", pb->tot_len);
363
+ }
390
364
391
365
_rx_last_packet = millis ();
392
366
#if ASYNC_TCP_SSL_ENABLED
393
367
if (_pcb_secure){
368
+ ASYNC_TCP_DEBUG (" _recv: %d\n " , pb->tot_len );
394
369
int read_bytes = tcp_ssl_read (pcb, pb);
395
370
if (read_bytes < 0 ){
396
371
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);
398
373
_close ();
399
374
}
400
375
return read_bytes;
@@ -406,13 +381,13 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
406
381
// we should not ack before we assimilate the data
407
382
_ack_pcb = true ;
408
383
pbuf *b = pb;
384
+ ASYNC_TCP_DEBUG (" _recv: %d\n " , b->len );
409
385
if (_recv_cb)
410
386
_recv_cb (_recv_cb_arg, this , b->payload , b->len );
411
387
if (!_ack_pcb)
412
388
_rx_ack_len += b->len ;
413
389
else
414
390
tcp_recved (pcb, b->len );
415
- // pb = pbuf_dechain(b);
416
391
pb = b->next ;
417
392
b->next = NULL ;
418
393
pbuf_free (b);
@@ -697,7 +672,7 @@ size_t AsyncClient::space(){
697
672
if ((_pcb != NULL ) && (_pcb->state == 4 ) && _handshake_done){
698
673
uint16_t s = tcp_sndbuf (_pcb);
699
674
if (_pcb_secure){
700
- #if AXTLS_2_0_0_SNDBUF
675
+ #ifdef AXTLS_2_0_0_SNDBUF
701
676
return tcp_ssl_sndbuf (_pcb);
702
677
#else
703
678
if (s >= 128 ) // safe approach
@@ -910,13 +885,13 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
910
885
if (tcp_ssl_has_client () || _pending){
911
886
struct pending_pcb * new_item = (struct pending_pcb *)malloc (sizeof (struct pending_pcb ));
912
887
if (!new_item){
913
- // ets_printf ("### malloc new pending failed!\n");
888
+ ASYNC_TCP_DEBUG (" ### malloc new pending failed!\n " );
914
889
if (tcp_close (pcb) != ERR_OK){
915
890
tcp_abort (pcb);
916
891
}
917
892
return ERR_OK;
918
893
}
919
- // ets_printf ("### put to wait: %d\n", _clients_waiting);
894
+ ASYNC_TCP_DEBUG (" ### put to wait: %d\n " , _clients_waiting);
920
895
new_item->pcb = pcb;
921
896
new_item->pb = NULL ;
922
897
new_item->next = NULL ;
@@ -978,7 +953,7 @@ int8_t AsyncServer::_poll(tcp_pcb* pcb){
978
953
p->next = b->next ;
979
954
p = b;
980
955
}
981
- // ets_printf ("### remove from wait: %d\n", _clients_waiting);
956
+ ASYNC_TCP_DEBUG (" ### remove from wait: %d\n " , _clients_waiting);
982
957
AsyncClient *c = new AsyncClient (pcb, _ssl_ctx);
983
958
if (c){
984
959
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){
999
974
struct pending_pcb * p;
1000
975
1001
976
if (!pb){
1002
- // ets_printf ("### close from wait: %d\n", _clients_waiting);
977
+ ASYNC_TCP_DEBUG (" ### close from wait: %d\n " , _clients_waiting);
1003
978
p = _pending;
1004
979
if (p->pcb == pcb){
1005
980
_pending = _pending->next ;
@@ -1017,7 +992,7 @@ int8_t AsyncServer::_recv(struct tcp_pcb *pcb, struct pbuf *pb, int8_t err){
1017
992
tcp_close (pcb);
1018
993
tcp_abort (pcb);
1019
994
} 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);
1021
996
p = _pending;
1022
997
while (p && p->pcb != pcb)
1023
998
p = p->next ;
0 commit comments