-int main_loop(void) {
- struct event timeout_event;
- struct event periodic_event;
-
- timeout_set(&timeout_event, timeout_handler, &timeout_event);
- event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
-
- timeout_set(&periodic_event, periodic_handler, &periodic_event);
- event_add(&periodic_event, &(struct timeval){5, 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));
- return 1;
+void main_loop(meshlink_handle_t *mesh) {
+ timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timespec) {
+ 1, prng(mesh, TIMER_FUDGE)
+ });
+ timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timespec) {
+ 0, 0
+ });
+
+ //Add signal handler
+ mesh->datafromapp.signum = 0;
+ signal_add(&mesh->loop, &mesh->datafromapp, meshlink_send_from_queue, mesh, mesh->datafromapp.signum);
+
+ if(!event_loop_run(&mesh->loop, &mesh->mutex)) {
+ logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
+ call_error_cb(mesh, MESHLINK_ENETWORK);