1#ifndef FAB131EA_3F90_43B6_833D_EB89DA373735
2#define FAB131EA_3F90_43B6_833D_EB89DA373735
5
6
7
8
9
10
11
12
25#include <nlohmann/json.hpp>
32
33
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
80
81
82
83
84
85
86
93
94
95
96
97
98
99
100
101
102
116
117
118
122
123
124
125
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
203 void add(std::string_view method, F&& f)
205 this->add(method, std::forward<F>(f),
nullptr);
203 void add(std::string_view method, F&& f) {
…}
209
210
211
212
213
214
215
216
217
218
219
220 template<
typename C,
typename F>
221 void add(std::string_view method, F&& f, C instance)
223 using traits = details::function_traits<std::decay_t<F>>;
224 using ArgsTuple =
typename traits::args_tuple;
226 const auto&& closure =
this->create_closure<C, F,
void, ArgsTuple>(instance, std::forward<F>(f));
227 this->add_internal_method(method, std::forward<
decltype(closure)>(closure));
221 void add(std::string_view method, F&& f, C instance) {
…}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
263 void add_ex(std::string_view method, F&& f)
265 this->add_ex(method, std::forward<F>(f),
nullptr);
263 void add_ex(std::string_view method, F&& f) {
…}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 template<
typename C,
typename F>
286 void add_ex(std::string_view method, F&& f, C instance)
288 using traits = details::function_traits<std::decay_t<F>>;
289 using ArgsTuple =
typename traits::args_tuple;
292 std::tuple_size<std::decay_t<ArgsTuple>>::value > 0,
293 "Handler function must accept the `context` argument. Use `add()` for handlers without the `context` "
297 const auto&& closure =
this->create_closure<C, F, context_t, ArgsTuple>(instance, std::forward<F>(f));
298 this->add_internal_method(method, std::forward<
decltype(closure)>(closure));
286 void add_ex(std::string_view method, F&& f, C instance) {
…}
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
339
340
341
342
343
344
345
346
347
348
349
351 do_process_request(
const nlohmann::json& request,
const std::any& data,
bool is_batch, std::uint64_t unique_id);
354
355
356
357
358
359
360
361
362
367
368
369
370
371
372
376
377
378
379
380
381
382
383
384
385
386
387
389 const std::string& method,
const nlohmann::json& params,
const dispatcher::context_t& ctx,
390 std::uint64_t unique_id
394
395
396
397
398
399
400
402 request_failed(
const nlohmann::json& request_id,
const std::exception* e,
bool is_batch, std::uint64_t unique_id);
406
407
408
409
410
414
415
416
417
418
419
420
421
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 template<
typename C,
typename F,
typename Context,
typename Args>
455 static_assert((std::is_pointer_v<C> && std::is_class_v<std::remove_pointer_t<C>>) || std::is_null_pointer_v<C>);
456 return [func = std::forward<F>(f), inst](
const context_t& ctx,
const nlohmann::json& params) {
457 assert(params.is_array());
458 constexpr auto args_size = std::tuple_size<std::decay_t<Args>>::value;
459 constexpr auto arg_pos = std::is_void_v<Context> ? 0 : 1;
461 if constexpr (args_size == arg_pos + 1) {
462 if constexpr (std::is_same_v<std::decay_t<std::tuple_element_t<arg_pos, Args>>, nlohmann::json>) {
463 auto&& tuple_args = std::tuple_cat(
464 details::make_inst_tuple(inst), details::make_context_tuple<Context>(ctx),
465 std::make_tuple(params)
468 return details::invoke_function(func, std::forward<
decltype(tuple_args)>(tuple_args));
472 if (params.size() + arg_pos == args_size) {
473 constexpr auto offset = std::is_void_v<Context> ? 0U : 1U;
474 auto&& tuple_args = std::tuple_cat(
475 details::make_inst_tuple(inst), details::make_context_tuple<Context>(ctx),
476 details::convert_args<Context, Args>(
477 params, details::offset_sequence_t<offset, std::make_index_sequence<args_size - offset>>{}
481 return details::invoke_function(func, std::forward<
decltype(tuple_args)>(tuple_args));
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 thrown when the method is not found.
method_not_found_exception()
#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 INVALID_REQUEST
The JSON sent is not a valid Request object.
Represents a JSON RPC request.