@@ -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) {
@@ -565,17 +565,7 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
565
565
_pcb = pcb;
566
566
_closed_slot = -1 ;
567
567
if (_pcb){
568
- xSemaphoreTake (_slots_lock, portMAX_DELAY);
569
- int closed_slot_min_index = 0 ;
570
- for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
571
- if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0 ) {
572
- closed_slot_min_index = _closed_slots[i];
573
- _closed_slot = i;
574
- }
575
- }
576
- _closed_slots[_closed_slot] = 0 ;
577
- xSemaphoreGive (_slots_lock);
578
-
568
+ _allocate_closed_slot ();
579
569
_rx_last_packet = millis ();
580
570
tcp_arg (_pcb, this );
581
571
tcp_recv (_pcb, &_tcp_recv);
@@ -589,6 +579,7 @@ AsyncClient::~AsyncClient(){
589
579
if (_pcb) {
590
580
_close ();
591
581
}
582
+ _free_closed_slot ();
592
583
}
593
584
594
585
/*
@@ -714,7 +705,6 @@ bool AsyncClient::connect(const char* host, uint16_t port){
714
705
ip_addr_t addr;
715
706
716
707
if (!_start_async_task ()){
717
- Serial.println (" failed to start task" );
718
708
log_e (" failed to start task" );
719
709
return false ;
720
710
}
@@ -825,6 +815,29 @@ int8_t AsyncClient::_close(){
825
815
return err;
826
816
}
827
817
818
+ void AsyncClient::_allocate_closed_slot (){
819
+ xSemaphoreTake (_slots_lock, portMAX_DELAY);
820
+ uint32_t closed_slot_min_index = 0 ;
821
+ for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
822
+ if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0 ) {
823
+ closed_slot_min_index = _closed_slots[i];
824
+ _closed_slot = i;
825
+ }
826
+ }
827
+ if (_closed_slot != -1 ) {
828
+ _closed_slots[_closed_slot] = 0 ;
829
+ }
830
+ xSemaphoreGive (_slots_lock);
831
+ }
832
+
833
+ void AsyncClient::_free_closed_slot (){
834
+ if (_closed_slot != -1 ) {
835
+ _closed_slots[_closed_slot] = _closed_index;
836
+ _closed_slot = -1 ;
837
+ ++ _closed_index;
838
+ }
839
+ }
840
+
828
841
/*
829
842
* Private Callbacks
830
843
* */
@@ -879,8 +892,7 @@ int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) {
879
892
if (tcp_close (_pcb) != ERR_OK) {
880
893
tcp_abort (_pcb);
881
894
}
882
- _closed_slots[_closed_slot] = _closed_index;
883
- ++ _closed_index;
895
+ _free_closed_slot ();
884
896
_pcb = NULL ;
885
897
return ERR_OK;
886
898
}
0 commit comments