Skip to content

Commit

Permalink
Add jsonipc event message constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Ramsay <[email protected]>
  • Loading branch information
lack committed Nov 12, 2022
1 parent 3164345 commit dfc45da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/json-ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);

Expand Down
17 changes: 14 additions & 3 deletions src/json-ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,28 @@ struct jsonipc_request* jsonipc_request_parse_new(json_t* root,
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*}",
Expand Down

0 comments on commit dfc45da

Please sign in to comment.