diff mbox series

[6/6] Webserver: write to IPC in non blocking mode

Message ID 20191208122844.11642-6-sbabic@denx.de
State Accepted
Headers show
Series [1/6] Updated mongoose to 6.16 | expand

Commit Message

Stefano Babic Dec. 8, 2019, 12:28 p.m. UTC
In blocking mode, additional sockets are not processed by the Webserver
event handler until the handler blocks. This forbids the ping / pong
mechanismus in websockets. Switch to non-blocking mode because new
Webserver version allows it.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 mongoose/mongoose_interface.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/mongoose/mongoose_interface.c b/mongoose/mongoose_interface.c
index 3296bc3..2b71119 100644
--- a/mongoose/mongoose_interface.c
+++ b/mongoose/mongoose_interface.c
@@ -292,6 +292,7 @@  static void upload_handler(struct mg_connection *nc, int ev, void *p)
 {
 	struct mg_http_multipart_part *mp;
 	struct file_upload_state *fus;
+	ssize_t written;
 
 	switch (ev) {
 	case MG_EV_HTTP_PART_BEGIN:
@@ -310,6 +311,10 @@  static void upload_handler(struct mg_connection *nc, int ev, void *p)
 			break;
 		}
 
+		if (swupdate_file_setnonblock(fus->fd, true)) {
+			WARN("IPC cannot be set in non-blocking, fallback to block mode");
+		}
+
 		mp->user_data = fus;
 
 		/*
@@ -333,8 +338,22 @@  static void upload_handler(struct mg_connection *nc, int ev, void *p)
 		if (!fus)
 			break;
 
-		ipc_send_data(fus->fd, (char *) mp->data.p, mp->data.len);
-		fus->len += mp->data.len;
+		written = write(fus->fd, (char *) mp->data.p, mp->data.len);
+		/*
+		 * IPC seems to block, wait for a while
+		 */
+		if (written != mp->data.len) {
+			if (errno != EAGAIN && errno != EWOULDBLOCK) {
+				ERROR("Writing to IPC fails due to %s", strerror(errno));
+			}
+			usleep(100);
+
+			if (written < 0)
+				written = 0;
+		}
+
+		mp->num_data_consumed = written;
+		fus->len += written;
 
 		break;