@@ -82,8 +82,8 @@ static TaskHandle_t _async_service_task_handle = NULL;
8282
8383SemaphoreHandle_t _slots_lock;
8484const 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 = []() {
8787 _slots_lock = xSemaphoreCreateBinary ();
8888 xSemaphoreGive (_slots_lock);
8989 for (int i = 0 ; i < _number_of_closed_slots; ++ i) {
@@ -565,17 +565,7 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
565565 _pcb = pcb;
566566 _closed_slot = -1 ;
567567 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 ();
579569 _rx_last_packet = millis ();
580570 tcp_arg (_pcb, this );
581571 tcp_recv (_pcb, &_tcp_recv);
@@ -589,6 +579,7 @@ AsyncClient::~AsyncClient(){
589579 if (_pcb) {
590580 _close ();
591581 }
582+ _free_closed_slot ();
592583}
593584
594585/*
@@ -714,7 +705,6 @@ bool AsyncClient::connect(const char* host, uint16_t port){
714705 ip_addr_t addr;
715706
716707 if (!_start_async_task ()){
717- Serial.println (" failed to start task" );
718708 log_e (" failed to start task" );
719709 return false ;
720710 }
@@ -825,6 +815,29 @@ int8_t AsyncClient::_close(){
825815 return err;
826816}
827817
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+
828841/*
829842 * Private Callbacks
830843 * */
@@ -879,8 +892,7 @@ int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) {
879892 if (tcp_close (_pcb) != ERR_OK) {
880893 tcp_abort (_pcb);
881894 }
882- _closed_slots[_closed_slot] = _closed_index;
883- ++ _closed_index;
895+ _free_closed_slot ();
884896 _pcb = NULL ;
885897 return ERR_OK;
886898}
0 commit comments