bmsd/websocket.c

changeset 678
14322825cb3d
parent 677
6e82fece1f8f
child 679
48f8f3fce7c0
--- a/bmsd/websocket.c	Tue May 12 16:08:30 2020 +0200
+++ b/bmsd/websocket.c	Thu May 14 14:38:20 2020 +0200
@@ -27,6 +27,7 @@
 #include "bms.h"
 #include "xutil.h"
 #include "websocket.h"
+#include "co2meters.h"
 #include <libwebsockets.h>
 
 
@@ -64,7 +65,8 @@
 static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
 {
     struct per_session_data__lws_mirror *pss = (struct per_session_data__lws_mirror *)user;
-    int n, m;
+    int		n, m;
+    char	buf[513];
 
     switch (reason) {
 
@@ -110,31 +112,14 @@
                 break;
 
 	case LWS_CALLBACK_RECEIVE:
-                if (((ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
-                    syslog(LOG_NOTICE, "ws: dropping!");
-                    goto choke;
-                }
-
-                if (ringbuffer[ringbuffer_head].payload)
-                    free(ringbuffer[ringbuffer_head].payload);
 
-                ringbuffer[ringbuffer_head].payload = malloc(LWS_PRE + len);
-                ringbuffer[ringbuffer_head].len = len;
-                memcpy((char *)ringbuffer[ringbuffer_head].payload + LWS_PRE, in, len);
-                if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
-                    ringbuffer_head = 0;
-                else
-                    ringbuffer_head++;
+		memcpy(buf, in, len);
+		buf[len] = '\0';
+		syslog(LOG_NOTICE, "ws: reveived %ld bytes %s", len, buf);
+		if (strncmp(buf, (char *)"{\"device\":\"co2meters\",", 22) == 0) {
+		    co2meter_ws_receive(buf);
+		}
 
-                if (((ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
-                    goto done;
-
-choke:
-                syslog(LOG_NOTICE, "ws: LWS_CALLBACK_RECEIVE: throttling");
-                lws_rx_flow_control(wsi, 0);
-
-done:
-                lws_callback_on_writable_all_protocol(lws_get_context(wsi), lws_get_protocol(wsi));
                 break;
 
 	case LWS_CALLBACK_CLOSED:
@@ -152,7 +137,7 @@
 
 
 static struct lws_protocols protocols[] = {
-	{ "bmsd-protocol", callback_ws, sizeof(struct per_session_data__lws_mirror), 128 },
+	{ "bmsd-protocol", callback_ws, sizeof(struct per_session_data__lws_mirror), 512 },
         { NULL, NULL, 0, 0 } /* terminator */
 };
 

mercurial