aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/json_rpc.h
blob: b2d026dad2e71950468572a58b692ca44daf8f20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (c) 2023 Egor Tensin <egor@tensin.name>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple
 * Distributed under the MIT License.
 */

#ifndef __JSON_RPC_H__
#define __JSON_RPC_H__

/* This attempts to adhere to the format described in https://www.jsonrpc.org/specification. */

#include <json-c/json_object.h>

#include <stdint.h>

struct jsonrpc_request;

int jsonrpc_generate_request_id(void);

int jsonrpc_request_create(
    struct jsonrpc_request**,
    int id,
    const char* method,
    struct json_object* params
);
void jsonrpc_request_destroy(struct jsonrpc_request*);

int jsonrpc_notification_create(
    struct jsonrpc_request**,
    const char* method,
    struct json_object* params
);
int jsonrpc_request_is_notification(const struct jsonrpc_request*);

int jsonrpc_request_send(const struct jsonrpc_request*, int fd);
int jsonrpc_request_recv(struct jsonrpc_request**, int fd);

const char* jsonrpc_request_get_method(const struct jsonrpc_request*);

int jsonrpc_request_get_param_string(const struct jsonrpc_request*, const char* name, const char**);
int jsonrpc_request_set_param_string(struct jsonrpc_request*, const char* name, const char*);
int jsonrpc_request_get_param_int(const struct jsonrpc_request*, const char* name, int64_t*);
int jsonrpc_request_set_param_int(struct jsonrpc_request*, const char* name, int64_t);

struct jsonrpc_response;

const char* jsonrpc_response_to_string(const struct jsonrpc_response*);

int jsonrpc_response_create(
    struct jsonrpc_response**,
    const struct jsonrpc_request*,
    struct json_object* result
);
void jsonrpc_response_destroy(struct jsonrpc_response*);

int jsonrpc_error_create(
    struct jsonrpc_response**,
    struct jsonrpc_request*,
    int code,
    const char* message
);
int jsonrpc_response_is_error(const struct jsonrpc_response*);

int jsonrpc_response_send(const struct jsonrpc_response*, int fd);
int jsonrpc_response_recv(struct jsonrpc_response**, int fd);

#endif