Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit d2fec0c

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents c18b7ca + ca8ac5f commit d2fec0c

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

src/AsyncTCP.cpp

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ static TaskHandle_t _async_service_task_handle = NULL;
8282

8383
SemaphoreHandle_t _slots_lock;
8484
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 = []() {
8787
_slots_lock = xSemaphoreCreateBinary();
8888
xSemaphoreGive(_slots_lock);
8989
for (int i = 0; i < _number_of_closed_slots; ++ i) {
@@ -152,7 +152,10 @@ static bool _remove_events_with_arg(void * arg){
152152
}
153153

154154
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){
156159
_remove_events_with_arg(e->arg);
157160
} else if(e->event == LWIP_TCP_RECV){
158161
//ets_printf("-R: 0x%08x\n", e->recv.pcb);
@@ -585,17 +588,7 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
585588
_pcb = pcb;
586589
_closed_slot = -1;
587590
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();
599592
_rx_last_packet = millis();
600593
tcp_arg(_pcb, this);
601594
tcp_recv(_pcb, &_tcp_recv);
@@ -609,6 +602,7 @@ AsyncClient::~AsyncClient(){
609602
if(_pcb) {
610603
_close();
611604
}
605+
_free_closed_slot();
612606
}
613607

614608
/*
@@ -734,7 +728,6 @@ bool AsyncClient::connect(const char* host, uint16_t port){
734728
ip_addr_t addr;
735729

736730
if(!_start_async_task()){
737-
Serial.println("failed to start task");
738731
log_e("failed to start task");
739732
return false;
740733
}
@@ -845,6 +838,29 @@ int8_t AsyncClient::_close(){
845838
return err;
846839
}
847840

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+
848864
/*
849865
* Private Callbacks
850866
* */
@@ -867,10 +883,12 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
867883
void AsyncClient::_error(int8_t err) {
868884
if(_pcb){
869885
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+
}
874892
_pcb = NULL;
875893
}
876894
if(_error_cb) {
@@ -888,15 +906,16 @@ int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) {
888906
return ERR_OK;
889907
}
890908
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+
}
895915
if(tcp_close(_pcb) != ERR_OK) {
896916
tcp_abort(_pcb);
897917
}
898-
_closed_slots[_closed_slot] = _closed_index;
899-
++ _closed_index;
918+
_free_closed_slot();
900919
_pcb = NULL;
901920
return ERR_OK;
902921
}

src/AsyncTCP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class AsyncClient {
170170
uint16_t _connect_port;
171171

172172
int8_t _close();
173+
void _free_closed_slot();
174+
void _allocate_closed_slot();
173175
int8_t _connected(void* pcb, int8_t err);
174176
void _error(int8_t err);
175177
int8_t _poll(tcp_pcb* pcb);

0 commit comments

Comments
 (0)