Add jsonipc event message constructor

Signed-off-by: Jim Ramsay <i.am@jimramsay.com>
pull/183/head
Jim Ramsay 2022-11-06 08:13:17 -05:00 committed by Andri Yngvason
parent c859c50463
commit 8d32dfaead
2 changed files with 15 additions and 3 deletions

View File

@ -51,6 +51,7 @@ void jsonipc_error_cleanup(struct jsonipc_error*);
struct jsonipc_request* jsonipc_request_parse_new(json_t* root, struct jsonipc_request* jsonipc_request_parse_new(json_t* root,
struct jsonipc_error* err); struct jsonipc_error* err);
struct jsonipc_request* jsonipc_request_new(const char* method, json_t* params); 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); json_t* jsonipc_request_pack(struct jsonipc_request*, json_error_t* err);
void jsonipc_request_destroy(struct jsonipc_request*); void jsonipc_request_destroy(struct jsonipc_request*);

View File

@ -89,17 +89,28 @@ failure:
return NULL; 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)); struct jsonipc_request* ipc = calloc(1, sizeof(*ipc));
ipc->method = method; ipc->method = method;
ipc->params = params; ipc->params = params;
json_incref(ipc->params); json_incref(ipc->params);
ipc->id = json_integer(request_id++); ipc->id = id;
return ipc; 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) 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*}", return json_pack_ex(err, 0, "{s:s, s:O*, s:O*}",