2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
16 * The test apps are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
21 #include "test-server.h"
25 int max_poll_elements;
29 struct lws_pollfd *pollfds;
33 volatile int force_exit = 0;
34 struct lws_context *context;
37 * This mutex lock protects code that changes or relies on wsi list outside of
38 * the service thread. The service thread will acquire it when changing the
39 * wsi list and other threads should acquire it while dereferencing wsis or
40 * calling apis like lws_callback_on_writable_all_protocol() which
41 * use the wsi list and wsis from a different thread context.
43 pthread_mutex_t lock_established_conns;
45 /* http server gets files from this path */
46 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
47 char *resource_path = LOCAL_RESOURCE_PATH;
50 * multithreaded version - protect wsi lifecycle changes in the library
51 * these are called from protocol 0 callbacks
54 void test_server_lock(int care)
57 pthread_mutex_lock(&lock_established_conns);
59 void test_server_unlock(int care)
62 pthread_mutex_unlock(&lock_established_conns);
66 * This demo server shows how to use libwebsockets for one or more
67 * websocket protocols in the same server
69 * It defines the following websocket protocols:
71 * dumb-increment-protocol: once the socket is opened, an incrementing
72 * ascii string is sent down it every 50ms.
73 * If you send "reset\n" on the websocket, then
74 * the incrementing number is reset to 0.
76 * lws-mirror-protocol: copies any received packet to every connection also
77 * using this protocol, including the sender
84 PROTOCOL_DUMB_INCREMENT,
91 /* list of supported protocols and callbacks */
93 static struct lws_protocols protocols[] = {
94 /* first protocol must always be HTTP handler */
97 "http-only", /* name */
98 callback_http, /* callback */
99 sizeof (struct per_session_data__http), /* per_session_data_size */
100 0, /* max frame size / rx buffer */
103 "dumb-increment-protocol",
104 callback_dumb_increment,
105 sizeof(struct per_session_data__dumb_increment),
109 "lws-mirror-protocol",
111 sizeof(struct per_session_data__lws_mirror),
114 { NULL, NULL, 0, 0 } /* terminator */
117 void *thread_dumb_increment(void *threadid)
119 while (!force_exit) {
121 * this lock means wsi in the active list cannot
122 * disappear underneath us, because the code to add and remove
123 * them is protected by the same lock
125 pthread_mutex_lock(&lock_established_conns);
126 lws_callback_on_writable_all_protocol(context,
127 &protocols[PROTOCOL_DUMB_INCREMENT]);
128 pthread_mutex_unlock(&lock_established_conns);
135 void *thread_service(void *threadid)
137 while (lws_service_tsi(context, 50, (int)(long)threadid) >= 0 && !force_exit)
143 void sighandler(int sig)
146 lws_cancel_service(context);
149 static const struct lws_extension exts[] = {
151 "permessage-deflate",
152 lws_extension_callback_pm_deflate,
153 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
157 lws_extension_callback_pm_deflate,
160 { NULL, NULL, NULL /* terminator */ }
163 static struct option options[] = {
164 { "help", no_argument, NULL, 'h' },
165 { "debug", required_argument, NULL, 'd' },
166 { "port", required_argument, NULL, 'p' },
167 { "ssl", no_argument, NULL, 's' },
168 { "allow-non-ssl", no_argument, NULL, 'a' },
169 { "interface", required_argument, NULL, 'i' },
170 { "closetest", no_argument, NULL, 'c' },
171 { "libev", no_argument, NULL, 'e' },
172 { "threads", required_argument, NULL, 'j' },
173 #ifndef LWS_NO_DAEMONIZE
174 { "daemonize", no_argument, NULL, 'D' },
176 { "resource_path", required_argument, NULL, 'r' },
180 int main(int argc, char **argv)
182 struct lws_context_creation_info info;
183 char interface_name[128] = "";
184 const char *iface = NULL;
185 pthread_t pthread_dumb, pthread_service[32];
186 char cert_path[1024];
194 int syslog_options = LOG_PID | LOG_PERROR;
196 #ifndef LWS_NO_DAEMONIZE
201 * take care to zero down the info struct, he contains random garbaage
202 * from the stack otherwise
204 memset(&info, 0, sizeof info);
207 pthread_mutex_init(&lock_established_conns, NULL);
210 n = getopt_long(argc, argv, "eci:hsap:d:Dr:j:", options, NULL);
215 threads = atoi(optarg);
216 if (threads > ARRAY_SIZE(pthread_service)) {
217 lwsl_err("Max threads %d\n",
218 ARRAY_SIZE(pthread_service));
223 opts |= LWS_SERVER_OPTION_LIBEV;
225 #ifndef LWS_NO_DAEMONIZE
229 syslog_options &= ~LOG_PERROR;
234 debug_level = atoi(optarg);
240 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
243 info.port = atoi(optarg);
246 strncpy(interface_name, optarg, sizeof interface_name);
247 interface_name[(sizeof interface_name) - 1] = '\0';
248 iface = interface_name;
252 fprintf(stderr, " Close testing mode -- closes on "
253 "client after 50 dumb increments"
254 "and suppresses lws_mirror spam\n");
257 resource_path = optarg;
258 printf("Setting resource path to \"%s\"\n", resource_path);
261 fprintf(stderr, "Usage: test-server "
262 "[--port=<p>] [--ssl] "
263 "[-d <log bitfield>] "
264 "[--resource_path <path>]\n");
269 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
271 * normally lock path would be /var/lock/lwsts or similar, to
272 * simplify getting started without having to take care about
273 * permissions or running as root, set to /tmp/.lwsts-lock
275 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
276 fprintf(stderr, "Failed to daemonize\n");
281 signal(SIGINT, sighandler);
284 /* we will only try to log things according to our debug_level */
285 setlogmask(LOG_UPTO (LOG_DEBUG));
286 openlog("lwsts", syslog_options, LOG_DAEMON);
289 /* tell the library what debug level to emit and to send it to syslog */
290 lws_set_log_level(debug_level, lwsl_emit_syslog);
291 lwsl_notice("libwebsockets test server pthreads - license LGPL2.1+SLE\n");
292 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
294 printf("Using resource path \"%s\"\n", resource_path);
296 max_poll_elements = getdtablesize();
297 pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
298 fd_lookup = malloc(max_poll_elements * sizeof (int));
299 if (pollfds == NULL || fd_lookup == NULL) {
300 lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
306 info.protocols = protocols;
307 info.extensions = exts;
309 info.ssl_cert_filepath = NULL;
310 info.ssl_private_key_filepath = NULL;
313 if (strlen(resource_path) > sizeof(cert_path) - 32) {
314 lwsl_err("resource path too long\n");
317 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
319 if (strlen(resource_path) > sizeof(key_path) - 32) {
320 lwsl_err("resource path too long\n");
323 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
326 info.ssl_cert_filepath = cert_path;
327 info.ssl_private_key_filepath = key_path;
332 info.count_threads = threads;
333 info.extensions = exts;
334 info.max_http_header_pool = 4;
336 context = lws_create_context(&info);
337 if (context == NULL) {
338 lwsl_err("libwebsocket init failed\n");
342 /* start the dumb increment thread */
344 n = pthread_create(&pthread_dumb, NULL, thread_dumb_increment, 0);
346 lwsl_err("Unable to create dumb thread\n");
351 * notice the actual number of threads may be capped by the library,
352 * so use lws_get_count_threads() to get the actual amount of threads
356 for (n = 0; n < lws_get_count_threads(context); n++)
357 if (pthread_create(&pthread_service[n], NULL, thread_service,
359 lwsl_err("Failed to start service thread\n");
361 /* wait for all the service threads to exit */
363 for (n = 0; n < lws_get_count_threads(context); n++)
364 pthread_join(pthread_service[n], &retval);
366 /* wait for pthread_dumb to exit */
367 pthread_join(pthread_dumb, &retval);
370 lws_context_destroy(context);
371 pthread_mutex_destroy(&lock_established_conns);
373 lwsl_notice("libwebsockets-test-server exited cleanly\n");