[Libwebsockets] [PATCH 05/11] select context->event_ops based on info->event

Andy Green andy at warmcat.com
Tue Dec 16 01:19:03 CET 2014



On 16 December 2014 07:36:26 GMT+08:00, Alejandro Mery <amery at geeks.cl> wrote:
>Signed-off-by: Alejandro Mery <amery at geeks.cl>
>---
> CMakeLists.txt              |  2 ++
> lib/context.c               | 22 ++++++++++++++++++++++
> lib/event-poll-unix.c       |  4 ++++
> lib/event-poll-win.c        |  4 ++++
> lib/private-libwebsockets.h |  5 +++++
> 5 files changed, 37 insertions(+)
> create mode 100644 lib/event-poll-unix.c
> create mode 100644 lib/event-poll-win.c
>
>diff --git a/CMakeLists.txt b/CMakeLists.txt
>index 75a29cd..b395153 100644
>--- a/CMakeLists.txt
>+++ b/CMakeLists.txt
>@@ -306,10 +306,12 @@ endif()
> if (WIN32)
> 	list(APPEND SOURCES
> 		lib/lws-plat-win.c
>+		lib/event-poll-win.c
> 		)
> else()
> 	list(APPEND SOURCES
> 		lib/lws-plat-unix.c
>+		lib/event-poll-unix.c
> 		)
> endif()
> 
>diff --git a/lib/context.c b/lib/context.c
>index 9e1ebf2..884849e 100644
>--- a/lib/context.c
>+++ b/lib/context.c
>@@ -106,6 +106,28 @@ libwebsocket_create_context(struct
>lws_context_creation_info *info)
> 		return NULL;
> 	}
> 
>+	switch (info->event) {
>+#ifdef LWS_USE_LIBEV
>+	case LWS_EVENT_LIBEV:
>+		context->event_ops = &lws_libev_event_ops;
>+		break;
>+#endif
>+	case LWS_EVENT_POLL:
>+		context->event_ops = &lws_poll_event_ops;
>+		break;
>+	default:
>+		lwsl_err("Events backend %d not supported\n",
>+			 info->event);
>+		lws_free(context);
>+		return NULL;
>+	}

Really the above doesn't "do" anything extra compared to

 - publicly export lws_*_event_ops

 - info struct lets the user code pass one in directly

 - default to the poll one if NULL, so people aren't forced to deal with it at all

There's also the advantage that depending on which ops end up in the ops struct, the user can do eg, external poll loop support by passing in his own custom event_ops struct without needing any knowledge in the library.

You can also eliminate the whole info->event and enum thing that way.

>+	if (context->event_ops->init &&
>+	    !context->event_ops->init(info, context)) {
>+		lws_free(context);
>+		return NULL;
>+	}
>+
> 	if (pid_daemon) {
> 		context->started_with_parent = pid_daemon;
> 		lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
>diff --git a/lib/event-poll-unix.c b/lib/event-poll-unix.c
>new file mode 100644
>index 0000000..7ccc6a5
>--- /dev/null
>+++ b/lib/event-poll-unix.c
>@@ -0,0 +1,4 @@
>+#include "private-libwebsockets.h"
>+
>+struct lws_event_ops lws_poll_event_ops = {
>+};
>diff --git a/lib/event-poll-win.c b/lib/event-poll-win.c
>new file mode 100644
>index 0000000..7ccc6a5
>--- /dev/null
>+++ b/lib/event-poll-win.c
>@@ -0,0 +1,4 @@
>+#include "private-libwebsockets.h"
>+
>+struct lws_event_ops lws_poll_event_ops = {
>+};
>diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
>index d66f430..5a84408 100755
>--- a/lib/private-libwebsockets.h
>+++ b/lib/private-libwebsockets.h
>@@ -405,6 +405,11 @@ struct lws_event_ops {
> 		    struct libwebsocket_context *context);
> };
> 
>+extern struct lws_event_ops lws_poll_event_ops;
>+#ifdef LWS_USE_LIBEV
>+extern struct lws_event_ops lws_libev_event_ops;
>+#endif
>+
> #ifdef LWS_USE_LIBEV
> struct _lws_libev_event_context {
> 	struct ev_loop *loop;




More information about the Libwebsockets mailing list