Update examples to NOT use mg_*mprintf

This commit is contained in:
cpq committed 2022-08-13 20:09:51 +01:00
1 parent f122bd5797
commit 189aa80743
3 files changed
+11 -17

No files matched your search

+7 -10
View File
@@ -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;
+3 -5
View File
@@ -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) {
+1 -2
View File
@@ -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;
}