mjsonrpc 1.0
A lightweight JSON-RPC 2.0 message parser and generator based on cJSON
Loading...
Searching...
No Matches
mjsonrpc.h
Go to the documentation of this file.
1
44#ifndef MJSONRPC_H_
45#define MJSONRPC_H_
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51#include "cJSON.h"
52#include <stdint.h>
53
61#define JSON_RPC_CODE_PARSE_ERROR (-32700)
62
64#define JSON_RPC_CODE_INVALID_REQUEST (-32600)
65
67#define JSON_RPC_CODE_METHOD_NOT_FOUND (-32601)
68
70#define JSON_RPC_CODE_INVALID_PARAMS (-32603)
71
73#define JSON_RPC_CODE_INTERNAL_ERROR (-32693)
74
76// -32000 to -32099 Reserved for implementation-defined server-errors.
77
113
121typedef struct
122{
124 void* data;
125
127 int32_t error_code;
128
132
145typedef cJSON* (*mjrpc_func)(mjrpc_func_ctx_t* context, cJSON* params, cJSON* id);
146
153{
155 char* name;
156
159
161 void* arg;
162
164 int state;
165};
166
174typedef struct mjrpc_handle
175{
178
180 size_t capacity;
181
183 size_t size;
185
205typedef void* (*mjrpc_malloc_func)(size_t size);
206
215typedef void (*mjrpc_free_func)(void* ptr);
216
226typedef char* (*mjrpc_strdup_func)(const char* str);
227
254 mjrpc_strdup_func strdup_func);
255
291char* mjrpc_request_str(const char* method, cJSON* params, cJSON* id);
292
320cJSON* mjrpc_request_cjson(const char* method, cJSON* params, cJSON* id);
321
364cJSON* mjrpc_response_ok(cJSON* result, cJSON* id);
365
392cJSON* mjrpc_response_error(int code, char* message, cJSON* id);
393
425mjrpc_handle_t* mjrpc_create_handle(size_t initial_capacity);
426
450
492int mjrpc_add_method(mjrpc_handle_t* handle, mjrpc_func function_pointer, const char* method_name,
493 void* arg2func);
494
517int mjrpc_del_method(mjrpc_handle_t* handle, const char* method_name);
518
556char* mjrpc_process_str(mjrpc_handle_t* handle, const char* request_str, int* ret_code);
557
588cJSON* mjrpc_process_cjson(mjrpc_handle_t* handle, const cJSON* request_cjson, int* ret_code);
589
593#ifdef __cplusplus
594}
595#endif
596
597#endif // MJSONRPC_H_
char * mjrpc_request_str(const char *method, cJSON *params, cJSON *id)
Build a JSON-RPC request as a string.
cJSON * mjrpc_request_cjson(const char *method, cJSON *params, cJSON *id)
Build a JSON-RPC request as a cJSON object.
int mjrpc_destroy_handle(mjrpc_handle_t *handle)
Destroy a JSON-RPC handle and free all associated memory.
mjrpc_handle_t * mjrpc_create_handle(size_t initial_capacity)
Create a new JSON-RPC handle.
int mjrpc_set_memory_hooks(mjrpc_malloc_func malloc_func, mjrpc_free_func free_func, mjrpc_strdup_func strdup_func)
Set custom memory management functions.
void(* mjrpc_free_func)(void *ptr)
Function pointer type for custom memory deallocation.
Definition mjsonrpc.h:215
void *(* mjrpc_malloc_func)(size_t size)
Function pointer type for custom memory allocation.
Definition mjsonrpc.h:205
char *(* mjrpc_strdup_func)(const char *str)
Function pointer type for custom string duplication.
Definition mjsonrpc.h:226
int mjrpc_del_method(mjrpc_handle_t *handle, const char *method_name)
Unregister an RPC method.
int mjrpc_add_method(mjrpc_handle_t *handle, mjrpc_func function_pointer, const char *method_name, void *arg2func)
Register a new RPC method.
char * mjrpc_process_str(mjrpc_handle_t *handle, const char *request_str, int *ret_code)
Process a JSON-RPC request string.
cJSON * mjrpc_process_cjson(mjrpc_handle_t *handle, const cJSON *request_cjson, int *ret_code)
Process a JSON-RPC request cJSON object.
cJSON * mjrpc_response_error(int code, char *message, cJSON *id)
Build an error JSON-RPC response.
cJSON * mjrpc_response_ok(cJSON *result, cJSON *id)
Build a successful JSON-RPC response with result.
mjrpc_error_return
Reserved for implementation-defined server-errors (-32000 to -32099)
Definition mjsonrpc.h:85
@ MJRPC_RET_ERROR_NOT_FOUND
Requested method not found.
Definition mjsonrpc.h:96
@ MJRPC_RET_OK_NOTIFICATION
Operation completed successfully (notification request)
Definition mjsonrpc.h:90
@ MJRPC_RET_ERROR_MEM_ALLOC_FAILED
Memory allocation failed.
Definition mjsonrpc.h:93
@ MJRPC_RET_ERROR_HANDLE_NOT_INITIALIZED
Handle not initialized.
Definition mjsonrpc.h:108
@ MJRPC_RET_ERROR_NOT_OBJ_ARY
Request is not a JSON object or array.
Definition mjsonrpc.h:102
@ MJRPC_RET_ERROR_EMPTY_REQUEST
Empty request received.
Definition mjsonrpc.h:99
@ MJRPC_RET_ERROR_PARSE_FAILED
JSON parsing failed.
Definition mjsonrpc.h:105
@ MJRPC_RET_OK
Operation completed successfully.
Definition mjsonrpc.h:87
@ MJRPC_RET_ERROR_INVALID_PARAM
Invalid parameter provided.
Definition mjsonrpc.h:111
cJSON *(* mjrpc_func)(mjrpc_func_ctx_t *context, cJSON *params, cJSON *id)
Function pointer type for RPC method implementations.
Definition mjsonrpc.h:145
struct mjrpc_handle mjrpc_handle_t
Typedef for struct mjrpc_handle for convenience.
Context structure passed to RPC method callback functions.
Definition mjsonrpc.h:122
int32_t error_code
Error code to be set by the method implementation (0 = no error)
Definition mjsonrpc.h:127
char * error_message
Error message to be set by the method implementation (will be freed automatically)
Definition mjsonrpc.h:130
void * data
User data pointer passed during method registration.
Definition mjsonrpc.h:124
Main handle structure for managing RPC methods.
Definition mjsonrpc.h:175
size_t capacity
Total capacity of the hash table.
Definition mjsonrpc.h:180
struct mjrpc_method * methods
Array of registered methods (hash table)
Definition mjsonrpc.h:177
size_t size
Current number of registered methods.
Definition mjsonrpc.h:183
Internal structure representing a registered RPC method.
Definition mjsonrpc.h:153
void * arg
User argument passed to the function.
Definition mjsonrpc.h:161
mjrpc_func func
Function pointer to method implementation.
Definition mjsonrpc.h:158
char * name
Method name.
Definition mjsonrpc.h:155
int state
Internal state for hash table management.
Definition mjsonrpc.h:164