mjsonrpc 1.0
A lightweight JSON-RPC 2.0 message parser and generator based on cJSON
Loading...
Searching...
No Matches
Functions
Method Management Functions

Functions for registering and unregistering RPC methods. More...

Functions

int mjrpc_add_method (mjrpc_handle_t *handle, mjrpc_func function_pointer, const char *method_name, void *arg2func)
 Register a new RPC method.
 
int mjrpc_del_method (mjrpc_handle_t *handle, const char *method_name)
 Unregister an RPC method.
 
size_t mjrpc_get_method_count (const mjrpc_handle_t *handle)
 Get the number of registered methods.
 
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.
 

Detailed Description

Functions for registering and unregistering RPC methods.

Function Documentation

◆ mjrpc_add_method()

int mjrpc_add_method ( mjrpc_handle_t handle,
mjrpc_func  function_pointer,
const char *  method_name,
void *  arg2func 
)

Register a new RPC method.

Adds a new method to the JSON-RPC handle with the specified callback function, method name, and optional user argument.

Parameters
handleJSON-RPC handle (must not be NULL)
function_pointerCallback function for the method (must not be NULL)
method_nameName of the method (must not be NULL)
arg2funcUser argument passed to the callback function (can be NULL). If not NULL, it must be heap-allocated (via malloc/calloc), as it will be automatically freed when the method is deleted or the handle is destroyed.
Returns
Error code from enum mjrpc_error_return
Return values
MJRPC_RET_OKIf successful
MJRPC_RET_ERROR_HANDLE_NOT_INITIALIZEDIf handle is NULL
MJRPC_RET_ERROR_INVALID_PARAMIf function_pointer or method_name is NULL
MJRPC_RET_ERROR_MEM_ALLOC_FAILEDIf memory allocation failed
Note
If a method with the same name already exists, it will be replaced
The method_name will be copied internally
Example:
cJSON *hello_method(mjrpc_func_ctx_t *ctx, cJSON *params, cJSON *id) {
return cJSON_CreateString("Hello, World!");
}
int ret = mjrpc_add_method(handle, hello_method, "hello", NULL);
if (ret != MJRPC_RET_OK) {
// Handle error...
}
mjrpc_handle_t * mjrpc_create_handle(size_t initial_capacity)
Create a new JSON-RPC handle.
int mjrpc_add_method(mjrpc_handle_t *handle, mjrpc_func function_pointer, const char *method_name, void *arg2func)
Register a new RPC method.
@ MJRPC_RET_OK
Operation completed successfully.
Definition mjsonrpc.h:87
Context structure passed to RPC method callback functions.
Definition mjsonrpc.h:121
Main handle structure for managing RPC methods.
Definition mjsonrpc.h:177

◆ mjrpc_del_method()

int mjrpc_del_method ( mjrpc_handle_t handle,
const char *  method_name 
)

Unregister an RPC method.

Removes the specified method from the JSON-RPC handle and frees associated memory.

Parameters
handleJSON-RPC handle (must not be NULL)
method_nameName of the method to remove (must not be NULL)
Returns
Error code from enum mjrpc_error_return
Return values
MJRPC_RET_OKIf successful
MJRPC_RET_ERROR_INVALID_PARAMIf method_name is NULL
MJRPC_RET_ERROR_NOT_FOUNDIf method was not found
Example:
int ret = mjrpc_del_method(handle, "hello");
printf("Method 'hello' not found\n");
}
int mjrpc_del_method(mjrpc_handle_t *handle, const char *method_name)
Unregister an RPC method.
@ MJRPC_RET_ERROR_NOT_FOUND
Requested method not found.
Definition mjsonrpc.h:96

◆ mjrpc_enum_methods()

int mjrpc_enum_methods ( const mjrpc_handle_t handle,
void(*)(const char *method_name, void *arg, void *user_data)  callback,
void *  user_data 
)

Enumerate all registered methods.

Iterates through all registered RPC methods and calls the provided callback function for each method.

Parameters
handleJSON-RPC handle (must not be NULL)
callbackCallback function to be called for each method
user_dataUser data pointer passed to the callback
Returns
Error code from enum mjrpc_error_return
Return values
MJRPC_RET_OKIf successful
MJRPC_RET_ERROR_HANDLE_NOT_INITIALIZEDIf handle is NULL
MJRPC_RET_ERROR_INVALID_PARAMIf callback is NULL
Example:
void print_method(const char *name, void *arg, void *user_data) {
printf("Method: %s\n", name);
}
mjrpc_enum_methods(handle, print_method, NULL);
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.

◆ mjrpc_get_method_count()

size_t mjrpc_get_method_count ( const mjrpc_handle_t handle)

Get the number of registered methods.

Returns the current number of registered RPC methods in the handle.

Parameters
handleJSON-RPC handle (must not be NULL)
Returns
Number of registered methods
Return values
0If handle is NULL or no methods are registered
Example:
size_t count = mjrpc_get_method_count(handle);
printf("Registered methods: %zu\n", count);
size_t mjrpc_get_method_count(const mjrpc_handle_t *handle)
Get the number of registered methods.