From df94378b96aec9b1f4a64b969f64fd9e714993c3 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 20 Jan 2022 03:35:03 +0000 Subject: [PATCH] Actually expire broadcasts. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3777 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/ssb.c b/src/ssb.c index 087402b2..a0ecc711 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -138,6 +138,7 @@ typedef struct _tf_ssb_t { uv_loop_t* loop; uv_udp_t broadcast_listener; uv_udp_t broadcast_sender; + uv_timer_t broadcast_cleanup_timer; uv_timer_t broadcast_timer; uv_timer_t trace_timer; uv_tcp_t server; @@ -1652,6 +1653,11 @@ void tf_ssb_destroy(tf_ssb_t* ssb) uv_close((uv_handle_t*)&ssb->broadcast_timer, _tf_ssb_on_handle_close); } + if (ssb->broadcast_cleanup_timer.data && !uv_is_closing((uv_handle_t*)&ssb->broadcast_cleanup_timer)) + { + uv_close((uv_handle_t*)&ssb->broadcast_cleanup_timer, _tf_ssb_on_handle_close); + } + if (ssb->trace_timer.data && !uv_is_closing((uv_handle_t*)&ssb->trace_timer)) { uv_close((uv_handle_t*)&ssb->trace_timer, _tf_ssb_on_handle_close); @@ -1665,6 +1671,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb) while (ssb->broadcast_listener.data || ssb->broadcast_sender.data || ssb->broadcast_timer.data || + ssb->broadcast_cleanup_timer.data || ssb->trace_timer.data || ssb->server.data) { @@ -2173,6 +2180,33 @@ void tf_ssb_visit_broadcasts(tf_ssb_t* ssb, void (*callback)(const struct sockad } } +static void _tf_ssb_broadcast_cleanup_timer(uv_timer_t* timer) +{ + tf_ssb_t* ssb = timer->data; + int modified = 0; + time_t now = time(NULL); + for (tf_ssb_broadcast_t** it = &ssb->broadcasts; *it;) + { + if ((*it)->mtime < now - 10) + { + tf_ssb_broadcast_t* node = *it; + *it = node->next; + free(node); + ssb->broadcasts_count--; + modified++; + } + else + { + it = &(*it)->next; + } + } + + if (modified) + { + _tf_ssb_notify_broadcasts_changed(ssb); + } +} + void tf_ssb_broadcast_listener_start(tf_ssb_t* ssb, bool linger) { if (ssb->broadcast_listener.data) @@ -2203,6 +2237,10 @@ void tf_ssb_broadcast_listener_start(tf_ssb_t* ssb, bool linger) { uv_unref((uv_handle_t*)&ssb->broadcast_listener); } + + ssb->broadcast_cleanup_timer.data = ssb; + uv_timer_init(ssb->loop, &ssb->broadcast_cleanup_timer); + uv_timer_start(&ssb->broadcast_cleanup_timer, _tf_ssb_broadcast_cleanup_timer, 2000, 2000); } void tf_ssb_append_post(tf_ssb_t* ssb, const char* text)