• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 libuv specific code
10  */
11 
12 #include <libwebsockets.h>
13 
14 #include <string.h>
15 #include <signal.h>
16 
17 #include <uv.h>
18 #ifdef LWS_HAVE_UV_VERSION_H
19 #include <uv-version.h>
20 #endif
21 #ifdef LWS_HAVE_NEW_UV_VERSION_H
22 #include <uv/version.h>
23 #endif
24 
25 #include "private.h"
26 
27 static uv_loop_t loop_uv;
28 static uv_timer_t timer_outer_uv;
29 static uv_signal_t sighandler_uv;
30 
31 static void
timer_cb_uv(uv_timer_t * t)32 timer_cb_uv(uv_timer_t *t)
33 {
34 	foreign_timer_service(&loop_uv);
35 }
36 
37 static void
signal_cb_uv(uv_signal_t * watcher,int signum)38 signal_cb_uv(uv_signal_t *watcher, int signum)
39 {
40 	signal_cb(signum);
41 }
42 
43 static void
foreign_event_loop_init_and_run_libuv(void)44 foreign_event_loop_init_and_run_libuv(void)
45 {
46 	/* we create and start our "foreign loop" */
47 
48 #if (UV_VERSION_MAJOR > 0) // Travis...
49 	uv_loop_init(&loop_uv);
50 #endif
51 	uv_signal_init(&loop_uv, &sighandler_uv);
52 	uv_signal_start(&sighandler_uv, signal_cb_uv, SIGINT);
53 
54 	uv_timer_init(&loop_uv, &timer_outer_uv);
55 #if (UV_VERSION_MAJOR > 0) // Travis...
56 	uv_timer_start(&timer_outer_uv, timer_cb_uv, 0, 1000);
57 #else
58 	(void)timer_cb_uv;
59 #endif
60 
61 	uv_run(&loop_uv, UV_RUN_DEFAULT);
62 }
63 
64 static void
foreign_event_loop_stop_libuv(void)65 foreign_event_loop_stop_libuv(void)
66 {
67 	uv_stop(&loop_uv);
68 }
69 
70 static void
foreign_event_loop_cleanup_libuv(void)71 foreign_event_loop_cleanup_libuv(void)
72 {
73 	/* cleanup the foreign loop assets */
74 
75 	uv_timer_stop(&timer_outer_uv);
76 	uv_close((uv_handle_t*)&timer_outer_uv, NULL);
77 	uv_signal_stop(&sighandler_uv);
78 	uv_close((uv_handle_t *)&sighandler_uv, NULL);
79 
80 	uv_run(&loop_uv, UV_RUN_DEFAULT);
81 #if (UV_VERSION_MAJOR > 0) // Travis...
82 	uv_loop_close(&loop_uv);
83 #endif
84 }
85 
86 const struct ops ops_libuv = {
87 	foreign_event_loop_init_and_run_libuv,
88 	foreign_event_loop_stop_libuv,
89 	foreign_event_loop_cleanup_libuv
90 };
91 
92