@@ -82,8 +82,8 @@ static TaskHandle_t _async_service_task_handle = NULL;
82
82
83
83
SemaphoreHandle_t _slots_lock;
84
84
const int _number_of_closed_slots = CONFIG_LWIP_MAX_ACTIVE_TCP;
85
- static int _closed_slots[_number_of_closed_slots];
86
- static int _closed_index = []() {
85
+ static uint32_t _closed_slots[_number_of_closed_slots];
86
+ static uint32_t _closed_index = []() {
87
87
_slots_lock = xSemaphoreCreateBinary ();
88
88
xSemaphoreGive (_slots_lock);
89
89
for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
@@ -152,7 +152,10 @@ static bool _remove_events_with_arg(void * arg){
152
152
}
153
153
154
154
static void _handle_async_event (lwip_event_packet_t * e){
155
- if (e->event == LWIP_TCP_CLEAR){
155
+ if (e->arg == NULL ){
156
+ // do nothing when arg is NULL
157
+ // ets_printf("event arg == NULL: 0x%08x\n", e->recv.pcb);
158
+ } else if (e->event == LWIP_TCP_CLEAR){
156
159
_remove_events_with_arg (e->arg );
157
160
} else if (e->event == LWIP_TCP_RECV){
158
161
// ets_printf("-R: 0x%08x\n", e->recv.pcb);
@@ -585,17 +588,7 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
585
588
_pcb = pcb;
586
589
_closed_slot = -1 ;
587
590
if (_pcb){
588
- xSemaphoreTake (_slots_lock, portMAX_DELAY);
589
- int closed_slot_min_index = 0 ;
590
- for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
591
- if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0 ) {
592
- closed_slot_min_index = _closed_slots[i];
593
- _closed_slot = i;
594
- }
595
- }
596
- _closed_slots[_closed_slot] = 0 ;
597
- xSemaphoreGive (_slots_lock);
598
-
591
+ _allocate_closed_slot ();
599
592
_rx_last_packet = millis ();
600
593
tcp_arg (_pcb, this );
601
594
tcp_recv (_pcb, &_tcp_recv);
@@ -609,6 +602,7 @@ AsyncClient::~AsyncClient(){
609
602
if (_pcb) {
610
603
_close ();
611
604
}
605
+ _free_closed_slot ();
612
606
}
613
607
614
608
/*
@@ -734,7 +728,6 @@ bool AsyncClient::connect(const char* host, uint16_t port){
734
728
ip_addr_t addr;
735
729
736
730
if (!_start_async_task ()){
737
- Serial.println (" failed to start task" );
738
731
log_e (" failed to start task" );
739
732
return false ;
740
733
}
@@ -845,6 +838,29 @@ int8_t AsyncClient::_close(){
845
838
return err;
846
839
}
847
840
841
+ void AsyncClient::_allocate_closed_slot (){
842
+ xSemaphoreTake (_slots_lock, portMAX_DELAY);
843
+ uint32_t closed_slot_min_index = 0 ;
844
+ for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
845
+ if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0 ) {
846
+ closed_slot_min_index = _closed_slots[i];
847
+ _closed_slot = i;
848
+ }
849
+ }
850
+ if (_closed_slot != -1 ) {
851
+ _closed_slots[_closed_slot] = 0 ;
852
+ }
853
+ xSemaphoreGive (_slots_lock);
854
+ }
855
+
856
+ void AsyncClient::_free_closed_slot (){
857
+ if (_closed_slot != -1 ) {
858
+ _closed_slots[_closed_slot] = _closed_index;
859
+ _closed_slot = -1 ;
860
+ ++ _closed_index;
861
+ }
862
+ }
863
+
848
864
/*
849
865
* Private Callbacks
850
866
* */
@@ -867,10 +883,12 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
867
883
void AsyncClient::_error (int8_t err) {
868
884
if (_pcb){
869
885
tcp_arg (_pcb, NULL );
870
- tcp_sent (_pcb, NULL );
871
- tcp_recv (_pcb, NULL );
872
- tcp_err (_pcb, NULL );
873
- tcp_poll (_pcb, NULL , 0 );
886
+ if (_pcb->state == LISTEN) {
887
+ tcp_sent (_pcb, NULL );
888
+ tcp_recv (_pcb, NULL );
889
+ tcp_err (_pcb, NULL );
890
+ tcp_poll (_pcb, NULL , 0 );
891
+ }
874
892
_pcb = NULL ;
875
893
}
876
894
if (_error_cb) {
@@ -888,15 +906,16 @@ int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) {
888
906
return ERR_OK;
889
907
}
890
908
tcp_arg (_pcb, NULL );
891
- tcp_sent (_pcb, NULL );
892
- tcp_recv (_pcb, NULL );
893
- tcp_err (_pcb, NULL );
894
- tcp_poll (_pcb, NULL , 0 );
909
+ if (_pcb->state == LISTEN) {
910
+ tcp_sent (_pcb, NULL );
911
+ tcp_recv (_pcb, NULL );
912
+ tcp_err (_pcb, NULL );
913
+ tcp_poll (_pcb, NULL , 0 );
914
+ }
895
915
if (tcp_close (_pcb) != ERR_OK) {
896
916
tcp_abort (_pcb);
897
917
}
898
- _closed_slots[_closed_slot] = _closed_index;
899
- ++ _closed_index;
918
+ _free_closed_slot ();
900
919
_pcb = NULL ;
901
920
return ERR_OK;
902
921
}
0 commit comments