Add mg_wakeup for BSD

This commit is contained in:
Sergio R. Caprile committed 2026-06-18 17:48:09 -03:00
1 parent a861fdf655
commit bb92055e87
6 files changed
+188 -16

No files matched your search

+77
View File
@@ -530,5 +530,82 @@ void mg_bsd_transport_close(void *t) {
mg_bsd_transport_free(x);
}
#ifndef MG_WAKEUP_QUEUE_DEPTH
#define MG_WAKEUP_QUEUE_DEPTH 4
#endif
struct wumsg {
unsigned long id;
size_t len;
uint8_t data[];
};
static void wufn(struct mg_connection *c, int ev, void *ev_data) {
QueueHandle_t q = (QueueHandle_t) c->mgr->pipe.q;
if (ev == MG_EV_POLL) {
struct wumsg *m;
if (xQueueReceive(q, &m, 0) == pdTRUE) {
struct mg_connection *t;
for (t = c->mgr->conns; t != NULL; t = t->next) {
if (t->id == m->id) {
struct mg_str data = mg_str_n((char *) m->data, m->len);
mg_call(t, MG_EV_WAKEUP, &data);
break;
}
}
free(m);
}
} else if (ev == MG_EV_CLOSE) {
struct wumsg *m;
while (xQueueReceive(q, &m, 0) == pdTRUE) free(m);
vQueueDelete(q);
c->mgr->pipe.q = NULL;
}
(void) ev_data;
}
bool mg_wakeup_init(struct mg_mgr *mgr) {
struct mg_connection *c;
if (mgr->pipe.q != NULL) return true;
mgr->pipe.q = xQueueCreate(MG_WAKEUP_QUEUE_DEPTH, sizeof(void *));
if (mgr->pipe.q == NULL) {
MG_ERROR(("Cannot create queue"));
return false;
}
c = mg_alloc_conn(mgr);
if (c == NULL) {
vQueueDelete((QueueHandle_t) mgr->pipe.q);
mgr->pipe.q = NULL;
return false;
}
c->fd = (void *) (size_t) MG_INVALID_SOCKET;
c->fn = wufn;
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
MG_DEBUG(("%lu queue %p", c->id, mgr->pipe.q));
mg_call(c, MG_EV_OPEN, NULL);
return true;
}
bool mg_wakeup(struct mg_mgr *mgr, unsigned long conn_id, const void *buf,
size_t len) {
struct wumsg *m;
if (mgr->pipe.q == NULL || conn_id == 0) return false;
m = (struct wumsg *) calloc(1, sizeof(*m) + len);
if (m == NULL) {
MG_ERROR("OOM");
return false;
}
m->id = conn_id;
m->len = len;
memcpy(m->data, buf, len);
if (xQueueSend((QueueHandle_t) mgr->pipe.q, &m, 0) != pdTRUE) {
free(m);
MG_ERROR("xQueueSend");
return false;
}
return true;
}
#endif // MG_ENABLE_FREERTOS
#endif // MG_ENABLE_BSD_SOCKETS