1 #ifndef JSONRPC_PROTOCOL_HANDLER_H 2 #define JSONRPC_PROTOCOL_HANDLER_H 26 rsp.
error.code = code;
29 response_data +=
"\n";
33 template<
class t_connection_context>
40 t_connection_context& conn_context) = 0;
47 template<
class t_connection_context>
54 template<
class t_connection_context = net_utils::connection_context_base>
63 t_connection_context& conn_context)
66 m_conn_context(conn_context),
67 m_is_stop_handling(
false)
95 bool res = handle_buff_in(
buf);
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");
117 extract_cached_request_and_handle(pos);
120 if (!m_cache.size()) {
121 m_is_stop_handling =
true;
127 bool extract_cached_request_and_handle(std::string::size_type pos)
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);
133 bool handle_request_and_send_response(
const std::string& request_data)
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);
145 std::string::size_type match_end_of_request(
const std::string&
buf)
147 std::string::size_type
res =
buf.find(
"\n");
148 if(std::string::npos !=
res) {
159 t_connection_context& m_conn_context;
161 bool m_is_stop_handling;
void handle_qued_callback()
virtual ~i_jsonrpc2_server_handler()
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
virtual bool handle_rpc_request(const std::string &req_data, std::string &resp_data, t_connection_context &conn_context)=0
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
bool store_t_to_json(t_struct &str_in, std::string &json_buff, size_t indent=0, bool insert_newlines=true)
virtual bool deinit_server_thread()
jsonrpc2_connection_handler(i_service_endpoint *psnd_hndlr, config_type &config, t_connection_context &conn_context)
std::string & make_error_resp_json(int64_t code, const std::string &message, std::string &response_data, const epee::serialization::storage_entry &id=nullptr)
virtual bool init_server_thread()
t_connection_context connection_context
epee::serialization::storage_entry id
std::string message("Message requiring signing")
virtual bool thread_deinit()
jsonrpc2_server_config< t_connection_context > config_type
bool after_init_connection()
i_service_endpoint * m_psnd_hndlr
i_jsonrpc2_server_handler< t_connection_context > * m_phandler
virtual bool handle_recv(const void *ptr, size_t cb)
virtual bool do_send(const void *ptr, size_t cb)=0
virtual bool thread_init()
virtual ~jsonrpc2_connection_handler()