Functions for registering and unregistering RPC methods.
More...
Functions for registering and unregistering RPC methods.
◆ 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
-
| handle | JSON-RPC handle (must not be NULL) |
| function_pointer | Callback function for the method (must not be NULL) |
| method_name | Name of the method (must not be NULL) |
| arg2func | User 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_OK | If successful |
| MJRPC_RET_ERROR_HANDLE_NOT_INITIALIZED | If handle is NULL |
| MJRPC_RET_ERROR_INVALID_PARAM | If function_pointer or method_name is NULL |
| MJRPC_RET_ERROR_MEM_ALLOC_FAILED | If 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:
return cJSON_CreateString("Hello, World!");
}
}
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.
Context structure passed to RPC method callback functions.
Main handle structure for managing RPC methods.
◆ 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
-
| handle | JSON-RPC handle (must not be NULL) |
| method_name | Name of the method to remove (must not be NULL) |
- Returns
- Error code from enum mjrpc_error_return
- Return values
-
| MJRPC_RET_OK | If successful |
| MJRPC_RET_ERROR_INVALID_PARAM | If method_name is NULL |
| MJRPC_RET_ERROR_NOT_FOUND | If method was not found |
- Example:
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.
◆ 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
-
| handle | JSON-RPC handle (must not be NULL) |
| callback | Callback function to be called for each method |
| user_data | User data pointer passed to the callback |
- Returns
- Error code from enum mjrpc_error_return
- Return values
-
| MJRPC_RET_OK | If successful |
| MJRPC_RET_ERROR_HANDLE_NOT_INITIALIZED | If handle is NULL |
| MJRPC_RET_ERROR_INVALID_PARAM | If callback is NULL |
- Example:
void print_method(const char *name, void *arg, void *user_data) {
printf("Method: %s\n", name);
}
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()
Get the number of registered methods.
Returns the current number of registered RPC methods in the handle.
- Parameters
-
| handle | JSON-RPC handle (must not be NULL) |
- Returns
- Number of registered methods
- Return values
-
| 0 | If handle is NULL or no methods are registered |
- Example:
printf("Registered methods: %zu\n", count);
size_t mjrpc_get_method_count(const mjrpc_handle_t *handle)
Get the number of registered methods.