1#ifndef E66C1505_D447_4384_BB35_55B23FF31F0A
2#define E66C1505_D447_4384_BB35_55B23FF31F0A
5
6
7
8
9
10
11
14
15
16
19
20
21
23#ifdef WWA_JSONRPC_STATIC_DEFINE
24# define WWA_JSONRPC_EXPORT
25# define WWA_JSONRPC_NO_EXPORT
27# ifdef wwa_jsonrpc_EXPORTS
29# if defined _WIN32 || defined __CYGWIN__
30# define WWA_JSONRPC_EXPORT __declspec(dllexport)
31# define WWA_JSONRPC_NO_EXPORT
33# define WWA_JSONRPC_EXPORT [[gnu::visibility("default")]]
34# define WWA_JSONRPC_NO_EXPORT [[gnu::visibility("hidden")]]
38# if defined _WIN32 || defined __CYGWIN__
39# define WWA_JSONRPC_EXPORT __declspec(dllimport)
40# define WWA_JSONRPC_NO_EXPORT
42# define WWA_JSONRPC_EXPORT [[gnu::visibility("default")]]
43# define WWA_JSONRPC_NO_EXPORT [[gnu::visibility("hidden")]]
Private implementation of the JSON RPC dispatcher class.
static std::uint64_t get_and_increment_counter() noexcept
Generates a unique request ID.
A class that manages JSON RPC method handlers and processes JSON RPC requests.
virtual nlohmann::json do_process_request(const nlohmann::json &request, const std::any &data, bool is_batch, std::uint64_t unique_id)
Processes a single, non-batch JSON RPC request.
dispatcher & operator=(dispatcher &&rhs)=default
Move assignment operator.
dispatcher(dispatcher &&rhs)=default
Move constructor.
void add_ex(std::string_view method, F &&f, C instance)
Adds a method handler with a context parameter and a class instance.
constexpr auto create_closure(C inst, F &&f) const
Creates a closure for invoking a member function with JSON parameters.
virtual void request_parsed(const jsonrpc_request &request, const std::any &data, std::uint64_t unique_id)
Invoked after the request has been parsed.
void add_internal_method(std::string_view method, handler_t &&handler)
Adds a method handler for the specified method.
nlohmann::json process_request(const nlohmann::json &request, const std::any &data={})
Processes a JSON RPC request.
virtual void request_failed(const nlohmann::json &request_id, const std::exception *e, bool is_batch, std::uint64_t unique_id)
Invoked when a request fails.
virtual nlohmann::json invoke(const std::string &method, const nlohmann::json ¶ms, const dispatcher::context_t &ctx, std::uint64_t unique_id)
Invokes a method handler.
void add(std::string_view method, F &&f)
Adds a method handler f for the method method.
void add(std::string_view method, F &&f, C instance)
Adds a method to the dispatcher with the specified instance and function.
virtual nlohmann::json process_batch_request(const nlohmann::json &request, const std::any &data, std::uint64_t unique_id)
Processes a batch request.
dispatcher()
Class constructor.
dispatcher(const dispatcher &)=delete
dispatcher & operator=(const dispatcher &)=delete
virtual ~dispatcher()
Class destructor.
void add_ex(std::string_view method, F &&f)
Adds a method handler with a context parameter.
std::unique_ptr< dispatcher_private > d_ptr
Pointer to the implementation (Pimpl idiom).
JSON RPC Exception class.
exception(const exception &)=default
Default copy constructor.
~exception() override
Default destructor.
nlohmann::json m_data
Custom data associated with the error.
std::string m_message
Error message.
int code() const noexcept
Returns the error code.
const std::string & message() const noexcept
Returns the error message.
exception(exception &&)=default
Default move constructor.
exception(int code, std::string_view message)
Construct a new exception object.
exception & operator=(const exception &rhs)=default
Default copy assignment operator.
const nlohmann::json & data() const noexcept
Returns custom data associated with the error.
exception & operator=(exception &&rhs)=default
Default move assignment operator.
const char * what() const noexcept override
Returns the error message.
nlohmann::json to_json() const
Returns the error message as an Error Object.
exception(int code, std::string_view message, const T &data)
Construct a new exception object with additional data.
Exception thrown when the method is not found.
method_not_found_exception(method_not_found_exception &&)=default
method_not_found_exception & operator=(method_not_found_exception &&)=default
method_not_found_exception & operator=(const method_not_found_exception &)=default
~method_not_found_exception() override
Default destructor.
method_not_found_exception()
method_not_found_exception(const method_not_found_exception &)=default
#define WWA_JSONRPC_EXPORT
Macro for exporting symbols when building the library dynamically or importing symbols when using the...
static constexpr int INVALID_PARAMS
Invalid method parameter(s).
static constexpr int INTERNAL_ERROR
Internal JSON-RPC error.
static constexpr int METHOD_NOT_FOUND
The method does not exist or is not available.
static constexpr int INVALID_REQUEST
The JSON sent is not a valid Request object.
static constexpr int PARSE_ERROR
Invalid JSON was received by the server.
static constexpr std::string_view err_method_not_found
Error message for when the method is not found.
static constexpr std::string_view err_not_jsonrpc_2_0_request
Error message for when the request is not a JSON-RPC 2.0 request.
static constexpr std::string_view err_empty_batch
Error message for when the batch request is empty.
static constexpr std::string_view err_empty_method
Error message for when the method is empty.
static constexpr std::string_view err_bad_request
Error request for when the request is not valid.
static constexpr std::string_view err_bad_params_type
Error message for when the parameters are not an array or an object.
static constexpr std::string_view err_invalid_params_passed_to_method
Error message for when the parameters passed to the method are not correct.
static constexpr std::string_view err_bad_id_type
Error message for when the ID is not a number, a string, or null.
constexpr auto convert_args(const nlohmann::json ¶ms, std::index_sequence< Indices... >)
Converts JSON parameters to a tuple of arguments based on the specified types.
Contains the implementation details of the JSON RPC library.
Represents a JSON RPC request.