-int main_loop(void) {
- struct event timeout_event;
-
- timeout_set(&timeout_event, timeout_handler, &timeout_event);
- event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
-
-#ifndef HAVE_MINGW
- struct event sighup_event;
- struct event sigterm_event;
- struct event sigquit_event;
- struct event sigalrm_event;
-
- signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
- signal_add(&sighup_event, NULL);
- signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
- signal_add(&sigterm_event, NULL);
- signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
- signal_add(&sigquit_event, NULL);
- signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
- signal_add(&sigalrm_event, NULL);
-#endif
-
- if(event_loop(0) < 0) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
+int main_loop(meshlink_handle_t *mesh) {
+ timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval) {
+ mesh->pingtimeout, rand() % 100000
+ });
+ timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval) {
+ 0, 0
+ });
+
+ //Add signal handler
+ 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))) {
+ logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));