Keep removed ETCP subscriptions until the outer dispatch returns

This commit is contained in:
evgeny committed 2026-10-23 22:39:56 +02:00
1 parent 64a216afe6
commit cfd2ba2fef
2 files changed
+74 -14

No files matched your search

+9 -2
View File
@@ -252,15 +252,18 @@ struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance, char* n
/* Конструктор удерживает ресурсы до завершения уведомлений; отмена не возвращает закрытый объект. */
etcp->callbacks_running++;
instance->etcp_callbacks_depth++;
struct etcp_inst_cbk_entry* cbe = instance->new_conn_cbks;
while (cbe) {
struct etcp_inst_cbk_entry* next = cbe->next;
cbe->fn(etcp, cbe->arg);
if (cbe->fn) cbe->fn(etcp, cbe->arg);
if (etcp->close_requested) break;
cbe = next;
}
if (!etcp->close_requested) etcp_fire_conn_status(etcp, ETCP_CONN_STATUS_NEW);
etcp->callbacks_running--;
instance->etcp_callbacks_depth--;
etcp_instance_callbacks_collect(instance);
if (etcp->close_requested) {
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] connection creation cancelled by callback", etcp->log_name);
etcp_connection_close(etcp);
@@ -278,14 +281,18 @@ void etcp_fire_conn_status(struct ETCP_CONN* etcp, int status) {
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "[%s] Connection status changed to %s", etcp->log_name,
(status >= 0 && status < (int)(sizeof(names)/sizeof(names[0]))) ? names[status] : "?");
etcp->callbacks_running++;
struct UTUN_INSTANCE* inst = etcp->instance;
inst->etcp_callbacks_depth++;
struct etcp_status_cbk_entry* cbe = etcp->instance->conn_status_cbks;
while (cbe) {
struct etcp_status_cbk_entry* n = cbe->next;
cbe->fn(etcp, status, cbe->arg);
if (cbe->fn) cbe->fn(etcp, status, cbe->arg);
if (etcp->close_requested && status != ETCP_CONN_STATUS_DOWN && status != ETCP_CONN_STATUS_DELETE) break;
cbe = n;
}
etcp->callbacks_running--;
inst->etcp_callbacks_depth--;
etcp_instance_callbacks_collect(inst);
}
void etcp_cbk_fire(struct ETCP_CONN* conn, int event) {
+65 -12
View File
@@ -13,6 +13,32 @@
#define DEBUG_CATEGORY_ETCP_API DEBUG_CATEGORY_ETCP
void etcp_instance_callbacks_collect(struct UTUN_INSTANCE* inst) {
if (!inst || inst->etcp_callbacks_depth) return;
unsigned removed = 0;
struct etcp_inst_cbk_entry **created = &inst->new_conn_cbks;
while (*created) {
struct etcp_inst_cbk_entry *e = *created;
if (!e->fn) { *created = e->next; u_free(e); removed++; } else created = &e->next;
}
struct etcp_status_cbk_entry **status = &inst->conn_status_cbks;
while (*status) {
struct etcp_status_cbk_entry *e = *status;
if (!e->fn) { *status = e->next; u_free(e); removed++; } else status = &e->next;
}
struct etcp_link_status_cbk_entry **links = &inst->link_status_cbks;
while (*links) {
struct etcp_link_status_cbk_entry *e = *links;
if (!e->fn) { *links = e->next; u_free(e); removed++; } else links = &e->next;
}
struct etcp_socket_cbk_entry **sockets = &inst->socket_cbks;
while (*sockets) {
struct etcp_socket_cbk_entry *e = *sockets;
if (!e->fn) { *sockets = e->next; u_free(e); removed++; } else sockets = &e->next;
}
if (removed) DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "instance callback cleanup: removed=%u", removed);
}
// Per-connection unified callback chain helpers
static void etcp_cbk_add_to_chain(struct etcp_cbk_entry** head, etcp_cbk_fn fn, void* arg, int event_mask) {
if (!head || !fn) return;
@@ -55,19 +81,22 @@ static void etcp_inst_cbk_add_chain(struct etcp_inst_cbk_entry** head, etcp_inst
e->fn = fn; e->arg = arg; e->next = *head;
*head = e;
}
static void etcp_inst_cbk_remove_chain(struct etcp_inst_cbk_entry** head, etcp_inst_cbk_fn fn, void* arg) {
static void etcp_inst_cbk_remove_chain(struct etcp_inst_cbk_entry** head, etcp_inst_cbk_fn fn, void* arg, unsigned depth) {
if (!head || !fn) return;
struct etcp_inst_cbk_entry** p = head;
while (*p) {
if ((*p)->fn == fn && (*p)->arg == arg) {
struct etcp_inst_cbk_entry* rm = *p;
if (depth) { rm->fn = NULL; rm->arg = NULL; return; }
*p = rm->next; u_free(rm); return;
}
p = &(*p)->next;
}
}
void etcp_add_new_conn_cbk(struct UTUN_INSTANCE* inst, etcp_inst_cbk_fn fn, void* arg) { if (inst) etcp_inst_cbk_add_chain(&inst->new_conn_cbks, fn, arg); }
void etcp_remove_new_conn_cbk(struct UTUN_INSTANCE* inst, etcp_inst_cbk_fn fn, void* arg) { if (inst) etcp_inst_cbk_remove_chain(&inst->new_conn_cbks, fn, arg); }
void etcp_remove_new_conn_cbk(struct UTUN_INSTANCE* inst, etcp_inst_cbk_fn fn, void* arg) {
if (inst) etcp_inst_cbk_remove_chain(&inst->new_conn_cbks, fn, arg, inst->etcp_callbacks_depth);
}
// Link status callback chain
static void etcp_link_status_cbk_add_chain(struct etcp_link_status_cbk_entry** head, etcp_link_status_cbk_fn fn, void* arg) {
@@ -77,30 +106,35 @@ static void etcp_link_status_cbk_add_chain(struct etcp_link_status_cbk_entry** h
e->fn = fn; e->arg = arg; e->next = *head;
*head = e;
}
static void etcp_link_status_cbk_remove_chain(struct etcp_link_status_cbk_entry** head, etcp_link_status_cbk_fn fn, void* arg) {
static void etcp_link_status_cbk_remove_chain(struct etcp_link_status_cbk_entry** head, etcp_link_status_cbk_fn fn, void* arg, unsigned depth) {
if (!head || !fn) return;
struct etcp_link_status_cbk_entry** p = head;
while (*p) {
if ((*p)->fn == fn && (*p)->arg == arg) {
struct etcp_link_status_cbk_entry* rm = *p;
if (depth) { rm->fn = NULL; rm->arg = NULL; return; }
*p = rm->next; u_free(rm); return;
}
p = &(*p)->next;
}
}
void etcp_add_link_status_cbk(struct UTUN_INSTANCE* inst, etcp_link_status_cbk_fn fn, void* arg) { if (inst) etcp_link_status_cbk_add_chain(&inst->link_status_cbks, fn, arg); }
void etcp_remove_link_status_cbk(struct UTUN_INSTANCE* inst, etcp_link_status_cbk_fn fn, void* arg) { if (inst) etcp_link_status_cbk_remove_chain(&inst->link_status_cbks, fn, arg); }
void etcp_remove_link_status_cbk(struct UTUN_INSTANCE* inst, etcp_link_status_cbk_fn fn, void* arg) {
if (inst) etcp_link_status_cbk_remove_chain(&inst->link_status_cbks, fn, arg, inst->etcp_callbacks_depth);
}
void etcp_fire_link_status_cbk(struct ETCP_LINK* link, int old_state, int old_status) {
if (!link || !link->etcp || !link->etcp->instance) return;
struct ETCP_CONN* conn = link->etcp;
struct UTUN_INSTANCE* inst = conn->instance;
int closing_connection = conn->close_requested;
int was_attached = 0;
for (struct ETCP_LINK* current = conn->links; current; current = current->next) if (current == link) { was_attached = 1; break; }
conn->callbacks_running++;
inst->etcp_callbacks_depth++;
struct etcp_link_status_cbk_entry* cbe = conn->instance->link_status_cbks;
while (cbe) {
cbe->fn(conn, link, old_state, old_status, cbe->arg);
if (cbe->fn) cbe->fn(conn, link, old_state, old_status, cbe->arg);
if (!closing_connection && conn->close_requested) break;
if (was_attached) {
int attached = 0;
@@ -111,6 +145,8 @@ void etcp_fire_link_status_cbk(struct ETCP_LINK* link, int old_state, int old_st
cbe = cbe->next;
}
conn->callbacks_running--;
inst->etcp_callbacks_depth--;
etcp_instance_callbacks_collect(inst);
}
static void etcp_status_cbk_add_chain(struct etcp_status_cbk_entry** head, etcp_conn_status_fn fn, void* arg) {
@@ -120,19 +156,22 @@ static void etcp_status_cbk_add_chain(struct etcp_status_cbk_entry** head, etcp_
e->fn = fn; e->arg = arg; e->next = *head;
*head = e;
}
static void etcp_status_cbk_remove_chain(struct etcp_status_cbk_entry** head, etcp_conn_status_fn fn, void* arg) {
static void etcp_status_cbk_remove_chain(struct etcp_status_cbk_entry** head, etcp_conn_status_fn fn, void* arg, unsigned depth) {
if (!head || !fn) return;
struct etcp_status_cbk_entry** p = head;
while (*p) {
if ((*p)->fn == fn && (*p)->arg == arg) {
struct etcp_status_cbk_entry* rm = *p;
if (depth) { rm->fn = NULL; rm->arg = NULL; return; }
*p = rm->next; u_free(rm); return;
}
p = &(*p)->next;
}
}
void etcp_add_conn_status_cbk(struct UTUN_INSTANCE* inst, etcp_conn_status_fn fn, void* arg) { if (inst) etcp_status_cbk_add_chain(&inst->conn_status_cbks, fn, arg); }
void etcp_remove_conn_status_cbk(struct UTUN_INSTANCE* inst, etcp_conn_status_fn fn, void* arg) { if (inst) etcp_status_cbk_remove_chain(&inst->conn_status_cbks, fn, arg); }
void etcp_remove_conn_status_cbk(struct UTUN_INSTANCE* inst, etcp_conn_status_fn fn, void* arg) {
if (inst) etcp_status_cbk_remove_chain(&inst->conn_status_cbks, fn, arg, inst->etcp_callbacks_depth);
}
static void etcp_socket_cbk_add_chain(struct etcp_socket_cbk_entry** head, etcp_socket_cbk_fn fn, void* arg, int event_mask) {
if (!head || !fn) return;
@@ -141,19 +180,26 @@ static void etcp_socket_cbk_add_chain(struct etcp_socket_cbk_entry** head, etcp_
e->fn = fn; e->arg = arg; e->event_mask = event_mask; e->next = *head;
*head = e;
}
static void etcp_socket_cbk_remove_chain(struct etcp_socket_cbk_entry** head, etcp_socket_cbk_fn fn, void* arg) {
static void etcp_socket_cbk_remove_chain(struct etcp_socket_cbk_entry** head, etcp_socket_cbk_fn fn, void* arg, unsigned depth) {
if (!head || !fn) return;
struct etcp_socket_cbk_entry** p = head;
while (*p) {
if ((*p)->fn == fn && (*p)->arg == arg) { struct etcp_socket_cbk_entry* rm = *p; *p = rm->next; u_free(rm); return; }
if ((*p)->fn == fn && (*p)->arg == arg) {
struct etcp_socket_cbk_entry* rm = *p;
if (depth) { rm->fn = NULL; rm->arg = NULL; return; }
*p = rm->next; u_free(rm); return;
}
p = &(*p)->next;
}
}
void etcp_add_socket_cbk(struct UTUN_INSTANCE* inst, etcp_socket_cbk_fn fn, void* arg, int event_mask) { if (inst) etcp_socket_cbk_add_chain(&inst->socket_cbks, fn, arg, event_mask); }
void etcp_remove_socket_cbk(struct UTUN_INSTANCE* inst, etcp_socket_cbk_fn fn, void* arg) { if (inst) etcp_socket_cbk_remove_chain(&inst->socket_cbks, fn, arg); }
void etcp_remove_socket_cbk(struct UTUN_INSTANCE* inst, etcp_socket_cbk_fn fn, void* arg) {
if (inst) etcp_socket_cbk_remove_chain(&inst->socket_cbks, fn, arg, inst->etcp_callbacks_depth);
}
void etcp_socket_cbk_fire(struct ETCP_SOCKET* sock, int event) {
if (!sock || !sock->instance) return;
struct UTUN_INSTANCE* inst = sock->instance;
static const char* names[] = { "ADDR_CHANGED", "STATUS_CHANGED" };
int idx = 0, e = event; while (e >>= 1) idx++;
const char* name = (idx >= 0 && idx < (int)(sizeof(names)/sizeof(names[0]))) ? names[idx] : "?";
@@ -163,8 +209,15 @@ void etcp_socket_cbk_fire(struct ETCP_SOCKET* sock, int event) {
sockaddr_storage_to_str(&sock->local_addr).str,
sockaddr_storage_to_str(&sock->nat_addr).str,
sock->type, sock->netif_index);
struct etcp_socket_cbk_entry* cbe = sock->instance->socket_cbks;
while (cbe) { struct etcp_socket_cbk_entry* n = cbe->next; if (cbe->event_mask & event) cbe->fn(sock, event, cbe->arg); cbe = n; }
inst->etcp_callbacks_depth++;
struct etcp_socket_cbk_entry* cbe = inst->socket_cbks;
while (cbe) {
struct etcp_socket_cbk_entry* next = cbe->next;
if (cbe->fn && (cbe->event_mask & event)) cbe->fn(sock, event, cbe->arg);
cbe = next;
}
inst->etcp_callbacks_depth--;
etcp_instance_callbacks_collect(inst);
}
int etcp_bind(struct UTUN_INSTANCE* inst, uint8_t id, etcp_recv_fn callback) {