1 /*
2 * lws-minimal-ws-server=threadpool
3 *
4 * Written in 2010-2019 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 #include <pthread.h>
25
26 #define LWS_PLUGIN_STATIC
27 #include "protocol_lws_minimal_threadpool.c"
28
29 static struct lws_protocols protocols[] = {
30 { "http", lws_callback_http_dummy, 0, 0 },
31 LWS_PLUGIN_PROTOCOL_MINIMAL,
32 { NULL, NULL, 0, 0 } /* terminator */
33 };
34
35 static int interrupted;
36
37 static const struct lws_http_mount mount = {
38 /* .mount_next */ NULL, /* linked-list "next" */
39 /* .mountpoint */ "/", /* mountpoint URL */
40 /* .origin */ "./mount-origin", /* serve from dir */
41 /* .def */ "index.html", /* default filename */
42 /* .protocol */ NULL,
43 /* .cgienv */ NULL,
44 /* .extra_mimetypes */ NULL,
45 /* .interpret */ NULL,
46 /* .cgi_timeout */ 0,
47 /* .cache_max_age */ 0,
48 /* .auth_mask */ 0,
49 /* .cache_reusable */ 0,
50 /* .cache_revalidate */ 0,
51 /* .cache_intermediaries */ 0,
52 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
53 /* .mountpoint_len */ 1, /* char count */
54 /* .basic_auth_login_file */ NULL,
55 };
56
57 /*
58 * This demonstrates how to pass a pointer into a specific protocol handler
59 * running on a specific vhost. In this case, it's our default vhost and
60 * we pass the pvo named "config" with the value a const char * "myconfig".
61 *
62 * This is the preferred way to pass configuration into a specific vhost +
63 * protocol instance.
64 */
65
66 static const struct lws_protocol_vhost_options pvo_ops = {
67 NULL,
68 NULL,
69 "config", /* pvo name */
70 (void *)"myconfig" /* pvo value */
71 };
72
73 static const struct lws_protocol_vhost_options pvo = {
74 NULL, /* "next" pvo linked-list */
75 &pvo_ops, /* "child" pvo linked-list */
76 "lws-minimal", /* protocol name we belong to on this vhost */
77 "" /* ignored */
78 };
79
sigint_handler(int sig)80 void sigint_handler(int sig)
81 {
82 interrupted = 1;
83 }
84
main(int argc,const char ** argv)85 int main(int argc, const char **argv)
86 {
87 struct lws_context_creation_info info;
88 struct lws_context *context;
89 const char *p;
90 int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
91 /* for LLL_ verbosity above NOTICE to be built into lws,
92 * lws must have been configured and built with
93 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
94 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
95 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
96 /* | LLL_DEBUG */;
97
98 signal(SIGINT, sigint_handler);
99
100 if ((p = lws_cmdline_option(argc, argv, "-d")))
101 logs = atoi(p);
102
103 lws_set_log_level(logs, NULL);
104 lwsl_user("LWS minimal ws server + threadpool | visit http://localhost:7681\n");
105
106 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
107 info.port = 7681;
108 info.mounts = &mount;
109 info.protocols = protocols;
110 info.pvo = &pvo; /* per-vhost options */
111 info.options =
112 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
113
114 context = lws_create_context(&info);
115 if (!context) {
116 lwsl_err("lws init failed\n");
117 return 1;
118 }
119
120 /* start the threads that create content */
121
122 while (!interrupted)
123 if (lws_service(context, 0))
124 interrupted = 1;
125
126 lws_context_destroy(context);
127
128 return 0;
129 }
130