[Libwebsockets] [PATCH v2 3/9] libev: mutilate and prepare for context->event_ops usage

Alejandro Mery amery at geeks.cl
Wed Dec 17 22:20:25 CET 2014


Signed-off-by: Alejandro Mery <alejandro.mery at opensynergy.com>
---
 lib/context.c               |   1 -
 lib/libev.c                 | 131 +++++---------------------------------------
 lib/libwebsockets.h         |  23 ++++----
 lib/lws-plat-unix.c         |   7 ---
 lib/pollfd.c                |   4 --
 lib/private-libwebsockets.h |  67 +++++++---------------
 lib/server.c                |   4 --
 lib/service.c               |   3 -
 lib/ssl.c                   |   5 --
 test-server/test-server.c   |   6 +-
 10 files changed, 50 insertions(+), 201 deletions(-)

diff --git a/lib/context.c b/lib/context.c
index c90e9c4..dafe576 100644
--- a/lib/context.c
+++ b/lib/context.c
@@ -89,7 +89,6 @@ libwebsocket_create_context(struct lws_context_creation_info *info)
 #else
 	lwsl_notice("IPV6 not compiled in\n");
 #endif
-	lws_feature_status_libev(info);
 	lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
 	lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
 
diff --git a/lib/libev.c b/lib/libev.c
index eff7197..8fcc3d7 100644
--- a/lib/libev.c
+++ b/lib/libev.c
@@ -21,68 +21,20 @@
 
 #include "private-libwebsockets.h"
 
-void lws_feature_status_libev(struct lws_context_creation_info *info)
+static int
+libev_init(struct lws_context_creation_info *info,
+	   struct libwebsocket_context *context)
 {
-	if (info->options & LWS_SERVER_OPTION_LIBEV)
-		lwsl_notice("libev support compiled in and enabled\n");
-	else
-		lwsl_notice("libev support compiled in but disabled\n");
-}
-
-static void 
-libwebsocket_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
-{
-	struct libwebsocket_pollfd eventfd;
-	struct lws_io_watcher *lws_io = container_of(watcher, struct lws_io_watcher, watcher);
-	struct libwebsocket_context *context = lws_io->context;
-
-	if (revents & EV_ERROR)
-		return;
-
-	eventfd.fd = watcher->fd;
-	eventfd.revents = EV_NONE;
-	if (revents & EV_READ)
-		eventfd.revents |= LWS_POLLIN;
-
-	if (revents & EV_WRITE)
-		eventfd.revents |= LWS_POLLOUT;
-
-	libwebsocket_service_fd(context, &eventfd);
-}
-
-LWS_VISIBLE void
-libwebsocket_sigint_cb(struct ev_loop *loop,
-		       struct ev_signal *watcher, int revents)
-{
-	ev_break(loop, EVBREAK_ALL);
-}
-
-LWS_VISIBLE int
-libwebsocket_initloop(
-	struct libwebsocket_context *context,
-	struct ev_loop *loop)
-{
-	int status = 0;
+	struct _lws_libev_event_info *evi = &info->e.ev;
+	struct _lws_libev_event_context *evc = &context->e.ev;
 	int backend;
 	const char * backend_name;
-	struct ev_io *w_accept = &context->w_accept.watcher;
-	struct ev_signal *w_sigint = &context->w_sigint.watcher;
 
-	if (!loop)
-		loop = ev_default_loop(0);
+	if (!evi->loop)
+		evi->loop = ev_default_loop(0);
 
-	context->io_loop = loop;
-   
-	/*
-	 * Initialize the accept w_accept with the listening socket
-	 * and register a callback for read operations:
-	 */
-	ev_io_init(w_accept, libwebsocket_accept_cb,
-					context->listen_service_fd, EV_READ);
-	ev_io_start(context->io_loop,w_accept);
-	ev_signal_init(w_sigint, libwebsocket_sigint_cb, SIGINT);
-	ev_signal_start(context->io_loop,w_sigint);
-	backend = ev_backend(loop);
+	evc->loop = evi->loop;
+	backend = ev_backend(evc->loop);
 
 	switch (backend) {
 	case EVBACKEND_SELECT:
@@ -110,66 +62,9 @@ libwebsocket_initloop(
 
 	lwsl_notice(" libev backend: %s\n", backend_name);
 
-	return status;
+	return 0;
 }
 
-LWS_VISIBLE void
-lws_libev_accept(struct libwebsocket_context *context,
-				 struct libwebsocket *new_wsi, int accept_fd)
-{
-	struct ev_io *r = &new_wsi->w_read.watcher;
-	struct ev_io *w = &new_wsi->w_write.watcher;
-
-	if (!LWS_LIBEV_ENABLED(context))
-		return;
-
-        new_wsi->w_read.context = context;
-        new_wsi->w_write.context = context;
-        ev_io_init(r, libwebsocket_accept_cb, accept_fd, EV_READ);
-        ev_io_init(w, libwebsocket_accept_cb, accept_fd, EV_WRITE);
-}
-
-LWS_VISIBLE void
-lws_libev_io(struct libwebsocket_context *context,
-					 struct libwebsocket *wsi, int flags)
-{
-	if (!LWS_LIBEV_ENABLED(context))
-		return;
-
-	if (!context->io_loop)
-		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)
-			ev_io_start(context->io_loop, &wsi->w_write.watcher);
-		if (flags & LWS_EV_READ)
-			ev_io_start(context->io_loop, &wsi->w_read.watcher);
-	} else {
-		if (flags & LWS_EV_WRITE)
-			ev_io_stop(context->io_loop, &wsi->w_write.watcher);
-		if (flags & LWS_EV_READ)
-			ev_io_stop(context->io_loop, &wsi->w_read.watcher);
-	}
-}
-
-LWS_VISIBLE int
-lws_libev_init_fd_table(struct libwebsocket_context *context)
-{
-	if (!LWS_LIBEV_ENABLED(context))
-		return 0;
-
-	context->w_accept.context = context;
-	context->w_sigint.context = context;
-
-	return 1;
-}
-
-LWS_VISIBLE void
-lws_libev_run(struct libwebsocket_context *context)
-{
-	if (context->io_loop && LWS_LIBEV_ENABLED(context))
-		ev_run(context->io_loop, 0);
-}
+struct lws_event_ops lws_libev_event_ops = {
+	.init = libev_init,
+};
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index 684de60..b12f212 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -169,7 +169,6 @@ enum libwebsocket_context_options {
 	LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
 	LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4,
 	LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = 8,
-	LWS_SERVER_OPTION_LIBEV = 16,
 	LWS_SERVER_OPTION_DISABLE_IPV6 = 32,
 	LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = 64,
 };
@@ -1000,6 +999,13 @@ struct lws_event_ops {
 
 LWS_EXTERN struct lws_event_ops lws_poll_event_ops;
 
+#ifdef LWS_USE_LIBEV
+struct _lws_libev_event_info {
+	struct ev_loop *loop;
+};
+LWS_EXTERN struct lws_event_ops lws_libev_event_ops;
+#endif
+
 /**
  * struct lws_context_creation_info: parameters to create context with
  *
@@ -1065,6 +1071,11 @@ struct lws_context_creation_info {
 	unsigned int options;
 	void *user;
 	struct lws_event_ops *event_ops;
+	union {
+#ifdef LWS_USE_LIBEV
+		struct _lws_libev_event_info ev;
+#endif
+	} e;
 	int ka_time;
 	int ka_probes;
 	int ka_interval;
@@ -1135,16 +1146,6 @@ lws_add_http_header_status(struct libwebsocket_context *context,
 
 LWS_EXTERN int lws_http_transaction_completed(struct libwebsocket *wsi);
 
-#ifdef LWS_USE_LIBEV
-LWS_VISIBLE LWS_EXTERN int
-libwebsocket_initloop(
-	struct libwebsocket_context *context, struct ev_loop *loop);
-
-LWS_VISIBLE void
-libwebsocket_sigint_cb(
-	struct ev_loop *loop, struct ev_signal *watcher, int revents);
-#endif /* LWS_USE_LIBEV */
-
 LWS_VISIBLE LWS_EXTERN int
 libwebsocket_service_fd(struct libwebsocket_context *context,
 							 struct libwebsocket_pollfd *pollfd);
diff --git a/lib/lws-plat-unix.c b/lib/lws-plat-unix.c
index 9af4bdf..2140b47 100644
--- a/lib/lws-plat-unix.c
+++ b/lib/lws-plat-unix.c
@@ -103,8 +103,6 @@ lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
 	if (!context)
 		return 1;
 
-	lws_libev_run(context);
-
 	context->service_tid = context->protocols[0].callback(context, NULL,
 				     LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
 
@@ -256,10 +254,6 @@ lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
 LWS_VISIBLE int
 lws_plat_init_fd_tables(struct libwebsocket_context *context)
 {
-	if (lws_libev_init_fd_table(context))
-		/* libev handled it instead */
-		return 0;
-
 	if (pipe(context->dummy_pipe_fds)) {
 		lwsl_err("Unable to create pipe\n");
 		return 1;
@@ -390,7 +384,6 @@ LWS_VISIBLE void
 lws_plat_insert_socket_into_fds(struct libwebsocket_context *context,
 						       struct libwebsocket *wsi)
 {
-	lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
 	context->fds[context->fds_count++].revents = 0;
 }
 
diff --git a/lib/pollfd.c b/lib/pollfd.c
index b09127e..c12ac5b 100644
--- a/lib/pollfd.c
+++ b/lib/pollfd.c
@@ -74,8 +74,6 @@ remove_wsi_socket_from_fds(struct libwebsocket_context *context,
 	int m;
 	struct libwebsocket_pollargs pa = { wsi->sock, 0, 0 };
 
-	lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
-
 	if (!--context->fds_count) {
 		context->protocols[0].callback(context, wsi,
 			LWS_CALLBACK_LOCK_POLL,
@@ -259,8 +257,6 @@ network_sock:
 	if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
 		return -1;
 
-	lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_WRITE);
-
 	return 1;
 }
 
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index 3abac16..1241b1f 100755
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -401,14 +401,8 @@ struct libwebsocket_protocols;
 struct libwebsocket;
 
 #ifdef LWS_USE_LIBEV
-struct lws_io_watcher {
-	struct ev_io watcher;
-	struct libwebsocket_context* context;
-};
-
-struct lws_signal_watcher {
-	struct ev_signal watcher;
-	struct libwebsocket_context* context;
+struct _lws_libev_event_context {
+	struct ev_loop *loop;
 };
 #endif /* LWS_USE_LIBEV */
 
@@ -422,14 +416,12 @@ struct libwebsocket_context {
 	struct libwebsocket_pollfd *fds;
 	struct libwebsocket **lws_lookup; /* fd to wsi */
 	int fds_count;
-#ifdef LWS_USE_LIBEV
-	struct ev_loop* io_loop;
-	struct lws_io_watcher w_accept;
-	struct lws_signal_watcher w_sigint;
-#endif /* LWS_USE_LIBEV */
 	int max_fds;
 
 	union {
+#ifdef LWS_USE_LIBEV
+		struct _lws_libev_event_context ev;
+#endif /* LWS_USE_LIBEV */
 #ifdef _WIN32
 		struct _lws_win_poll_event_context poll;
 #endif
@@ -497,36 +489,6 @@ struct libwebsocket_context {
 	void *user_space;
 };
 
-enum {
-	LWS_EV_READ = (1 << 0),
-	LWS_EV_WRITE = (1 << 1),
-	LWS_EV_START = (1 << 2),
-	LWS_EV_STOP = (1 << 3),
-};
-
-#ifdef LWS_USE_LIBEV
-#define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
-LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
-LWS_EXTERN void
-lws_libev_accept(struct libwebsocket_context *context,
-		 struct libwebsocket *new_wsi, int accept_fd);
-LWS_EXTERN void
-lws_libev_io(struct libwebsocket_context *context,
-				struct libwebsocket *wsi, int flags);
-LWS_EXTERN int
-lws_libev_init_fd_table(struct libwebsocket_context *context);
-LWS_EXTERN void
-lws_libev_run(struct libwebsocket_context *context);
-#else
-#define LWS_LIBEV_ENABLED(context) (0)
-#define lws_feature_status_libev(_a) \
-			lwsl_notice("libev support not compiled in\n")
-#define lws_libev_accept(_a, _b, _c) ((void) 0)
-#define lws_libev_io(_a, _b, _c) ((void) 0)
-#define lws_libev_init_fd_table(_a) (0)
-#define lws_libev_run(_a) ((void) 0)
-#endif
-
 #ifdef LWS_USE_IPV6
 #define LWS_IPV6_ENABLED(context) (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
 #else
@@ -787,14 +749,19 @@ struct _lws_websocket_related {
 	unsigned int ping_payload_len; /* nonzero if PONG pending */
 };
 
+#ifdef LWS_USE_LIBEV
+struct _lws_libev_event_related {
+	struct libwebsocket_context *context;
+
+	struct ev_io w_read;
+	struct ev_io w_write;
+};
+#endif /* LWS_USE_LIBEV */
+
 struct libwebsocket {
 
 	/* lifetime members */
 
-#ifdef LWS_USE_LIBEV
-    struct lws_io_watcher w_read;
-    struct lws_io_watcher w_write;
-#endif /* LWS_USE_LIBEV */
 	const struct libwebsocket_protocols *protocol;
 #ifndef LWS_NO_EXTENSIONS
 	struct libwebsocket_extension *
@@ -849,6 +816,12 @@ struct libwebsocket {
 		struct _lws_websocket_related ws;
 	} u;
 
+	union e {
+#ifdef LWS_USE_LIBEV
+		struct _lws_libev_event_related ev;
+#endif /* LWS_USE_LIBEV */
+	} e;
+
 #ifdef LWS_OPENSSL_SUPPORT
 	SSL *ssl;
 	BIO *client_bio;
diff --git a/lib/server.c b/lib/server.c
index ed5c47b..13097c7 100644
--- a/lib/server.c
+++ b/lib/server.c
@@ -730,8 +730,6 @@ try_pollout:
 		/* one shot */
 		if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
 			goto fail;
-		
-		lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_WRITE);
 
 		if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE) {
 			n = user_callback_handle_rxflow(
@@ -815,8 +813,6 @@ try_pollout:
 		(context->protocols[0].callback)(context, new_wsi,
 			LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED, NULL, NULL, 0);
 
-		lws_libev_accept(context, new_wsi, accept_fd);
-
 		if (!LWS_SSL_ENABLED(context)) {
 			lwsl_debug("accepted new conn  port %u on fd=%d\n",
 					  ntohs(cli_addr.sin_port), accept_fd);
diff --git a/lib/service.c b/lib/service.c
index f905a60..710f458 100644
--- a/lib/service.c
+++ b/lib/service.c
@@ -204,8 +204,6 @@ user_service:
 			lwsl_info("failled at set pollfd\n");
 			return 1;
 		}
-
-		lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_WRITE);
 	}
 
 #ifdef LWS_USE_HTTP2
@@ -359,7 +357,6 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
 
 	time(&now);
 
-	/* TODO: if using libev, we should probably use timeout watchers... */
 	if (context->last_timeout_check_s != now) {
 		context->last_timeout_check_s = now;
 
diff --git a/lib/ssl.c b/lib/ssl.c
index cfee6de..9125ac5 100644
--- a/lib/ssl.c
+++ b/lib/ssl.c
@@ -540,8 +540,6 @@ lws_server_socket_service_ssl(struct libwebsocket_context *context,
 		if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
 			goto fail;
 
-		lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_WRITE);
-
 		lws_latency_pre(context, wsi);
 
 		n = recv(wsi->sock, context->service_buffer,
@@ -589,8 +587,6 @@ lws_server_socket_service_ssl(struct libwebsocket_context *context,
 			if (lws_change_pollfd(wsi, 0, LWS_POLLIN))
 				goto fail;
 
-			lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
-
 			lwsl_info("SSL_ERROR_WANT_READ\n");
 			break;
 		}
@@ -598,7 +594,6 @@ lws_server_socket_service_ssl(struct libwebsocket_context *context,
 			if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
 				goto fail;
 
-			lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_WRITE);
 			break;
 		}
 		lwsl_debug("SSL_accept failed skt %u: %s\n",
diff --git a/test-server/test-server.c b/test-server/test-server.c
index 567f545..079eeca 100644
--- a/test-server/test-server.c
+++ b/test-server/test-server.c
@@ -788,6 +788,7 @@ int main(int argc, char **argv)
 #endif
 	unsigned int ms, oldms = 0;
 	struct lws_context_creation_info info;
+	struct lws_event_ops *event_ops = NULL;
 
 	int debug_level = 7;
 #ifndef LWS_NO_DAEMONIZE
@@ -803,7 +804,9 @@ int main(int argc, char **argv)
 			continue;
 		switch (n) {
 		case 'e':
-			opts |= LWS_SERVER_OPTION_LIBEV;
+#ifdef LWS_USE_LIBEV
+			event_ops = &lws_libev_event_ops;
+#endif
 			break;
 #ifndef LWS_NO_DAEMONIZE
 		case 'D':
@@ -915,6 +918,7 @@ int main(int argc, char **argv)
 	info.gid = -1;
 	info.uid = -1;
 	info.options = opts;
+	info.event_ops = event_ops;
 
 	context = libwebsocket_create_context(&info);
 	if (context == NULL) {
-- 
2.2.0




More information about the Libwebsockets mailing list