• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-ws-server-threads-foreign-smp
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  * This demonstrates a minimal ws server that can cooperate with
10  * other threads cleanly.  Two other threads are started, which fill
11  * a ringbuffer with strings at 10Hz.
12  *
13  * The actual work and thread spawning etc are done in the protocol
14  * implementation in protocol_lws_minimal.c.
15  *
16  * To keep it simple, it serves stuff in the subdirectory "./mount-origin" of
17  * the directory it was started in.
18  * You can change that by changing mount.origin.
19  */
20 
21 #include <libwebsockets.h>
22 #include <string.h>
23 #include <signal.h>
24 #if defined(WIN32)
25 #define HAVE_STRUCT_TIMESPEC
26 #if defined(pid_t)
27 #undef pid_t
28 #endif
29 #endif
30 #include <pthread.h>
31 #include <uv.h>
32 
33 #define COUNT_THREADS 5
34 
35 #define LWS_PLUGIN_STATIC
36 #include "protocol_lws_minimal.c"
37 
38 static struct lws_protocols protocols[] = {
39 	{ "http", lws_callback_http_dummy, 0, 0, 0, NULL, 0 },
40 	LWS_PLUGIN_PROTOCOL_MINIMAL,
41 	LWS_PROTOCOL_LIST_TERM
42 };
43 
44 static struct lws_context *context;
45 static int interrupted;
46 static uv_loop_t loop[COUNT_THREADS];
47 static uv_signal_t *s, signal_outer[COUNT_THREADS];
48 
49 static const struct lws_http_mount mount = {
50 	/* .mount_next */		NULL,		/* linked-list "next" */
51 	/* .mountpoint */		"/",		/* mountpoint URL */
52 	/* .origin */			"./mount-origin", /* serve from dir */
53 	/* .def */			"index.html",	/* default filename */
54 	/* .protocol */			NULL,
55 	/* .cgienv */			NULL,
56 	/* .extra_mimetypes */		NULL,
57 	/* .interpret */		NULL,
58 	/* .cgi_timeout */		0,
59 	/* .cache_max_age */		0,
60 	/* .auth_mask */		0,
61 	/* .cache_reusable */		0,
62 	/* .cache_revalidate */		0,
63 	/* .cache_intermediaries */	0,
64 	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
65 	/* .mountpoint_len */		1,		/* char count */
66 	/* .basic_auth_login_file */	NULL,
67 };
68 
69 /*
70  * This demonstrates how to pass a pointer into a specific protocol handler
71  * running on a specific vhost.  In this case, it's our default vhost and
72  * we pass the pvo named "config" with the value a const char * "myconfig".
73  *
74  * This is the preferred way to pass configuration into a specific vhost +
75  * protocol instance.
76  */
77 
78 static const struct lws_protocol_vhost_options pvo_ops = {
79 	NULL,
80 	NULL,
81 	"config",		/* pvo name */
82 	(void *)"myconfig"	/* pvo value */
83 };
84 
85 static const struct lws_protocol_vhost_options pvo = {
86 	NULL,		/* "next" pvo linked-list */
87 	&pvo_ops,	/* "child" pvo linked-list */
88 	"lws-minimal",	/* protocol name we belong to on this vhost */
89 	""		/* ignored */
90 };
91 
thread_service(void * threadid)92 void *thread_service(void *threadid)
93 {
94 	/*
95 	 * This is a foreign thread context for each event loop... lws doesn't
96 	 * know about it, except that it's getting called into from the event
97 	 * lib bound to each of these.
98 	 *
99 	 * When closing, at the point we have detached everything related to
100 	 * lws from the loop and destroyed the context we can as the "foreign
101 	 * app" take care of stopping the foreign loop and cloing this thread.
102 	 *
103 	 * The call to lws_service_tsi just starts the related event loop
104 	 */
105 	while (lws_service_tsi(context, 0,
106 			       (int)(lws_intptr_t)threadid) >= 0 &&
107 	       !interrupted)
108 		lwsl_notice("%s\n", __func__);
109 
110 	lwsl_info("%s: thr %d: exiting\n", __func__, (int)(lws_intptr_t)threadid);
111 
112 	pthread_exit(NULL);
113 
114 	return NULL;
115 }
116 
117 static void
signal_cb(uv_signal_t * watcher,int signum)118 signal_cb(uv_signal_t *watcher, int signum)
119 {
120 	int n;
121 
122 	n = (int)(watcher - signal_outer);
123 
124 	lwsl_notice("%s: thr %d: signal %d caught\n", __func__, n,
125 			watcher->signum);
126 
127 	uv_signal_stop(watcher);
128 	uv_close((uv_handle_t *)&signal_outer[n], NULL);
129 	if (!interrupted) {
130 		interrupted = 1;
131 		lws_context_destroy(context);
132 	}
133 }
134 
main(int argc,const char ** argv)135 int main(int argc, const char **argv)
136 {
137 	int n, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
138 	pthread_t pthread_service[COUNT_THREADS];
139 	struct lws_context_creation_info info;
140 	void *foreign_loops[COUNT_THREADS];
141 	int actual_threads;
142 	const char *p;
143 	void *retval;
144 
145 	if ((p = lws_cmdline_option(argc, argv, "-d")))
146 		logs = atoi(p);
147 
148 	lws_set_log_level(logs, NULL);
149 	lwsl_user("LWS minimal ws server + threads + smp | visit http://localhost:7681\n");
150 
151 	for (n = 0; n < COUNT_THREADS; n++) {
152 		uv_loop_init(&loop[n]);
153 
154 		s = &signal_outer[n];
155 		uv_signal_init(&loop[n], s);
156 		uv_signal_start(s, signal_cb, SIGINT);
157 
158 		foreign_loops[n] = &loop[n];
159 	}
160 
161 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
162 	info.port = 7681;
163 	info.mounts = &mount;
164 	info.pcontext = &context;
165 	info.protocols = protocols;
166 	info.pvo = &pvo; /* per-vhost options */
167 	info.foreign_loops = foreign_loops;
168 	info.count_threads = COUNT_THREADS;
169 	info.options = LWS_SERVER_OPTION_LIBUV |
170 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
171 
172 	context = lws_create_context(&info);
173 	if (!context) {
174 		lwsl_err("lws init failed\n");
175 		return 1;
176 	}
177 
178 	actual_threads = lws_get_count_threads(context);
179 	lwsl_notice("  Service threads: %d\n", actual_threads);
180 
181 	/* start all the service threads */
182 
183 	for (n = 0; n < actual_threads; n++)
184 		if (pthread_create(&pthread_service[n], NULL, thread_service,
185 				   (void *)(lws_intptr_t)n))
186 			lwsl_err("Failed to start service thread\n");
187 
188 	/* wait for all the service threads to exit */
189 
190 	while ((--n) >= 0)
191 		pthread_join(pthread_service[n], &retval);
192 
193 	lws_context_destroy(context);
194 
195 	for (n = 0; n < COUNT_THREADS; n++) {
196 		int m;
197 
198 		m = uv_loop_close(&loop[n]);
199 		if (m)
200 			lwsl_notice("%s: uv_close_loop %d: %d\n", __func__, n, m);
201 	}
202 
203 	return 0;
204 }
205