Electroneum
Loading...
Searching...
No Matches
jsonrpc_server_handlers_map.h
Go to the documentation of this file.
1#ifndef JSONRPC_SERVER_HANDLERS_MAP_H
2#define JSONRPC_SERVER_HANDLERS_MAP_H
3
4#include <string>
8#include "jsonrpc_structs.h"
10
11#define BEGIN_JSONRPC2_MAP(t_connection_context) \
12bool handle_rpc_request(const std::string& req_data, \
13 std::string& resp_data, \
14 t_connection_context& m_conn_context) \
15{ \
16 bool handled = false; \
17 uint64_t ticks = epee::misc_utils::get_tick_count(); \
18 epee::serialization::portable_storage ps; \
19 if (!ps.load_from_json(req_data)) \
20 { \
21 epee::net_utils::jsonrpc2::make_error_resp_json(-32700, "Parse error", resp_data); \
22 return true; \
23 } \
24 epee::serialization::storage_entry id_; \
25 id_ = epee::serialization::storage_entry(std::string()); \
26 if (!ps.get_value("id", id_, nullptr)) \
27 { \
28 epee::net_utils::jsonrpc2::make_error_resp_json(-32600, "Invalid Request", resp_data); \
29 return true; \
30 } \
31 std::string callback_name; \
32 if (!ps.get_value("method", callback_name, nullptr)) \
33 { \
34 epee::net_utils::jsonrpc2::make_error_resp_json(-32600, "Invalid Request", resp_data, id_); \
35 return true; \
36 } \
37 if (false) return true; //just a stub to have "else if"
38
39
40
41#define PREPARE_JSONRPC2_OBJECTS_FROM_JSON(command_type) \
42 handled = true; \
43 boost::value_initialized<epee::json_rpc::request<command_type::request> > req_; \
44 epee::json_rpc::request<command_type::request>& req = static_cast<epee::json_rpc::request<command_type::request>&>(req_);\
45 if(!req.load(ps)) \
46 { \
47 epee::net_utils::jsonrpc2::make_error_resp_json(-32602, "Invalid params", resp_data, req.id); \
48 return true; \
49 } \
50 uint64_t ticks1 = epee::misc_utils::get_tick_count(); \
51 boost::value_initialized<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> > resp_; \
52 epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error>& resp = static_cast<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> &>(resp_); \
53 resp.jsonrpc = "2.0"; \
54 resp.id = req.id;
55
56#define FINALIZE_JSONRPC2_OBJECTS_TO_JSON(method_name) \
57 uint64_t ticks2 = epee::misc_utils::get_tick_count(); \
58 epee::serialization::store_t_to_json(resp, resp_data, 0, false); \
59 resp_data += "\n"; \
60 uint64_t ticks3 = epee::misc_utils::get_tick_count(); \
61 LOG_PRINT("[" << method_name << "] processed with " << ticks1-ticks << "/"<< ticks2-ticks1 << "/" << ticks3-ticks2 << "ms", LOG_LEVEL_2);
62
63
64#define MAP_JSONRPC2_WE(method_name, callback_f, command_type) \
65 else if (callback_name == method_name) \
66 { \
67 PREPARE_JSONRPC2_OBJECTS_FROM_JSON(command_type) \
68 epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \
69 fail_resp.jsonrpc = "2.0"; \
70 fail_resp.id = req.id; \
71 if(!callback_f(req.params, resp.result, fail_resp.error, m_conn_context)) \
72 { \
73 epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), resp_data, 0, false); \
74 resp_data += "\n"; \
75 return true; \
76 } \
77 FINALIZE_JSONRPC2_OBJECTS_TO_JSON(method_name) \
78 return true; \
79 }
80
81#define END_JSONRPC2_MAP() \
82 epee::net_utils::jsonrpc2::make_error_resp_json(-32601, "Method not found", resp_data, id_); \
83 return true; \
84}
85
86#endif /* JSONRPC_SERVER_HANDLERS_MAP_H */