aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/json_rpc.h
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-07-17 23:03:59 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-07-18 00:38:50 +0200
commite05f02acd583797ceb449fc501d371d45a4293c1 (patch)
tree491322cf633b67918f0361c1ce986f6b690844bc /src/json_rpc.h
parentdocker: sanitize package dependencies (diff)
downloadcimple-e05f02acd583797ceb449fc501d371d45a4293c1.tar.gz
cimple-e05f02acd583797ceb449fc501d371d45a4293c1.zip
switch to JSON-RPC as message format
Instead of the weird `struct msg` I had, I switched to the JSON-RPC format. It's basically the same, but has a well-defined semantics in case of errors.
Diffstat (limited to 'src/json_rpc.h')
-rw-r--r--src/json_rpc.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/json_rpc.h b/src/json_rpc.h
new file mode 100644
index 0000000..6e2be04
--- /dev/null
+++ b/src/json_rpc.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com>
+ * 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_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 *);
+
+const char *jsonrpc_request_to_string(struct jsonrpc_request *);
+int jsonrpc_request_parse(struct jsonrpc_request **, const char *src);
+
+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;
+
+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 *);
+
+const char *jsonrpc_response_to_string(struct jsonrpc_response *);
+int jsonrpc_response_parse(struct jsonrpc_response **, const char *src);
+
+int jsonrpc_response_send(const struct jsonrpc_response *, int fd);
+int jsonrpc_response_recv(struct jsonrpc_response **, int fd);
+
+#endif