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 (-32602)
71
73#define JSON_RPC_CODE_INTERNAL_ERROR (-32603)
74
77// -32000 to -32099 Reserved for implementation-defined server-errors.
78
113
121typedef struct {
123 void *data;
124
126 int32_t error_code;
127
131
135
148typedef cJSON *(*mjrpc_func)(mjrpc_func_ctx_t *context, cJSON *params,
149 cJSON *id);
150
158 char *name;
159
162
164 void *arg;
165
167 int state;
168};
169
177typedef struct mjrpc_handle {
180
182 size_t capacity;
183
185 size_t size;
187
206typedef void *(*mjrpc_malloc_func)(size_t size);
207
216typedef void (*mjrpc_free_func)(void *ptr);
217
227typedef char *(*mjrpc_strdup_func)(const char *str);
228
263 mjrpc_free_func free_func,
264 mjrpc_strdup_func strdup_func);
265
301char *mjrpc_request_str(const char *method, cJSON *params, cJSON *id);
302
332cJSON *mjrpc_request_cjson(const char *method, cJSON *params, cJSON *id);
333
376cJSON *mjrpc_response_ok(cJSON *result, cJSON *id);
377
403cJSON *mjrpc_response_error(int code, const char *message, cJSON *id);
404
437mjrpc_handle_t *mjrpc_create_handle(size_t initial_capacity);
438
462
508int mjrpc_add_method(mjrpc_handle_t *handle, mjrpc_func function_pointer,
509 const char *method_name, void *arg2func);
510
533int mjrpc_del_method(mjrpc_handle_t *handle, const char *method_name);
534
552
577 void (*callback)(const char *method_name, void *arg,
578 void *user_data),
579 void *user_data);
580
618char *mjrpc_process_str(const mjrpc_handle_t *handle, const char *request_str,
619 int *ret_code);
620
652 const cJSON *request_cjson, int *ret_code);
653
657#ifdef __cplusplus
658}
659#endif
660
661#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:216
void *(* mjrpc_malloc_func)(size_t size)
Function pointer type for custom memory allocation.
Definition mjsonrpc.h:206
char *(* mjrpc_strdup_func)(const char *str)
Function pointer type for custom string duplication.
Definition mjsonrpc.h:227
int mjrpc_del_method(mjrpc_handle_t *handle, const char *method_name)
Unregister an RPC method.
int mjrpc_enum_methods(const mjrpc_handle_t *handle, void(*callback)(const char *method_name, void *arg, void *user_data), void *user_data)
Enumerate all registered methods.
int mjrpc_add_method(mjrpc_handle_t *handle, mjrpc_func function_pointer, const char *method_name, void *arg2func)
Register a new RPC method.
size_t mjrpc_get_method_count(const mjrpc_handle_t *handle)
Get the number of registered methods.
cJSON * mjrpc_process_cjson(const mjrpc_handle_t *handle, const cJSON *request_cjson, int *ret_code)
Process a JSON-RPC request cJSON object.
char * mjrpc_process_str(const mjrpc_handle_t *handle, const char *request_str, int *ret_code)
Process a JSON-RPC request string.
cJSON * mjrpc_response_error(int code, const 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:148
struct mjrpc_handle mjrpc_handle_t
Typedef for struct mjrpc_handle for convenience.
Context structure passed to RPC method callback functions.
Definition mjsonrpc.h:121
int32_t error_code
Error code to be set by the method implementation (0 = no error)
Definition mjsonrpc.h:126
char * error_message
Error message to be set by the method implementation (will be freed automatically)
Definition mjsonrpc.h:130
int params_type
Parameter type: 0=object, 1=array, 2=no params.
Definition mjsonrpc.h:133
void * data
User data pointer passed during method registration.
Definition mjsonrpc.h:123
Main handle structure for managing RPC methods.
Definition mjsonrpc.h:177
size_t capacity
Total capacity of the hash table.
Definition mjsonrpc.h:182
struct mjrpc_method * methods
Array of registered methods (hash table)
Definition mjsonrpc.h:179
size_t size
Current number of registered methods.
Definition mjsonrpc.h:185
Internal structure representing a registered RPC method.
Definition mjsonrpc.h:156
void * arg
User argument passed to the function.
Definition mjsonrpc.h:164
mjrpc_func func
Function pointer to method implementation.
Definition mjsonrpc.h:161
char * name
Method name.
Definition mjsonrpc.h:158
int state
Internal state for hash table management.
Definition mjsonrpc.h:167