Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/scripts/install-platformio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function build_pio_sketch(){ # build_pio_sketch <board> <path-to-ino>
local sketch_dir=$(dirname "$sketch")
echo ""
echo "Compiling '"$(basename "$sketch")"' ..."
python -m platformio ci -l '.' --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv"
python -m platformio ci -l '.' -l "$HOME/ESPAsyncWebServer" --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv"
}

function count_sketches() # count_sketches <examples-path>
Expand Down
6 changes: 3 additions & 3 deletions .github/scripts/on-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ else

if [[ "$OSTYPE" != "cygwin" ]] && [[ "$OSTYPE" != "msys" ]] && [[ "$OSTYPE" != "win32" ]]; then
echo "Installing ESPAsyncWebServer ..."
python -m platformio lib -g install https://github.com/me-no-dev/ESPAsyncWebServer.git > /dev/null 2>&1
# python -m platformio lib -g install https://github.com/me-no-dev/ESPAsyncWebServer.git > /dev/null 2>&1
git clone https://github.com/me-no-dev/ESPAsyncWebServer "$HOME/ESPAsyncWebServer" > /dev/null 2>&1

echo "Installing ArduinoJson ..."
python -m platformio lib -g install https://github.com/bblanchon/ArduinoJson.git > /dev/null 2>&1
# echo "Installing ArduinoJson ..."
# python -m platformio lib -g install https://github.com/bblanchon/ArduinoJson.git > /dev/null 2>&1

build_pio_sketches "$BOARD" "$HOME/ESPAsyncWebServer/examples"
fi
Expand Down
69 changes: 44 additions & 25 deletions src/AsyncTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ static TaskHandle_t _async_service_task_handle = NULL;

SemaphoreHandle_t _slots_lock;
const int _number_of_closed_slots = CONFIG_LWIP_MAX_ACTIVE_TCP;
static int _closed_slots[_number_of_closed_slots];
static int _closed_index = []() {
static uint32_t _closed_slots[_number_of_closed_slots];
static uint32_t _closed_index = []() {
_slots_lock = xSemaphoreCreateBinary();
xSemaphoreGive(_slots_lock);
for (int i = 0; i < _number_of_closed_slots; ++ i) {
Expand Down Expand Up @@ -152,7 +152,10 @@ static bool _remove_events_with_arg(void * arg){
}

static void _handle_async_event(lwip_event_packet_t * e){
if(e->event == LWIP_TCP_CLEAR){
if(e->arg == NULL){
// do nothing when arg is NULL
//ets_printf("event arg == NULL: 0x%08x\n", e->recv.pcb);
} else if(e->event == LWIP_TCP_CLEAR){
_remove_events_with_arg(e->arg);
} else if(e->event == LWIP_TCP_RECV){
//ets_printf("-R: 0x%08x\n", e->recv.pcb);
Expand Down Expand Up @@ -565,17 +568,7 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
_pcb = pcb;
_closed_slot = -1;
if(_pcb){
xSemaphoreTake(_slots_lock, portMAX_DELAY);
int closed_slot_min_index = 0;
for (int i = 0; i < _number_of_closed_slots; ++ i) {
if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0) {
closed_slot_min_index = _closed_slots[i];
_closed_slot = i;
}
}
_closed_slots[_closed_slot] = 0;
xSemaphoreGive(_slots_lock);

_allocate_closed_slot();
_rx_last_packet = millis();
tcp_arg(_pcb, this);
tcp_recv(_pcb, &_tcp_recv);
Expand All @@ -589,6 +582,7 @@ AsyncClient::~AsyncClient(){
if(_pcb) {
_close();
}
_free_closed_slot();
}

/*
Expand Down Expand Up @@ -714,7 +708,6 @@ bool AsyncClient::connect(const char* host, uint16_t port){
ip_addr_t addr;

if(!_start_async_task()){
Serial.println("failed to start task");
log_e("failed to start task");
return false;
}
Expand Down Expand Up @@ -825,6 +818,29 @@ int8_t AsyncClient::_close(){
return err;
}

void AsyncClient::_allocate_closed_slot(){
xSemaphoreTake(_slots_lock, portMAX_DELAY);
uint32_t closed_slot_min_index = 0;
for (int i = 0; i < _number_of_closed_slots; ++ i) {
if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0) {
closed_slot_min_index = _closed_slots[i];
_closed_slot = i;
}
}
if (_closed_slot != -1) {
_closed_slots[_closed_slot] = 0;
}
xSemaphoreGive(_slots_lock);
}

void AsyncClient::_free_closed_slot(){
if (_closed_slot != -1) {
_closed_slots[_closed_slot] = _closed_index;
_closed_slot = -1;
++ _closed_index;
}
}

/*
* Private Callbacks
* */
Expand All @@ -847,10 +863,12 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
void AsyncClient::_error(int8_t err) {
if(_pcb){
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_poll(_pcb, NULL, 0);
if(_pcb->state == LISTEN) {
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_poll(_pcb, NULL, 0);
}
_pcb = NULL;
}
if(_error_cb) {
Expand All @@ -868,15 +886,16 @@ int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) {
return ERR_OK;
}
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_poll(_pcb, NULL, 0);
if(_pcb->state == LISTEN) {
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_poll(_pcb, NULL, 0);
}
if(tcp_close(_pcb) != ERR_OK) {
tcp_abort(_pcb);
}
_closed_slots[_closed_slot] = _closed_index;
++ _closed_index;
_free_closed_slot();
_pcb = NULL;
return ERR_OK;
}
Expand Down
2 changes: 2 additions & 0 deletions src/AsyncTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class AsyncClient {
uint16_t _connect_port;

int8_t _close();
void _free_closed_slot();
void _allocate_closed_slot();
int8_t _connected(void* pcb, int8_t err);
void _error(int8_t err);
int8_t _poll(tcp_pcb* pcb);
Expand Down