Actually expire broadcasts.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3777 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-01-20 03:35:03 +00:00
parent 83fa488b8d
commit df94378b96

View File

@ -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)