From 548c2febecb299f6afdf3096cccb544b31f22884 Mon Sep 17 00:00:00 2001 From: cpq Date: Sat, 18 Apr 2026 05:55:34 +0100 Subject: [PATCH] Keep struct mg_dash --- mongoose.h | 16 ++++++++++------ src/dash.h | 16 ++++++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/mongoose.h b/mongoose.h index 14e2107f..6c77d810 100644 --- a/mongoose.h +++ b/mongoose.h @@ -3157,6 +3157,10 @@ struct mg_field { void (*set)(const union mg_val *); }; +struct mg_dash { + struct mg_field *fields; +}; + static inline struct mg_str trimq(struct mg_str s) { // Trim double quotes if (s.len > 1 && s.buf[0] == '"') s.len -= 2, s.buf++; return s; @@ -3302,14 +3306,14 @@ static inline void mg_dash_apply(struct mg_connection *c, struct mg_str json, static inline void mg_dash_process_msg(struct mg_connection *c, struct mg_ws_message *wm, - struct mg_field *fields) { + struct mg_dash *dash) { struct mg_str req = wm->data; struct mg_str method = trimq(mg_json_get_tok(req, "$.method")); if (mg_match(method, mg_str("get"), NULL)) { - mg_dash_success(c, req, "%M", mg_dash_print, &req, fields); + mg_dash_success(c, req, "%M", mg_dash_print, &req, dash->fields); } else if (mg_match(method, mg_str("set"), NULL)) { struct mg_str params = trimq(mg_json_get_tok(req, "$.params")); - mg_dash_apply(c, params, fields); + mg_dash_apply(c, params, dash->fields); mg_dash_success(c, req, "%s", "true"); } else { mg_dash_error(c, req, "%s", "unknown method"); @@ -3331,12 +3335,12 @@ static inline void mg_dash_ev_handler(struct mg_connection *c, int ev, } } else if (ev == MG_EV_WS_MSG) { // Add this to automatically handle "get" and "set" JSON-RPC calls - struct mg_field *fields = (struct mg_field *) c->fn_data; + struct mg_dash *dash = (struct mg_dash *) c->fn_data; struct mg_ws_message *wm = (struct mg_ws_message *) ev_data; - if (fields == NULL) { + if (dash == NULL || dash->fields == NULL) { mg_dash_error(c, wm->data, "%s", "no fields defined"); } else { - mg_dash_process_msg(c, wm, fields); + mg_dash_process_msg(c, wm, dash); } } } diff --git a/src/dash.h b/src/dash.h index 8ba2d8cb..cc0a60d9 100644 --- a/src/dash.h +++ b/src/dash.h @@ -19,6 +19,10 @@ struct mg_field { void (*set)(const union mg_val *); }; +struct mg_dash { + struct mg_field *fields; +}; + static inline struct mg_str trimq(struct mg_str s) { // Trim double quotes if (s.len > 1 && s.buf[0] == '"') s.len -= 2, s.buf++; return s; @@ -164,14 +168,14 @@ static inline void mg_dash_apply(struct mg_connection *c, struct mg_str json, static inline void mg_dash_process_msg(struct mg_connection *c, struct mg_ws_message *wm, - struct mg_field *fields) { + struct mg_dash *dash) { struct mg_str req = wm->data; struct mg_str method = trimq(mg_json_get_tok(req, "$.method")); if (mg_match(method, mg_str("get"), NULL)) { - mg_dash_success(c, req, "%M", mg_dash_print, &req, fields); + mg_dash_success(c, req, "%M", mg_dash_print, &req, dash->fields); } else if (mg_match(method, mg_str("set"), NULL)) { struct mg_str params = trimq(mg_json_get_tok(req, "$.params")); - mg_dash_apply(c, params, fields); + mg_dash_apply(c, params, dash->fields); mg_dash_success(c, req, "%s", "true"); } else { mg_dash_error(c, req, "%s", "unknown method"); @@ -193,12 +197,12 @@ static inline void mg_dash_ev_handler(struct mg_connection *c, int ev, } } else if (ev == MG_EV_WS_MSG) { // Add this to automatically handle "get" and "set" JSON-RPC calls - struct mg_field *fields = (struct mg_field *) c->fn_data; + struct mg_dash *dash = (struct mg_dash *) c->fn_data; struct mg_ws_message *wm = (struct mg_ws_message *) ev_data; - if (fields == NULL) { + if (dash == NULL || dash->fields == NULL) { mg_dash_error(c, wm->data, "%s", "no fields defined"); } else { - mg_dash_process_msg(c, wm, fields); + mg_dash_process_msg(c, wm, dash); } } }