1 /*
2 * lws-minimal-http-server-eventlib-foreign
3 *
4 * Written in 2010-2020 by Andy Green <andy@warmcat.com>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
9 * The libevent specific code
10 */
11
12 #include <libwebsockets.h>
13
14 #include <event2/event.h>
15
16 #include <string.h>
17 #include <signal.h>
18
19 #include "private.h"
20
21 static struct event_base *loop_event;
22 static struct event *timer_outer_event;
23 static struct event *sighandler_event;
24
25 static void
timer_cb_event(evutil_socket_t fd,short event,void * arg)26 timer_cb_event(evutil_socket_t fd, short event, void *arg)
27 {
28 foreign_timer_service(loop_event);
29 }
30
31 static void
signal_cb_event(evutil_socket_t fd,short event,void * arg)32 signal_cb_event(evutil_socket_t fd, short event, void *arg)
33 {
34 signal_cb((int)(lws_intptr_t)arg);
35 }
36
37 static void
foreign_event_loop_init_and_run_libevent(void)38 foreign_event_loop_init_and_run_libevent(void)
39 {
40 struct timeval tv;
41
42 /* we create and start our "foreign loop" */
43
44 tv.tv_sec = 1;
45 tv.tv_usec = 0;
46 event_enable_debug_mode();
47
48 loop_event = event_base_new();
49 sighandler_event = evsignal_new((struct event_base *)loop_event, SIGINT, signal_cb_event,
50 (void*)SIGINT);
51
52 timer_outer_event = event_new((struct event_base *)loop_event, -1, EV_PERSIST,
53 timer_cb_event, NULL);
54 //evtimer_new(loop_event, timer_cb_event, NULL);
55 evtimer_add(timer_outer_event, &tv);
56
57 event_base_loop(loop_event, 0);
58 }
59
60 static void
foreign_event_loop_stop_libevent(void)61 foreign_event_loop_stop_libevent(void)
62 {
63 event_base_loopexit(loop_event, NULL);
64 }
65
66 static void
foreign_event_loop_cleanup_libevent(void)67 foreign_event_loop_cleanup_libevent(void)
68 {
69 /* cleanup the foreign loop assets */
70
71 evtimer_del(timer_outer_event);
72 event_free(timer_outer_event);
73 evsignal_del(sighandler_event);
74 event_free(sighandler_event);
75
76 event_base_loop(loop_event, 0);
77 event_base_free(loop_event);
78 }
79
80 const struct ops ops_libevent = {
81 foreign_event_loop_init_and_run_libevent,
82 foreign_event_loop_stop_libevent,
83 foreign_event_loop_cleanup_libevent
84 };
85