From 189aa80743bb91ad3e8eb21527e253cf06ba0246 Mon Sep 17 00:00:00 2001 From: cpq Date: Sat, 13 Aug 2022 20:09:51 +0100 Subject: [PATCH] Update examples to NOT use mg_*mprintf --- examples/huge-response/main.c | 17 +++++++---------- examples/json-rpc-over-websocket/main.c | 8 +++----- test/unit_test.c | 3 +-- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/examples/huge-response/main.c b/examples/huge-response/main.c index 7cd7eb0a..66313a26 100644 --- a/examples/huge-response/main.c +++ b/examples/huge-response/main.c @@ -27,7 +27,7 @@ static long getparam(struct mg_http_message *hm, const char *json_path) { return dv; } -static size_t printdata(void (*out)(char, void *), void *ptr, va_list *ap) { +static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) { unsigned start = va_arg(*ap, unsigned); unsigned max = start + CHUNK_SIZE; const char *comma = ""; @@ -45,23 +45,20 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = ev_data; if (mg_http_match_uri(hm, "/api/data")) { + const char *headers = "content-type: text/json\r\n"; long start = getparam(hm, "$.start"); long version = getparam(hm, "$.version"); - char *response = NULL; - MG_INFO(("%.*s", (int) hm->body.len, hm->body.ptr)); + MG_DEBUG(("%.*s", (int) hm->body.len, hm->body.ptr)); if (version > 0 && version != s_version) { // Version mismatch: s_data has changed while client fetches it // Tell client to restart - response = mg_mprintf("{%Q:%Q, %Q:%ld}", "error", "wrong version", - "version", version); + mg_http_reply(c, 200, headers, "{%Q:%Q, %Q:%ld}", "error", + "wrong version", "version", version); } else { // Return data, up to CHUNK_SIZE elements - response = mg_mprintf("{%Q:%ld,%Q:%ld,%Q:[%M]}", "version", s_version, - "start", start, "data", printdata, start); + mg_http_reply(c, 200, headers, "{%Q:%ld,%Q:%ld,%Q:[%M]}", "version", + s_version, "start", start, "data", printdata, start); } - mg_http_reply(c, 200, "Content-Type: text/json\r\n", "%s", response); - MG_INFO(("%s", response)); - free(response); } else { struct mg_http_serve_opts opts = {0}; opts.root_dir = s_root_dir; diff --git a/examples/json-rpc-over-websocket/main.c b/examples/json-rpc-over-websocket/main.c index 230eac00..47a98f4d 100644 --- a/examples/json-rpc-over-websocket/main.c +++ b/examples/json-rpc-over-websocket/main.c @@ -56,14 +56,12 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { static void timer_fn(void *arg) { struct mg_mgr *mgr = (struct mg_mgr *) arg; - char *msg = mg_mprintf("{%Q:%Q,%Q:[%d,%d,%d]}", "method", "notification1", - "params", 1, 2, 3); // Broadcast message to all connected websocket clients. for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) { - // Send JSON-RPC notifications to marked connections - if (c->label[0] == 'W') mg_ws_send(c, msg, strlen(msg), WEBSOCKET_OP_TEXT); + if (c->label[0] != 'W') continue; + mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%Q:%Q,%Q:[%d,%d,%d]}", "method", + "notification1", "params", 1, 2, 3); } - free(msg); } int main(void) { diff --git a/test/unit_test.c b/test/unit_test.c index 09c1e3be..2434d05a 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -1528,7 +1528,6 @@ static void test_str(void) { ASSERT(strcmp(buf, expected) == 0); p = mg_mprintf("[%s,%M,%s]", "null", pf1, 2, 3, "hi"); - // printf("-> %s\n", p); ASSERT(strcmp(p, "[null,5,hi]") == 0); free(p); @@ -1539,7 +1538,6 @@ static void test_str(void) { mg_rprintf(mg_pfn_iobuf, &io, "[%M", pf2, 10); mg_rprintf(mg_pfn_iobuf, &io, ","); mg_rprintf(mg_pfn_iobuf, &io, "%d]", 7); - printf("-> %s\n", io.buf); ASSERT(strcmp((char *) io.buf, "[9876543210,7]") == 0); mg_iobuf_free(&io); } @@ -2571,5 +2569,6 @@ int main(void) { test_sntp(); test_mqtt(); printf("SUCCESS. Total tests: %d\n", s_num_tests); + return EXIT_SUCCESS; }