[Libwebsockets] [PATCH v2 5/9] move lws_plat_service() implementations to context->event_ops->service()

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


Signed-off-by: Alejandro Mery <alejandro.mery at opensynergy.com>
---
 lib/event-poll-unix.c       | 87 +++++++++++++++++++++++++++++++++++++++++++++
 lib/event-poll-win.c        | 68 +++++++++++++++++++++++++++++++++++
 lib/libwebsockets.h         |  3 ++
 lib/lws-plat-unix.c         | 86 --------------------------------------------
 lib/lws-plat-win.c          | 67 ----------------------------------
 lib/private-libwebsockets.h |  2 --
 lib/service.c               |  5 ++-
 7 files changed, 162 insertions(+), 156 deletions(-)

diff --git a/lib/event-poll-unix.c b/lib/event-poll-unix.c
index 7ccc6a5..e043a2f 100644
--- a/lib/event-poll-unix.c
+++ b/lib/event-poll-unix.c
@@ -1,4 +1,91 @@
 #include "private-libwebsockets.h"
 
+static int
+poll_service(struct libwebsocket_context *context, int timeout_ms)
+{
+	int n;
+	int m;
+	char buf;
+
+	/* stay dead once we are dead */
+
+	if (!context)
+		return 1;
+
+	context->service_tid = context->protocols[0].callback(context, NULL,
+				     LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
+
+#ifdef LWS_OPENSSL_SUPPORT
+	/* if we know we have non-network pending data, do not wait in poll */
+	if (context->ssl_flag_buffered_reads)
+		timeout_ms = 0;
+#endif
+	n = poll(context->fds, context->fds_count, timeout_ms);
+	context->service_tid = 0;
+
+#ifdef LWS_OPENSSL_SUPPORT
+	if (!context->ssl_flag_buffered_reads && n == 0) {
+#else
+	if (n == 0) /* poll timeout */ {
+#endif
+		libwebsocket_service_fd(context, NULL);
+		return 0;
+	}
+
+#ifdef LWS_OPENSSL_SUPPORT
+	/* any more will have to set it fresh this time around */
+	context->ssl_flag_buffered_reads = 0;
+#endif
+
+	if (n < 0) {
+		if (LWS_ERRNO != LWS_EINTR)
+			return -1;
+		return 0;
+	}
+
+	/* any socket with events to service? */
+
+	for (n = 0; n < context->fds_count; n++) {
+#ifdef LWS_OPENSSL_SUPPORT
+		struct libwebsocket *wsi;
+
+		wsi = context->lws_lookup[context->fds[n].fd];
+		if (wsi == NULL)
+			continue;
+		/*
+		 * if he's not flowcontrolled, make sure we service ssl
+		 * pending read data
+		 */
+		if (wsi->ssl && wsi->buffered_reads_pending) {
+			lwsl_debug("wsi %p: forcing POLLIN\n", wsi);
+			context->fds[n].revents |= context->fds[n].events & POLLIN;
+			if (context->fds[n].revents & POLLIN)
+				wsi->buffered_reads_pending = 0;
+			else
+				/* somebody left with pending SSL read data */
+				context->ssl_flag_buffered_reads = 1;
+		}
+#endif
+		if (!context->fds[n].revents)
+			continue;
+
+		if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
+			if (read(context->fds[n].fd, &buf, 1) != 1)
+				lwsl_err("Cannot read from dummy pipe.");
+			continue;
+		}
+
+		m = libwebsocket_service_fd(context, &context->fds[n]);
+		if (m < 0)
+			return -1;
+		/* if something closed, retry this slot */
+		if (m)
+			n--;
+	}
+
+	return 0;
+}
+
 struct lws_event_ops lws_poll_event_ops = {
+	.service = poll_service,
 };
diff --git a/lib/event-poll-win.c b/lib/event-poll-win.c
index 7ccc6a5..3a86955 100644
--- a/lib/event-poll-win.c
+++ b/lib/event-poll-win.c
@@ -1,4 +1,72 @@
 #include "private-libwebsockets.h"
 
+static int
+poll_service(struct libwebsocket_context *context, int timeout_ms)
+{
+	int n;
+	int i;
+	DWORD ev;
+	WSANETWORKEVENTS networkevents;
+	struct libwebsocket_pollfd *pfd;
+
+	/* stay dead once we are dead */
+
+	if (context == NULL)
+		return 1;
+
+	context->service_tid = context->protocols[0].callback(context, NULL,
+				     LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
+
+	for (i = 0; i < context->fds_count; ++i) {
+		pfd = &context->fds[i];
+		if (pfd->fd == context->listen_service_fd)
+			continue;
+
+		if (pfd->events & LWS_POLLOUT) {
+			if (context->lws_lookup[pfd->fd]->sock_send_blocking)
+				continue;
+			pfd->revents = LWS_POLLOUT;
+			n = libwebsocket_service_fd(context, pfd);
+			if (n < 0)
+				return n;
+		}
+	}
+
+	ev = WSAWaitForMultipleEvents(context->fds_count + 1,
+				     context->e.poll.events, FALSE, timeout_ms, FALSE);
+	context->service_tid = 0;
+
+	if (ev == WSA_WAIT_TIMEOUT) {
+		libwebsocket_service_fd(context, NULL);
+		return 0;
+	}
+
+	if (ev == WSA_WAIT_EVENT_0) {
+		WSAResetEvent(context->e.poll.events[0]);
+		return 0;
+	}
+
+	if (ev < WSA_WAIT_EVENT_0 || ev > WSA_WAIT_EVENT_0 + context->fds_count)
+		return -1;
+
+	pfd = &context->fds[ev - WSA_WAIT_EVENT_0 - 1];
+
+	if (WSAEnumNetworkEvents(pfd->fd,
+			context->e.poll.events[ev - WSA_WAIT_EVENT_0],
+					      &networkevents) == SOCKET_ERROR) {
+		lwsl_err("WSAEnumNetworkEvents() failed with error %d\n",
+								     LWS_ERRNO);
+		return -1;
+	}
+
+	pfd->revents = networkevents.lNetworkEvents;
+
+	if (pfd->revents & LWS_POLLOUT)
+		context->lws_lookup[pfd->fd]->sock_send_blocking = FALSE;
+
+	return libwebsocket_service_fd(context, pfd);
+}
+
 struct lws_event_ops lws_poll_event_ops = {
+	.service = poll_service,
 };
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index b12f212..cf220eb 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -995,6 +995,9 @@ struct libwebsocket_extension {
 struct lws_event_ops {
 	int (*init)(struct lws_context_creation_info *info,
 		    struct libwebsocket_context *context);
+
+	int (*service)(struct libwebsocket_context *context,
+		       int timeout_ms);
 };
 
 LWS_EXTERN struct lws_event_ops lws_poll_event_ops;
diff --git a/lib/lws-plat-unix.c b/lib/lws-plat-unix.c
index 2140b47..a0f1104 100644
--- a/lib/lws-plat-unix.c
+++ b/lib/lws-plat-unix.c
@@ -92,92 +92,6 @@ LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
 }
 
 LWS_VISIBLE int
-lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
-{
-	int n;
-	int m;
-	char buf;
-
-	/* stay dead once we are dead */
-
-	if (!context)
-		return 1;
-
-	context->service_tid = context->protocols[0].callback(context, NULL,
-				     LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
-
-#ifdef LWS_OPENSSL_SUPPORT
-	/* if we know we have non-network pending data, do not wait in poll */
-	if (context->ssl_flag_buffered_reads)
-		timeout_ms = 0;
-#endif
-	n = poll(context->fds, context->fds_count, timeout_ms);
-	context->service_tid = 0;
-
-#ifdef LWS_OPENSSL_SUPPORT
-	if (!context->ssl_flag_buffered_reads && n == 0) {
-#else
-	if (n == 0) /* poll timeout */ {
-#endif
-		libwebsocket_service_fd(context, NULL);
-		return 0;
-	}
-	
-#ifdef LWS_OPENSSL_SUPPORT
-	/* any more will have to set it fresh this time around */
-	context->ssl_flag_buffered_reads = 0;
-#endif
-
-	if (n < 0) {
-		if (LWS_ERRNO != LWS_EINTR)
-			return -1;
-		return 0;
-	}
-
-	/* any socket with events to service? */
-
-	for (n = 0; n < context->fds_count; n++) {
-#ifdef LWS_OPENSSL_SUPPORT
-		struct libwebsocket *wsi;
-		
-		wsi = context->lws_lookup[context->fds[n].fd];
-		if (wsi == NULL)
-			continue;
-		/* 
-		 * if he's not flowcontrolled, make sure we service ssl
-		 * pending read data
-		 */
-		if (wsi->ssl && wsi->buffered_reads_pending) {
-			lwsl_debug("wsi %p: forcing POLLIN\n", wsi);
-			context->fds[n].revents |= context->fds[n].events & POLLIN;
-			if (context->fds[n].revents & POLLIN)
-				wsi->buffered_reads_pending = 0;
-			else
-				/* somebody left with pending SSL read data */
-				context->ssl_flag_buffered_reads = 1;
-		}
-#endif
-		if (!context->fds[n].revents)
-			continue;
-
-		if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
-			if (read(context->fds[n].fd, &buf, 1) != 1)
-				lwsl_err("Cannot read from dummy pipe.");
-			continue;
-		}
-
-		m = libwebsocket_service_fd(context, &context->fds[n]);
-		if (m < 0)
-			return -1;
-		/* if something closed, retry this slot */
-		if (m)
-			n--;
-	}
-
-	return 0;
-}
-
-LWS_VISIBLE int
 lws_plat_set_socket_options(struct libwebsocket_context *context, int fd)
 {
 	int optval = 1;
diff --git a/lib/lws-plat-win.c b/lib/lws-plat-win.c
index ede9318..88c04fb 100644
--- a/lib/lws-plat-win.c
+++ b/lib/lws-plat-win.c
@@ -82,73 +82,6 @@ LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
 }
 
 LWS_VISIBLE int
-lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
-{
-	int n;
-	int i;
-	DWORD ev;
-	WSANETWORKEVENTS networkevents;
-	struct libwebsocket_pollfd *pfd;
-
-	/* stay dead once we are dead */
-
-	if (context == NULL)
-		return 1;
-
-	context->service_tid = context->protocols[0].callback(context, NULL,
-				     LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
-
-	for (i = 0; i < context->fds_count; ++i) {
-		pfd = &context->fds[i];
-		if (pfd->fd == context->listen_service_fd)
-			continue;
-
-		if (pfd->events & LWS_POLLOUT) {
-			if (context->lws_lookup[pfd->fd]->sock_send_blocking)
-				continue;
-			pfd->revents = LWS_POLLOUT;
-			n = libwebsocket_service_fd(context, pfd);
-			if (n < 0)
-				return n;
-		}
-	}
-
-	ev = WSAWaitForMultipleEvents(context->fds_count + 1,
-				     context->e.poll.events, FALSE, timeout_ms, FALSE);
-	context->service_tid = 0;
-
-	if (ev == WSA_WAIT_TIMEOUT) {
-		libwebsocket_service_fd(context, NULL);
-		return 0;
-	}
-
-	if (ev == WSA_WAIT_EVENT_0) {
-		WSAResetEvent(context->e.poll.events[0]);
-		return 0;
-	}
-
-	if (ev < WSA_WAIT_EVENT_0 || ev > WSA_WAIT_EVENT_0 + context->fds_count)
-		return -1;
-
-	pfd = &context->fds[ev - WSA_WAIT_EVENT_0 - 1];
-
-	if (WSAEnumNetworkEvents(pfd->fd,
-			context->e.poll.events[ev - WSA_WAIT_EVENT_0],
-					      &networkevents) == SOCKET_ERROR) {
-		lwsl_err("WSAEnumNetworkEvents() failed with error %d\n",
-								     LWS_ERRNO);
-		return -1;
-	}
-
-	pfd->revents = networkevents.lNetworkEvents;
-
-	if (pfd->revents & LWS_POLLOUT)
-		context->lws_lookup[pfd->fd]->sock_send_blocking = FALSE;
-
-	return libwebsocket_service_fd(context, pfd);
-}
-
-LWS_VISIBLE int
 lws_plat_set_socket_options(struct libwebsocket_context *context, int fd)
 {
 	int optval = 1;
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index 1241b1f..27abc89 100755
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -1185,8 +1185,6 @@ lws_plat_context_late_destroy(struct libwebsocket_context *context);
 LWS_EXTERN int
 lws_poll_listen_fd(struct libwebsocket_pollfd *fd);
 LWS_EXTERN int
-lws_plat_service(struct libwebsocket_context *context, int timeout_ms);
-LWS_EXTERN int
 lws_plat_init_fd_tables(struct libwebsocket_context *context);
 LWS_EXTERN void
 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
diff --git a/lib/service.c b/lib/service.c
index 710f458..c0cb8ee 100644
--- a/lib/service.c
+++ b/lib/service.c
@@ -628,6 +628,9 @@ handled:
 LWS_VISIBLE int
 libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
 {
-	return lws_plat_service(context, timeout_ms);
+	assert(context && context->event_ops);
+	assert(context->event_ops->service);
+
+	return context->event_ops->service(context, timeout_ms);
 }
 
-- 
2.2.0




More information about the Libwebsockets mailing list