]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/commitdiff
data stored in context
authorJiří Matěják <jiri.matejak@fel.cvut.cz>
Tue, 22 May 2018 08:42:21 +0000 (10:42 +0200)
committerJiří Matěják <jiri.matejak@fel.cvut.cz>
Tue, 22 May 2018 08:42:21 +0000 (10:42 +0200)
mt_server.c

index cb5ba026a70d29639ddaa01f3329b07b3592f867..567c3af6fe08d2dd2c53bf59aa0d27e48e09b637 100644 (file)
@@ -143,6 +143,7 @@ static int callback_merica_terminal(struct lws *wsi,
                                     enum lws_callback_reasons reason,
                                     void *user, void *in, size_t len)
 {
+    struct lws_context *context = lws_get_context(wsi);
     struct lws_vhost *vhost = lws_get_vhost(wsi);
     const struct lws_protocols *prot = lws_get_protocol(wsi);
     /*
@@ -155,7 +156,7 @@ static int callback_merica_terminal(struct lws *wsi,
 
     int n, m;
 
-    list *lines = (list *)prot->user;
+    list *lines = (list *)lws_context_user(context);
     char *line;
 
     switch (reason) {
@@ -164,7 +165,7 @@ static int callback_merica_terminal(struct lws *wsi,
                       vhost, prot,
                       sizeof(struct per_vhost_data__merica_terminal)
                   );
-            vhd->context = lws_get_context(wsi);
+            vhd->context = context;
             vhd->protocol = prot;
             vhd->vhost = vhost;
             break;
@@ -185,7 +186,7 @@ static int callback_merica_terminal(struct lws *wsi,
                 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
                 list_remove(lines);
                 if (lines->first) {
-                    lws_callback_on_writable_all_protocol(lws_get_context(wsi),
+                    lws_callback_on_writable_all_protocol(context,
                         &protocols[PROTOCOL_MERICA_TERMINAL]);
                 }
             } else {
@@ -209,7 +210,7 @@ static int callback_merica_terminal(struct lws *wsi,
                 line = copy_line(JSON_EMPTY);
                 if (line) {
                     if (list_add(lines, line) == 0) {
-                        lws_callback_on_writable_all_protocol(lws_get_context(wsi),
+                        lws_callback_on_writable_all_protocol(context,
                             &protocols[PROTOCOL_MERICA_TERMINAL]);
                     } else {
                         free_line(line);
@@ -259,16 +260,23 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
 {
     struct lws_context_creation_info info;
 
+    list *l = list_init();
+    if (!l) {
+        return -1;
+    }
+
     memset(&info, 0, sizeof(info));
     info.port = HTTP_PORT;
     info.mounts = &mount;
     info.protocols = protocols;
     info.max_http_header_pool = 1;
     info.options |= LWS_SERVER_OPTION_LIBEV;
+    info.user = l;
 
     struct lws_context *context = lws_create_context(&info);
     if (!context) {
         fprintf(stderr, "lws_create_context failed\n");
+        list_deinit(l);
         return -1;
     }
 
@@ -276,14 +284,11 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
 
     ev_io_ws *w = &(self->fd_watcher);
     w->context = context;
-    w->lines = list_init();
-    if (!w->lines) {
-        return -1;
-    }
-    protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->lines;
+    w->lines = l;
+
     char *line = new_line();
     if (!line) {
-        list_deinit(w->lines);
+        list_deinit(l);
         return -1;
     }
     w->text = line;