if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
abort();
}
+
+ loop->deletion = true;
+}
+
+static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
+ splay_unlink_node(&loop->timeouts, &timeout->node);
+ timeout->tv = (struct timeval) {
+ 0, 0
+ };
}
void timeout_del(event_loop_t *loop, timeout_t *timeout) {
return;
}
- loop->deletion = true;
+ if(timerisset(&timeout->tv)) {
+ timeout_disable(loop, timeout);
+ }
- splay_unlink_node(&loop->timeouts, &timeout->node);
- timeout->cb = 0;
- timeout->tv = (struct timeval) {
- 0, 0
- };
+ timeout->cb = NULL;
+ loop->deletion = true;
}
static int signal_compare(const signal_t *a, const signal_t *b) {
}
}
+static void pipe_exit(event_loop_t *loop) {
+ io_del(loop, &loop->signalio);
+
+ close(loop->pipefd[0]);
+ close(loop->pipefd[1]);
+
+ loop->pipefd[0] = -1;
+ loop->pipefd[1] = -1;
+}
+
void signal_trigger(event_loop_t *loop, signal_t *sig) {
uint8_t signum = sig->signum;
write(loop->pipefd[1], &signum, 1);
splay_unlink_node(&loop->signals, &sig->node);
sig->cb = NULL;
+
+ if(!loop->signals.count && loop->pipefd[0] != -1) {
+ pipe_exit(loop);
+ }
}
void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
fd_set readable;
fd_set writable;
-
while(loop->running) {
gettimeofday(&loop->now, NULL);
struct timeval diff, it, *tv = NULL;
timersub(&timeout->tv, &loop->now, &diff);
if(diff.tv_sec < 0) {
+ timeout_disable(loop, timeout);
timeout->cb(loop, timeout->data);
-
- if(timercmp(&timeout->tv, &loop->now, <)) {
- timeout_del(loop, timeout);
- }
} else {
tv = &diff;
break;
void handle_network_change(meshlink_handle_t *mesh, bool online) {
(void)online;
- if(!mesh->connections) {
+ if(!mesh->connections || !mesh->loop.running) {
return;
}
mesh->datafromapp.signum = 0;
signal_add(&(mesh->loop), &(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue, mesh, mesh->datafromapp.signum);
- if(!event_loop_run(&(mesh->loop), &(mesh->mesh_mutex))) {
+ if(!event_loop_run(&mesh->loop, &mesh->mesh_mutex)) {
logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
abort();
+ signal_del(&mesh->loop, &mesh->datafromapp);
timeout_del(&mesh->loop, &mesh->periodictimer);
timeout_del(&mesh->loop, &mesh->pingtimer);
return 1;
}
+ signal_del(&mesh->loop, &mesh->datafromapp);
timeout_del(&mesh->loop, &mesh->periodictimer);
timeout_del(&mesh->loop, &mesh->pingtimer);
logger(mesh, MESHLINK_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
}
- if(left)
+ if(left) {
timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timeval) {
- 10, rand() % 100000
- });
+ 10, rand() % 100000
+ });
+ }
}
bool seen_request(meshlink_handle_t *mesh, const char *request) {
new = xmalloc(sizeof(*new));
new->request = xstrdup(request);
new->firstseen = mesh->loop.now.tv_sec;
+
+ if(!mesh->past_request_tree->head) {
+ timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timeval) {
+ 10, rand() % 100000
+ });
+ }
+
splay_insert(mesh->past_request_tree, new);
- timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timeval) {
- 10, rand() % 100000
- });
return false;
}
}
void init_requests(meshlink_handle_t *mesh) {
mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
+ timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timeval) {
+ 0, 0
+ });
}
void exit_requests(meshlink_handle_t *mesh) {