@@ -575,11 +575,10 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
575
575
, _pb_cb_arg (0 )
576
576
, _timeout_cb (0 )
577
577
, _timeout_cb_arg (0 )
578
- , _pcb_busy (0 )
579
- , _pcb_sent_at (0 )
580
578
, _ack_pcb (true )
581
- , _rx_last_packet (0 )
582
- , _rx_since_timeout (0 )
579
+ , _tx_last_packet (0 )
580
+ , _rx_timeout (0 )
581
+ , _rx_last_ack (0 )
583
582
, _ack_timeout (ASYNC_MAX_ACK_TIME)
584
583
, _connect_port (0 )
585
584
, prev (NULL )
@@ -783,14 +782,12 @@ size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
783
782
}
784
783
785
784
bool AsyncClient::send (){
786
- auto pcb_sent_at_backup = _pcb_sent_at;
787
- _pcb_sent_at = millis ();
788
- _pcb_busy++;
785
+ auto backup = _tx_last_packet;
786
+ _tx_last_packet = millis ();
789
787
if (_tcp_output (_pcb, _closed_slot) == ERR_OK) {
790
788
return true ;
791
789
}
792
- _pcb_sent_at = pcb_sent_at_backup;
793
- _pcb_busy--;
790
+ _tx_last_packet = backup;
794
791
return false ;
795
792
}
796
793
@@ -870,7 +867,6 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
870
867
_pcb = reinterpret_cast <tcp_pcb*>(pcb);
871
868
if (_pcb){
872
869
_rx_last_packet = millis ();
873
- _pcb_busy = 0 ;
874
870
// tcp_recv(_pcb, &_tcp_recv);
875
871
// tcp_sent(_pcb, &_tcp_sent);
876
872
// tcp_poll(_pcb, &_tcp_poll, 1);
@@ -932,10 +928,10 @@ int8_t AsyncClient::_fin(tcp_pcb* pcb, int8_t err) {
932
928
933
929
int8_t AsyncClient::_sent (tcp_pcb* pcb, uint16_t len) {
934
930
_rx_last_packet = millis ();
931
+ _rx_last_ack = millis ();
935
932
// log_i("%u", len);
936
- _pcb_busy--;
937
933
if (_sent_cb) {
938
- _sent_cb (_sent_cb_arg, this , len, (millis () - _pcb_sent_at ));
934
+ _sent_cb (_sent_cb_arg, this , len, (millis () - _tx_last_packet ));
939
935
}
940
936
return ERR_OK;
941
937
}
@@ -978,15 +974,18 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
978
974
uint32_t now = millis ();
979
975
980
976
// ACK Timeout
981
- if (_pcb_busy > 0 && _ack_timeout && (now - _pcb_sent_at) >= _ack_timeout){
982
- _pcb_busy = 0 ;
983
- log_w (" ack timeout %d" , pcb->state );
984
- if (_timeout_cb)
985
- _timeout_cb (_timeout_cb_arg, this , (now - _pcb_sent_at));
986
- return ERR_OK;
977
+ if (_ack_timeout){
978
+ const uint32_t one_day = 86400000 ;
979
+ bool last_tx_is_after_last_ack = (_rx_last_ack - _tx_last_packet + one_day) < one_day;
980
+ if (last_tx_is_after_last_ack && (now - _tx_last_packet) >= _ack_timeout) {
981
+ log_w (" ack timeout %d" , pcb->state );
982
+ if (_timeout_cb)
983
+ _timeout_cb (_timeout_cb_arg, this , (now - _tx_last_packet));
984
+ return ERR_OK;
985
+ }
987
986
}
988
987
// RX Timeout
989
- if (_rx_since_timeout && (now - _rx_last_packet) >= (_rx_since_timeout * 1000 )){
988
+ if (_rx_timeout && (now - _rx_last_packet) >= (_rx_timeout * 1000 )) {
990
989
log_w (" rx timeout %d" , pcb->state );
991
990
_close ();
992
991
return ERR_OK;
@@ -1045,11 +1044,11 @@ size_t AsyncClient::write(const char* data, size_t size, uint8_t apiflags) {
1045
1044
}
1046
1045
1047
1046
void AsyncClient::setRxTimeout (uint32_t timeout){
1048
- _rx_since_timeout = timeout;
1047
+ _rx_timeout = timeout;
1049
1048
}
1050
1049
1051
1050
uint32_t AsyncClient::getRxTimeout (){
1052
- return _rx_since_timeout ;
1051
+ return _rx_timeout ;
1053
1052
}
1054
1053
1055
1054
uint32_t AsyncClient::getAckTimeout (){
0 commit comments