implement UDP zombie reaper task

This commit is contained in:
jess 2026-04-03 03:48:42 -07:00
parent 9825ddb287
commit 3239eaf9c8
1 changed files with 37 additions and 0 deletions

View File

@ -20,6 +20,9 @@
#define UDP_PORT 5941 #define UDP_PORT 5941
#define UDP_BUF_SIZE 128 #define UDP_BUF_SIZE 128
#define UDP_CLIENTS_MAX 16 #define UDP_CLIENTS_MAX 16
#define REAP_THRESHOLD 10
#define REAP_WINDOW_MS 200
#define REAP_INTERVAL_MS 5000
static int udp_sock = -1; static int udp_sock = -1;
static esp_netif_t *ap_netif; static esp_netif_t *ap_netif;
@ -236,6 +239,39 @@ static void udp_rx_task(void *param)
} }
} }
static void udp_reaper_task(void *arg)
{
(void)arg;
for (;;) {
vTaskDelay(pdMS_TO_TICKS(REAP_INTERVAL_MS));
xSemaphoreTake(client_mutex, portMAX_DELAY);
if (client_count < REAP_THRESHOLD) {
xSemaphoreGive(client_mutex);
continue;
}
uint32_t cutoff = xTaskGetTickCount() * portTICK_PERIOD_MS;
printf("REAP: cycle start, %d clients\n", client_count);
xSemaphoreGive(client_mutex);
send_keepalive();
vTaskDelay(pdMS_TO_TICKS(REAP_WINDOW_MS));
xSemaphoreTake(client_mutex, portMAX_DELAY);
int reaped = 0;
for (int i = client_count - 1; i >= 0; i--) {
if (clients[i].last_touch_ms < cutoff) {
client_remove_by_index(i);
reaped++;
}
}
xSemaphoreGive(client_mutex);
if (reaped) printf("REAP: removed %d zombie(s), %d remain\n", reaped, client_count);
}
}
int wifi_send_sysex(const uint8_t *sysex, uint16_t len) int wifi_send_sysex(const uint8_t *sysex, uint16_t len)
{ {
if (udp_sock < 0 || client_count == 0) if (udp_sock < 0 || client_count == 0)
@ -384,5 +420,6 @@ int wifi_transport_init(void)
} }
xTaskCreate(udp_rx_task, "udp_rx", 4096, NULL, 5, NULL); xTaskCreate(udp_rx_task, "udp_rx", 4096, NULL, 5, NULL);
xTaskCreate(udp_reaper_task, "reaper", 2048, NULL, 4, NULL);
return 0; return 0;
} }