option(LWS_WITH_ZLIB "Include zlib support (required for extensions)" ON)
option(LWS_WITH_LIBEV "Compile with support for libev" OFF)
option(LWS_WITH_LIBUV "Compile with support for libuv" OFF)
+option(LWS_WITH_LIBEVENT "Compile with support for libevent" OFF)
option(LWS_USE_BUNDLED_ZLIB "Use bundled zlib version (Windows only)" ${LWS_USE_BUNDLED_ZLIB_DEFAULT})
option(LWS_SSL_CLIENT_USE_OS_CA_CERTS "SSL support should make use of the OS-installed CA root certs" ON)
option(LWS_WITHOUT_BUILTIN_GETIFADDRS "Don't use the BSD getifaddrs implementation from libwebsockets if it is missing (this will result in a compilation error) ... The default is to assume that your libc provides it. On some systems such as uclibc it doesn't exist." OFF)
set(LWS_LIBUV_INCLUDE_DIRS CACHE PATH "Path to the libuv include directory")
set(LWS_SQLITE3_LIBRARIES CACHE PATH "Path to the sqlite3 library")
set(LWS_SQLITE3_INCLUDE_DIRS CACHE PATH "Path to the sqlite3 include directory")
+set(LWS_LIBEVENT_INCLUDE_DIRS CACHE PATH "Path to the libevent include directory")
+set(LWS_LIBEVENT_LIBRARIES CACHE PATH "Path to the libevent library")
if (NOT LWS_WITH_SSL)
endif()
endif()
+if (LWS_WITH_LIBEVENT)
+ if ("${LWS_LIBEVENT_LIBRARIES}" STREQUAL "" OR "${LWS_LIBEVENT_INCLUDE_DIRS}" STREQUAL "")
+ else()
+ set(LIBEVENT_LIBRARIES ${LWS_LIBEVENT_LIBRARIES})
+ set(LIBEVENT_INCLUDE_DIRS ${LWS_LIBEVENT_INCLUDE_DIRS})
+ set(LIBEVENT_FOUND 1)
+ endif()
+endif()
+
if (LWS_WITH_SQLITE3)
if ("${LWS_SQLITE3_LIBRARIES}" STREQUAL "" OR "${LWS_SQLITE3_INCLUDE_DIRS}" STREQUAL "")
else()
set(LWS_USE_LIBUV 1)
endif()
+if (LWS_WITH_LIBEVENT)
+ set(LWS_USE_LIBEVENT 1)
+endif()
+
if (LWS_IPV6)
set(LWS_USE_IPV6 1)
endif()
lib/libuv.c)
endif()
+if (LWS_WITH_LIBEVENT)
+ list(APPEND SOURCES
+ lib/libevent.c)
+endif()
+
if (LWS_WITH_LEJP)
list(APPEND SOURCES
lib/lejp.c)
list(APPEND LIB_LIST ${LIBUV_LIBRARIES})
endif()
+if (LWS_WITH_LIBEVENT)
+ if (NOT LIBEVENT_FOUND)
+ find_path(LIBEVENT_INCLUDE_DIRS NAMES event2/event.h)
+ find_library(LIBEVENT_LIBRARIES NAMES event)
+ if(LIBEVENT_INCLUDE_DIRS AND LIBEVENT_LIBRARIES)
+ set(LIBEVENT_FOUND 1)
+ endif()
+ endif()
+ message("libevent include dir: ${LIBEVENT_INCLUDE_DIRS}")
+ message("libevent libraries: ${LIBEVENT_LIBRARIES}")
+ include_directories("${LIBEVENT_INCLUDE_DIRS}")
+ list(APPEND LIB_LIST ${LIBEVENT_LIBRARIES})
+endif(LWS_WITH_LIBEVENT)
+
if (LWS_WITH_SQLITE3)
if (NOT SQLITE3_FOUND)
find_path(SQLITE3_INCLUDE_DIRS NAMES sqlite3.h)
"test-server/test-server-status.c"
"test-server/test-server-echogen.c")
endif()
+ if (NOT ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
+ AND LWS_WITH_LIBEVENT)
+ create_test_app(test-server-libevent
+ "test-server/test-server-libevent.c"
+ "test-server/test-server-http.c"
+ "test-server/test-server-dumb-increment.c"
+ "test-server/test-server-mirror.c"
+ "test-server/test-server-status.c"
+ "test-server/test-server-echogen.c")
+ endif()
endif()
#
message(" LWS_WITHOUT_DAEMONIZE = ${LWS_WITHOUT_DAEMONIZE}")
message(" LWS_USE_LIBEV = ${LWS_USE_LIBEV}")
message(" LWS_USE_LIBUV = ${LWS_USE_LIBUV}")
+message(" LWS_USE_LIBEVENT = ${LWS_USE_LIBEVENT}")
message(" LWS_IPV6 = ${LWS_IPV6}")
message(" LWS_UNIX_SOCK = ${LWS_UNIX_SOCK}")
message(" LWS_WITH_HTTP2 = ${LWS_WITH_HTTP2}")
lws_libev_accept(wsi, wsi->desc);
lws_libuv_accept(wsi, wsi->desc);
+ lws_libevent_accept(wsi, wsi->desc);
if (insert_wsi_socket_into_fds(context, wsi)) {
compatible_close(wsi->desc.sockfd);
cce = "insert wsi failed";
context->use_ev_sigint = 1;
context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
#endif
+#ifdef LWS_USE_LIBEVENT
+ /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
+ * enable libev mediated SIGINT handling with a default handler of
+ * lws_sigint_cb. The handler can be overridden or disabled
+ * by invoking lws_sigint_cfg after creating the context, but
+ * before invoking lws_initloop:
+ */
+ context->use_ev_sigint = 1;
+ context->lws_event_sigint_cb = &lws_event_sigint_cb;
+#endif /* LWS_USE_LIBEVENT */
lwsl_info(" mem: context: %5lu bytes (%ld ctx + (%ld thr x %d))\n",
(long)sizeof(struct lws_context) +
lws_libev_destroyloop(context, n);
lws_libuv_destroyloop(context, n);
+ lws_libevent_destroyloop(context, n);
lws_free_set_NULL(context->pt[n].serv_buf);
if (pt->ah_pool)
--- /dev/null
+/*
+ * libwebsockets - small server side websockets and web server implementation
+ *
+ * Copyright (C) 2010-2014 Andy Green <andy@warmcat.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation:
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ */
+
+#include "private-libwebsockets.h"
+
+void lws_feature_status_libevent(struct lws_context_creation_info *info)
+{
+ if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBEVENT))
+ lwsl_notice("libevent support compiled in and enabled\n");
+ else
+ lwsl_notice("libevent support compiled in but disabled\n");
+}
+
+static void
+lws_event_cb(evutil_socket_t sock_fd, short revents, void *ctx)
+{
+ struct lws_io_watcher *lws_io = (struct lws_io_watcher *)ctx;
+ struct lws_context *context = lws_io->context;
+ struct lws_pollfd eventfd;
+
+ if (revents & EV_TIMEOUT)
+ return;
+
+ if (revents & EV_CLOSED)
+ {
+ event_del(lws_io->event_watcher);
+ event_free(lws_io->event_watcher);
+ return;
+ }
+
+ eventfd.fd = sock_fd;
+ eventfd.events = 0;
+ eventfd.revents = 0;
+ if (revents & EV_READ)
+ {
+ eventfd.events |= LWS_POLLIN;
+ eventfd.revents |= LWS_POLLIN;
+ }
+ if (revents & EV_WRITE)
+ {
+ eventfd.events |= LWS_POLLOUT;
+ eventfd.revents |= LWS_POLLOUT;
+ }
+ lws_service_fd(context, &eventfd);
+}
+
+LWS_VISIBLE void
+lws_event_sigint_cb(evutil_socket_t sock_fd, short revents, void *ctx)
+{
+ struct lws_context_per_thread *pt = ctx;
+ if (!pt->ev_loop_foreign)
+ event_base_loopbreak(pt->io_loop_event_base);
+}
+
+LWS_VISIBLE int
+lws_event_sigint_cfg(struct lws_context *context, int use_event_sigint,
+ lws_event_signal_cb_t *cb)
+{
+ context->use_ev_sigint = use_event_sigint;
+ if (cb)
+ context->lws_event_sigint_cb = cb;
+ else
+ context->lws_event_sigint_cb = &lws_event_sigint_cb;
+
+ return 0;
+}
+
+LWS_VISIBLE int
+lws_event_initloop(struct lws_context *context, struct event_base *loop,
+ int tsi)
+{
+ if (!loop)
+ {
+ context->pt[tsi].io_loop_event_base = event_base_new();
+ }
+ else
+ {
+ context->pt[tsi].ev_loop_foreign = 1;
+ context->pt[tsi].io_loop_event_base = loop;
+ }
+
+ /*
+ * Initialize all events with the listening sockets
+ * and register a callback for read operations
+ */
+ struct lws_vhost *vh = context->vhost_list;
+ while (vh)
+ {
+ if (vh->lserv_wsi)
+ {
+ vh->lserv_wsi->w_read.context = context;
+ vh->lserv_wsi->w_read.event_watcher = event_new(
+ loop,
+ vh->lserv_wsi->desc.sockfd,
+ (EV_READ | EV_PERSIST),
+ lws_event_cb,
+ &vh->lserv_wsi->w_read);
+ event_add(vh->lserv_wsi->w_read.event_watcher, NULL);
+ }
+ vh = vh->vhost_next;
+ }
+
+ /* Register the signal watcher unless the user says not to */
+ if (context->use_ev_sigint)
+ {
+ struct event *w_sigint = evsignal_new(loop, SIGINT,
+ context->lws_event_sigint_cb, &context->pt[tsi]);
+ context->pt[tsi].w_sigint.event_watcher = w_sigint;
+ event_add(w_sigint, NULL);
+ }
+
+ return 0;
+}
+
+void
+lws_libevent_destroyloop(struct lws_context *context, int tsi)
+{
+ if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEVENT))
+ return;
+
+ struct lws_context_per_thread *pt = &context->pt[tsi];
+ if (!pt->io_loop_event_base)
+ return;
+
+ /*
+ * Free all events with the listening sockets
+ */
+ struct lws_vhost *vh = context->vhost_list;
+ while (vh)
+ {
+ if (vh->lserv_wsi)
+ {
+ event_free(vh->lserv_wsi->w_read.event_watcher);
+ vh->lserv_wsi->w_read.event_watcher = NULL;
+ }
+ vh = vh->vhost_next;
+ }
+
+ if (context->use_ev_sigint)
+ event_free(pt->w_sigint.event_watcher);
+ if (!pt->ev_loop_foreign)
+ event_base_free(pt->io_loop_event_base);
+}
+
+LWS_VISIBLE void
+lws_libevent_accept(struct lws *new_wsi, lws_sock_file_fd_type desc)
+{
+ struct lws_context *context = lws_get_context(new_wsi);
+ if (!LWS_LIBEVENT_ENABLED(context))
+ return;
+
+ new_wsi->w_read.context = context;
+ new_wsi->w_write.context = context;
+
+ // Initialize the event
+ struct lws_context_per_thread *pt = &context->pt[(int)new_wsi->tsi];
+ int fd;
+ if (new_wsi->mode == LWSCM_RAW_FILEDESC)
+ fd = desc.filefd;
+ else
+ fd = desc.sockfd;
+ new_wsi->w_read.event_watcher = event_new(pt->io_loop_event_base, fd,
+ (EV_READ | EV_PERSIST), lws_event_cb, &new_wsi->w_read);
+ new_wsi->w_write.event_watcher = event_new(pt->io_loop_event_base, fd,
+ (EV_WRITE | EV_PERSIST), lws_event_cb, &new_wsi->w_write);
+}
+
+LWS_VISIBLE void
+lws_libevent_io(struct lws *wsi, int flags)
+{
+ struct lws_context *context = lws_get_context(wsi);
+
+ if (!LWS_LIBEVENT_ENABLED(context))
+ return;
+
+ struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
+ if (!pt->io_loop_event_base || context->being_destroyed)
+ return;
+
+ assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
+ (flags & (LWS_EV_READ | LWS_EV_WRITE)));
+
+ if (flags & LWS_EV_START)
+ {
+ if (flags & LWS_EV_WRITE)
+ {
+ event_add(wsi->w_write.event_watcher, NULL);
+ }
+ if (flags & LWS_EV_READ)
+ {
+ event_add(wsi->w_read.event_watcher, NULL);
+ }
+ }
+ else
+ {
+ if (flags & LWS_EV_WRITE)
+ {
+ event_del(wsi->w_write.event_watcher);
+ }
+ if (flags & LWS_EV_READ)
+ {
+ event_del(wsi->w_read.event_watcher);
+ }
+ }
+}
+
+LWS_VISIBLE int
+lws_libevent_init_fd_table(struct lws_context *context)
+{
+ if (!LWS_LIBEVENT_ENABLED(context))
+ return 0;
+
+ int n;
+ for (n = 0; n < context->count_threads; n++)
+ {
+ context->pt[n].w_sigint.context = context;
+ }
+
+ return 1;
+}
+
+LWS_VISIBLE void
+lws_libevent_run(const struct lws_context *context, int tsi)
+{
+ // Run/Dispatch the event_base loop
+ if (context->pt[tsi].io_loop_event_base && LWS_LIBEVENT_ENABLED(context))
+ event_base_dispatch(context->pt[tsi].io_loop_event_base);
+}
#include <uv-version.h>
#endif
#endif /* LWS_USE_LIBUV */
+#ifdef LWS_USE_LIBEVENT
+#include <event2/event.h>
+#endif /* LWS_USE_LIBEVENT */
#ifndef LWS_EXTERN
#define LWS_EXTERN extern
*/
LWS_SERVER_OPTION_FALLBACK_TO_RAW = (1 << 20),
/**< (VH) if invalid http is coming in the first line, */
+ LWS_SERVER_OPTION_LIBEVENT = (1 << 21),
+ /**< (CTX) Use libevent event loop */
/****** add new things just above ---^ ******/
};
#endif /* LWS_USE_LIBUV */
///@}
+/*! \defgroup event libevent helpers
+ *
+ * ##libevent helpers
+ *
+ * APIs specific to libevent event loop itegration
+ */
+///@{
+
+#ifdef LWS_USE_LIBEVENT
+typedef void (lws_event_signal_cb_t) (evutil_socket_t sock_fd, short revents,
+ void *ctx);
+
+LWS_VISIBLE LWS_EXTERN int
+lws_event_sigint_cfg(struct lws_context *context, int use_event_sigint,
+ lws_event_signal_cb_t cb);
+
+LWS_VISIBLE LWS_EXTERN int
+lws_event_initloop(struct lws_context *context, struct event_base *loop,
+ int tsi);
+
+LWS_VISIBLE LWS_EXTERN void
+lws_event_sigint_cb(evutil_socket_t sock_fd, short revents,
+ void *ctx);
+#endif /* LWS_USE_LIBEVENT */
+
+///@}
+
/*! \defgroup timeout Connection timeouts
APIs related to setting connection timeouts
os_timer_arm(&context->to_timer, 1000, 1);
if (!lws_libev_init_fd_table(context) &&
- !lws_libuv_init_fd_table(context)) {
+ !lws_libuv_init_fd_table(context) &&
+ !lws_libevent_init_fd_table(context)) {
/* otherwise libev handled it instead */
#if 0
while (n--) {
lws_libev_run(context, tsi);
lws_libuv_run(context, tsi);
+ lws_libevent_run(context, tsi);
if (!context->service_tid_detected) {
struct lws _lws;
lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
+ lws_libevent_io(wsi, LWS_EV_START | LWS_EV_READ);
pt->fds[pt->fds_count++].revents = 0;
}
lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
+ lws_libevent_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
pt->fds_count--;
}
}
if (!lws_libev_init_fd_table(context) &&
- !lws_libuv_init_fd_table(context)) {
+ !lws_libuv_init_fd_table(context) &&
+ !lws_libevent_init_fd_table(context)) {
/* otherwise libev handled it instead */
while (n--) {
if (_and & LWS_POLLIN) {
lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ);
lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ);
+ lws_libevent_io(wsi, LWS_EV_STOP | LWS_EV_READ);
}
if (_or & LWS_POLLIN) {
lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
+ lws_libevent_io(wsi, LWS_EV_START | LWS_EV_READ);
}
if (_and & LWS_POLLOUT) {
lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
+ lws_libevent_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
}
if (_or & LWS_POLLOUT) {
lws_libev_io(wsi, LWS_EV_START | LWS_EV_WRITE);
lws_libuv_io(wsi, LWS_EV_START | LWS_EV_WRITE);
+ lws_libevent_io(wsi, LWS_EV_START | LWS_EV_WRITE);
}
/*
#ifdef LWS_USE_LIBUV
#include <uv.h>
#endif
+#ifdef LWS_USE_LIBEVENT
+#include <event2/event.h>
+#endif
#ifndef LWS_NO_FORK
#ifdef LWS_HAVE_SYS_PRCTL_H
struct lws_protocols;
struct lws;
-#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
+#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV) || defined(LWS_USE_LIBEVENT)
struct lws_io_watcher {
#ifdef LWS_USE_LIBEV
#endif
#ifdef LWS_USE_LIBUV
uv_poll_t uv_watcher;
+#endif
+#ifdef LWS_USE_LIBEVENT
+ struct event *event_watcher;
#endif
struct lws_context *context;
};
#endif
#ifdef LWS_USE_LIBUV
uv_signal_t uv_watcher;
+#endif
+#ifdef LWS_USE_LIBEVENT
+ struct event *event_watcher;
#endif
struct lws_context *context;
};
struct lws *rx_draining_ext_list;
struct lws *tx_draining_ext_list;
struct lws *timeout_list;
-#ifdef LWS_USE_LIBUV
+#if defined(LWS_USE_LIBUV) || defined(LWS_USE_LIBEVENT)
struct lws_context *context;
#endif
#ifdef LWS_WITH_CGI
uv_timer_t uv_timeout_watcher;
uv_idle_t uv_idle;
#endif
+#if defined(LWS_USE_LIBEVENT)
+ struct event_base *io_loop_event_base;
+#endif
#if defined(LWS_USE_LIBEV)
struct lws_io_watcher w_accept;
#endif
-#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
+#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV) || defined(LWS_USE_LIBEVENT)
struct lws_signal_watcher w_sigint;
unsigned char ev_loop_foreign:1;
#endif
#endif
#if defined(LWS_USE_LIBUV)
uv_signal_cb lws_uv_sigint_cb;
+#endif
+#if defined(LWS_USE_LIBEVENT)
+ lws_event_signal_cb_t * lws_event_sigint_cb;
#endif
char canonical_hostname[128];
#ifdef LWS_LATENCY
#endif
int max_fds;
-#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
+#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV) || defined(LWS_USE_LIBEVENT)
int use_ev_sigint;
#endif
int started_with_parent;
#endif
#endif
+#if defined(LWS_USE_LIBEVENT)
+LWS_EXTERN void
+lws_libevent_accept(struct lws *new_wsi, lws_sock_file_fd_type desc);
+LWS_EXTERN void
+lws_libevent_io(struct lws *wsi, int flags);
+LWS_EXTERN int
+lws_libevent_init_fd_table(struct lws_context *context);
+LWS_EXTERN void
+lws_libevent_destroyloop(struct lws_context *context, int tsi);
+LWS_EXTERN void
+lws_libevent_run(const struct lws_context *context, int tsi);
+#define LWS_LIBEVENT_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEVENT)
+LWS_EXTERN void lws_feature_status_libevent(struct lws_context_creation_info *info);
+#else
+#define lws_libevent_accept(_a, _b) ((void) 0)
+#define lws_libevent_io(_a, _b) ((void) 0)
+#define lws_libevent_init_fd_table(_a) (0)
+#define lws_libevent_run(_a, _b) ((void) 0)
+#define lws_libevent_destroyloop(_a, _b) ((void) 0)
+#define LWS_LIBEVENT_ENABLED(context) (0)
+#if LWS_POSIX && !defined(LWS_WITH_ESP32)
+#define lws_feature_status_libevent(_a) \
+ lwsl_notice("libevent support not compiled in\n")
+#else
+#define lws_feature_status_libevent(_a)
+#endif
+#endif
+
#ifdef LWS_USE_IPV6
#define LWS_IPV6_ENABLED(vh) \
/* lifetime members */
-#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
+#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV) || defined(LWS_USE_LIBEVENT)
struct lws_io_watcher w_read;
#endif
-#if defined(LWS_USE_LIBEV)
+#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBEVENT)
struct lws_io_watcher w_write;
#endif
time_t pending_timeout_limit;
lws_libev_accept(new_wsi, new_wsi->desc);
lws_libuv_accept(new_wsi, new_wsi->desc);
+ lws_libevent_accept(new_wsi, new_wsi->desc);
if (!ssl) {
if (insert_wsi_socket_into_fds(context, new_wsi)) {
/* Enable libuv io loop */
#cmakedefine LWS_USE_LIBUV
+/* Enable libevent io loop */
+#cmakedefine LWS_USE_LIBEVENT
+
/* Build with support for ipv6 */
#cmakedefine LWS_USE_IPV6
--- /dev/null
+/*
+ * libwebsockets-test-server - libwebsockets test implementation
+ *
+ * Copyright (C) 2011-2016 Andy Green <andy@warmcat.com>
+ *
+ * This file is made available under the Creative Commons CC0 1.0
+ * Universal Public Domain Dedication.
+ *
+ * The person who associated a work with this deed has dedicated
+ * the work to the public domain by waiving all of his or her rights
+ * to the work worldwide under copyright law, including all related
+ * and neighboring rights, to the extent allowed by law. You can copy,
+ * modify, distribute and perform the work, even for commercial purposes,
+ * all without asking permission.
+ *
+ * The test apps are intended to be adapted for use in your code, which
+ * may be proprietary. So unlike the library itself, they are licensed
+ * Public Domain.
+ */
+#include "test-server.h"
+
+int close_testing;
+int max_poll_elements;
+int debug_level = 7;
+volatile int force_exit = 0;
+struct lws_context *context;
+struct lws_plat_file_ops fops_plat;
+
+/* http server gets files from this path */
+#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
+char *resource_path = LOCAL_RESOURCE_PATH;
+
+#if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
+char crl_path[1024] = "";
+#endif
+
+/* singlethreaded version --> no locks */
+
+void test_server_lock(int care)
+{
+}
+void test_server_unlock(int care)
+{
+}
+
+/*
+ * This demo server shows how to use libwebsockets for one or more
+ * websocket protocols in the same server
+ *
+ * It defines the following websocket protocols:
+ *
+ * dumb-increment-protocol: once the socket is opened, an incrementing
+ * ascii string is sent down it every 50ms.
+ * If you send "reset\n" on the websocket, then
+ * the incrementing number is reset to 0.
+ *
+ * lws-mirror-protocol: copies any received packet to every connection also
+ * using this protocol, including the sender
+ */
+
+enum demo_protocols {
+ /* always first */
+ PROTOCOL_HTTP = 0,
+
+ PROTOCOL_DUMB_INCREMENT,
+ PROTOCOL_LWS_MIRROR,
+
+ /* always last */
+ DEMO_PROTOCOL_COUNT
+};
+
+/* list of supported protocols and callbacks */
+
+static struct lws_protocols protocols[] = {
+ /* first protocol must always be HTTP handler */
+
+ {
+ "http-only", /* name */
+ callback_http, /* callback */
+ sizeof (struct per_session_data__http), /* per_session_data_size */
+ 0, /* max frame size / rx buffer */
+ },
+ {
+ "dumb-increment-protocol",
+ callback_dumb_increment,
+ sizeof(struct per_session_data__dumb_increment),
+ 10,
+ },
+ {
+ "lws-mirror-protocol",
+ callback_lws_mirror,
+ sizeof(struct per_session_data__lws_mirror),
+ 128,
+ },
+ {
+ "lws-status",
+ callback_lws_status,
+ sizeof(struct per_session_data__lws_status),
+ 128,
+ },
+ { NULL, NULL, 0, 0 } /* terminator */
+};
+
+static const struct lws_extension exts[] = {
+ {
+ "permessage-deflate",
+ lws_extension_callback_pm_deflate,
+ "permessage-deflate; client_no_context_takeover; client_max_window_bits"
+ },
+ {
+ "deflate-frame",
+ lws_extension_callback_pm_deflate,
+ "deflate_frame"
+ },
+ { NULL, NULL, NULL /* terminator */ }
+};
+
+/* this shows how to override the lws file operations. You don't need
+ * to do any of this unless you have a reason (eg, want to serve
+ * compressed files without decompressing the whole archive)
+ */
+static lws_fop_fd_t
+test_server_fops_open(const struct lws_plat_file_ops *fops,
+ const char *vfs_path, const char *vpath,
+ lws_fop_flags_t *flags)
+{
+ lws_fop_fd_t n;
+
+ /* call through to original platform implementation */
+ n = fops_plat.open(fops, vfs_path, vpath, flags);
+
+ lwsl_notice("%s: opening %s, ret %p\n", __func__, vfs_path, n);
+
+ return n;
+}
+
+void signal_cb(evutil_socket_t sock_fd, short events, void *ctx)
+{
+ lwsl_notice("Signal caught, exiting...\n");
+ force_exit = 1;
+ if (events & EV_SIGNAL) {
+ struct event_base *event_base_loop = event_get_base((struct event *) ctx);
+ event_base_loopbreak(event_base_loop);
+ }
+}
+
+static void
+ev_timeout_cb (evutil_socket_t sock_fd, short events, void *ctx)
+{
+ lws_callback_on_writable_all_protocol(context,
+ &protocols[PROTOCOL_DUMB_INCREMENT]);
+}
+
+static struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "debug", required_argument, NULL, 'd' },
+ { "port", required_argument, NULL, 'p' },
+ { "ssl", no_argument, NULL, 's' },
+ { "allow-non-ssl", no_argument, NULL, 'a' },
+ { "interface", required_argument, NULL, 'i' },
+ { "closetest", no_argument, NULL, 'c' },
+ { "libevent", no_argument, NULL, 'e' },
+#ifndef LWS_NO_DAEMONIZE
+ { "daemonize", no_argument, NULL, 'D' },
+#endif
+ { "resource_path", required_argument, NULL, 'r' },
+ { NULL, 0, 0, 0 }
+};
+
+int main(int argc, char **argv)
+{
+ int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE };
+ struct event *signals[ARRAY_SIZE(sigs)];
+ struct event_base *event_base_loop = event_base_new();
+ struct lws_context_creation_info info;
+ char interface_name[128] = "";
+ const char *iface = NULL;
+ struct event *timeout_watcher;
+ char cert_path[1024];
+ char key_path[1024];
+ int use_ssl = 0;
+ int opts = 0;
+ int n = 0;
+#ifndef _WIN32
+ int syslog_options = LOG_PID | LOG_PERROR;
+#endif
+#ifndef LWS_NO_DAEMONIZE
+ int daemonize = 0;
+#endif
+
+ /*
+ * take care to zero down the info struct, he contains random garbaage
+ * from the stack otherwise
+ */
+ memset(&info, 0, sizeof info);
+ info.port = 7681;
+
+ while (n >= 0) {
+ n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
+ if (n < 0)
+ continue;
+ switch (n) {
+ case 'e':
+ opts |= LWS_SERVER_OPTION_LIBEVENT;
+ break;
+#ifndef LWS_NO_DAEMONIZE
+ case 'D':
+ daemonize = 1;
+ #ifndef _WIN32
+ syslog_options &= ~LOG_PERROR;
+ #endif
+ break;
+#endif
+ case 'd':
+ debug_level = atoi(optarg);
+ break;
+ case 's':
+ use_ssl = 1;
+ break;
+ case 'a':
+ opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
+ break;
+ case 'p':
+ info.port = atoi(optarg);
+ break;
+ case 'i':
+ strncpy(interface_name, optarg, sizeof interface_name);
+ interface_name[(sizeof interface_name) - 1] = '\0';
+ iface = interface_name;
+ break;
+ case 'c':
+ close_testing = 1;
+ fprintf(stderr, " Close testing mode -- closes on "
+ "client after 50 dumb increments"
+ "and suppresses lws_mirror spam\n");
+ break;
+ case 'r':
+ resource_path = optarg;
+ printf("Setting resource path to \"%s\"\n", resource_path);
+ break;
+ case 'h':
+ fprintf(stderr, "Usage: test-server "
+ "[--port=<p>] [--ssl] "
+ "[-d <log bitfield>] "
+ "[--resource_path <path>]\n");
+ exit(1);
+ }
+ }
+
+#if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
+ /*
+ * normally lock path would be /var/lock/lwsts or similar, to
+ * simplify getting started without having to take care about
+ * permissions or running as root, set to /tmp/.lwsts-lock
+ */
+ if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
+ fprintf(stderr, "Failed to daemonize\n");
+ return 1;
+ }
+#endif
+
+ for (n = 0; n < ARRAY_SIZE(sigs); n++) {
+ signals[n] = evsignal_new(event_base_loop, sigs[n], signal_cb, event_self_cbarg());
+ evsignal_add(signals[n], NULL);
+ }
+
+#ifndef _WIN32
+ /* we will only try to log things according to our debug_level */
+ setlogmask(LOG_UPTO (LOG_DEBUG));
+ openlog("lwsts", syslog_options, LOG_DAEMON);
+#endif
+
+ /* tell the library what debug level to emit and to send it to syslog */
+ lws_set_log_level(debug_level, lwsl_emit_syslog);
+
+ lwsl_notice("libwebsockets test server libevent - license LGPL2.1+SLE\n");
+ lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
+
+ printf("Using resource path \"%s\"\n", resource_path);
+
+ info.iface = iface;
+ info.protocols = protocols;
+ info.extensions = exts;
+
+ info.ssl_cert_filepath = NULL;
+ info.ssl_private_key_filepath = NULL;
+
+ if (use_ssl) {
+ if (strlen(resource_path) > sizeof(cert_path) - 32) {
+ lwsl_err("resource path too long\n");
+ return -1;
+ }
+ sprintf(cert_path, "%s/libwebsockets-test-server.pem",
+ resource_path);
+ if (strlen(resource_path) > sizeof(key_path) - 32) {
+ lwsl_err("resource path too long\n");
+ return -1;
+ }
+ sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
+ resource_path);
+
+ info.ssl_cert_filepath = cert_path;
+ info.ssl_private_key_filepath = key_path;
+
+ opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
+ }
+ info.gid = -1;
+ info.uid = -1;
+ info.max_http_header_pool = 1;
+ info.options = opts | LWS_SERVER_OPTION_LIBEVENT;
+
+ context = lws_create_context(&info);
+ if (context == NULL) {
+ lwsl_err("libwebsocket init failed\n");
+ return -1;
+ }
+
+ /*
+ * this shows how to override the lws file operations. You don't need
+ * to do any of this unless you have a reason (eg, want to serve
+ * compressed files without decompressing the whole archive)
+ */
+ /* stash original platform fops */
+ fops_plat = *(lws_get_fops(context));
+ /* override the active fops */
+ lws_get_fops(context)->open = test_server_fops_open;
+
+ // Don't use the default Signal Event Watcher & Handler
+ lws_event_sigint_cfg(context, 0, NULL);
+ // Initialize the LWS with libevent loop
+ lws_event_initloop(context, event_base_loop, 0);
+
+ timeout_watcher = evtimer_new(event_base_loop, ev_timeout_cb, NULL);
+ struct timeval tv = {0, 50000};
+ evtimer_add(timeout_watcher, &tv);
+ event_base_dispatch(event_base_loop);
+
+ lws_context_destroy(context);
+ lwsl_notice("libwebsockets-test-server exited cleanly\n");
+
+#ifndef _WIN32
+ closelog();
+#endif
+
+ return 0;
+}