Electroneum
Loading...
Searching...
No Matches
jsonrpc_protocol_handler.h
Go to the documentation of this file.
1#ifndef JSONRPC_PROTOCOL_HANDLER_H
2#define JSONRPC_PROTOCOL_HANDLER_H
3
4#include <cstdint>
5#include <string>
6
8#include "jsonrpc_structs.h"
11
12namespace epee
13{
14namespace net_utils
15{
16 namespace jsonrpc2
17 {
18 inline
19 std::string& make_error_resp_json(int64_t code, const std::string& message,
20 std::string& response_data,
21 const epee::serialization::storage_entry& id = nullptr)
22 {
24 rsp.id = id;
25 rsp.jsonrpc = "2.0";
26 rsp.error.code = code;
27 rsp.error.message = message;
28 epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(rsp), response_data, 0, false);
29 response_data += "\n";
30 return response_data;
31 }
32
33 template<class t_connection_context>
35 {
38 virtual bool handle_rpc_request(const std::string& req_data,
39 std::string& resp_data,
40 t_connection_context& conn_context) = 0;
41 virtual bool init_server_thread()
42 { return true; }
43 virtual bool deinit_server_thread()
44 { return true; }
45 };
46
47 template<class t_connection_context>
53
54 template<class t_connection_context = net_utils::connection_context_base>
56 {
57 public:
58 typedef t_connection_context connection_context;
60
63 t_connection_context& conn_context)
64 : m_psnd_hndlr(psnd_hndlr),
65 m_config(config),
66 m_conn_context(conn_context),
67 m_is_stop_handling(false)
68 {}
71
73 {
74 return true;
75 }
76 virtual bool thread_init()
77 {
78 return true;
79 }
80 virtual bool thread_deinit()
81 {
82 return true;
83 }
87 {
88 return true;
89 }
90 virtual bool handle_recv(const void* ptr, size_t cb)
91 {
92 std::string buf((const char*)ptr, cb);
93 LOG_PRINT_L0("JSONRPC2_RECV: " << ptr << "\r\n" << buf);
94
95 bool res = handle_buff_in(buf);
96 return res;
97 }
98 private:
99 bool handle_buff_in(std::string& buf)
100 {
101 if(m_cache.size())
102 m_cache += buf;
103 else
104 m_cache.swap(buf);
105
106 m_is_stop_handling = false;
107 while (!m_is_stop_handling) {
108 std::string::size_type pos = match_end_of_request(m_cache);
109 if (std::string::npos == pos) {
110 m_is_stop_handling = true;
111 if (m_cache.size() > 4096) {
112 LOG_ERROR("jsonrpc2_connection_handler::handle_buff_in: Too long request");
113 return false;
114 }
115 break;
116 } else {
117 extract_cached_request_and_handle(pos);
118 }
119
120 if (!m_cache.size()) {
121 m_is_stop_handling = true;
122 }
123 }
124
125 return true;
126 }
127 bool extract_cached_request_and_handle(std::string::size_type pos)
128 {
129 std::string request_data(m_cache.begin(), m_cache.begin() + pos);
130 m_cache.erase(0, pos);
131 return handle_request_and_send_response(request_data);
132 }
133 bool handle_request_and_send_response(const std::string& request_data)
134 {
135 CHECK_AND_ASSERT_MES(m_config.m_phandler, false, "m_config.m_phandler is NULL!!!!");
136 std::string response_data;
137
138 LOG_PRINT_L3("JSONRPC2_REQUEST: >> \r\n" << request_data);
139 bool rpc_result = m_config.m_phandler->handle_rpc_request(request_data, response_data, m_conn_context);
140 LOG_PRINT_L3("JSONRPC2_RESPONSE: << \r\n" << response_data);
141
142 m_psnd_hndlr->do_send((void*)response_data.data(), response_data.size());
143 return rpc_result;
144 }
145 std::string::size_type match_end_of_request(const std::string& buf)
146 {
147 std::string::size_type res = buf.find("\n");
148 if(std::string::npos != res) {
149 return res + 2;
150 }
151 return res;
152 }
153
154 protected:
156
157 private:
158 config_type& m_config;
159 t_connection_context& m_conn_context;
160 std::string m_cache;
161 bool m_is_stop_handling;
162 };
163 }
164}
165}
166
167#endif /* JSONRPC_PROTOCOL_HANDLER_H */
jsonrpc2_connection_handler(i_service_endpoint *psnd_hndlr, config_type &config, t_connection_context &conn_context)
jsonrpc2_server_config< t_connection_context > config_type
std::string message("Message requiring signing")
const char * res
#define LOG_PRINT_L3(x)
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
#define LOG_ERROR(x)
Definition misc_log_ex.h:98
#define LOG_PRINT_L0(x)
Definition misc_log_ex.h:99
response< dummy_result, error > error_response
std::string & make_error_resp_json(int64_t code, const std::string &message, std::string &response_data, const epee::serialization::storage_entry &id=nullptr)
bool store_t_to_json(t_struct &str_in, std::string &json_buff, size_t indent=0, bool insert_newlines=true)
boost::variant< uint64_t, uint32_t, uint16_t, uint8_t, int64_t, int32_t, int16_t, int8_t, double, bool, std::string, section, array_entry > storage_entry
const char * buf
#define false
signed __int64 int64_t
Definition stdint.h:135
epee::serialization::storage_entry id
virtual bool handle_rpc_request(const std::string &req_data, std::string &resp_data, t_connection_context &conn_context)=0
i_jsonrpc2_server_handler< t_connection_context > * m_phandler