From 8d32dfaeade0fceef81fed0d2c7d26928c0494fd Mon Sep 17 00:00:00 2001 From: Jim Ramsay Date: Sun, 6 Nov 2022 08:13:17 -0500 Subject: [PATCH] Add jsonipc event message constructor Signed-off-by: Jim Ramsay --- include/json-ipc.h | 1 + src/json-ipc.c | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/json-ipc.h b/include/json-ipc.h index 1ba141c..eac2f22 100644 --- a/include/json-ipc.h +++ b/include/json-ipc.h @@ -51,6 +51,7 @@ void jsonipc_error_cleanup(struct jsonipc_error*); struct jsonipc_request* jsonipc_request_parse_new(json_t* root, struct jsonipc_error* err); struct jsonipc_request* jsonipc_request_new(const char* method, json_t* params); +struct jsonipc_request* jsonipc_event_new(const char* method, json_t* params); json_t* jsonipc_request_pack(struct jsonipc_request*, json_error_t* err); void jsonipc_request_destroy(struct jsonipc_request*); diff --git a/src/json-ipc.c b/src/json-ipc.c index 7a4c643..7ebf094 100644 --- a/src/json-ipc.c +++ b/src/json-ipc.c @@ -89,17 +89,28 @@ failure: return NULL; } -static int request_id = 1; -struct jsonipc_request* jsonipc_request_new(const char* method, json_t* params) +struct jsonipc_request* jsonipc_request__new(const char* method, json_t* params, + json_t* id) { struct jsonipc_request* ipc = calloc(1, sizeof(*ipc)); ipc->method = method; ipc->params = params; json_incref(ipc->params); - ipc->id = json_integer(request_id++); + ipc->id = id; return ipc; } +static int request_id = 1; +struct jsonipc_request* jsonipc_request_new(const char* method, json_t* params) +{ + return jsonipc_request__new(method, params, json_integer(request_id++)); +} + +struct jsonipc_request* jsonipc_event_new(const char* method, json_t* params) +{ + return jsonipc_request__new(method, params, NULL); +} + json_t* jsonipc_request_pack(struct jsonipc_request* self, json_error_t* err) { return json_pack_ex(err, 0, "{s:s, s:O*, s:O*}",