Electroneum
Toggle main menu visibility
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
7
#include "
net/net_utils_base.h
"
8
#include "
jsonrpc_structs.h
"
9
#include "
storages/portable_storage.h
"
10
#include "
storages/portable_storage_template_helper.h
"
11
12
namespace
epee
13
{
14
namespace
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
{
23
epee::json_rpc::error_response
rsp;
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>
34
struct
i_jsonrpc2_server_handler
35
{
36
virtual
~i_jsonrpc2_server_handler
()
37
{}
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>
48
struct
jsonrpc2_server_config
49
{
50
i_jsonrpc2_server_handler<t_connection_context>
*
m_phandler
;
51
critical_section
m_lock
;
52
};
53
54
template
<
class
t_connection_context = net_utils::connection_context_base>
55
class
jsonrpc2_connection_handler
56
{
57
public
:
58
typedef
t_connection_context
connection_context
;
59
typedef
jsonrpc2_server_config<t_connection_context>
config_type
;
60
61
jsonrpc2_connection_handler
(
i_service_endpoint
* psnd_hndlr,
62
config_type
&
config
,
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
{}
69
virtual
~jsonrpc2_connection_handler
()
70
{}
71
72
bool
release_protocol
()
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
}
84
void
handle_qued_callback
()
85
{}
86
bool
after_init_connection
()
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
:
155
i_service_endpoint
*
m_psnd_hndlr
;
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 */
epee::critical_section
Definition
syncobj.h:82
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::after_init_connection
bool after_init_connection()
Definition
jsonrpc_protocol_handler.h:86
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::connection_context
t_connection_context connection_context
Definition
jsonrpc_protocol_handler.h:58
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::handle_qued_callback
void handle_qued_callback()
Definition
jsonrpc_protocol_handler.h:84
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::m_psnd_hndlr
i_service_endpoint * m_psnd_hndlr
Definition
jsonrpc_protocol_handler.h:155
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::~jsonrpc2_connection_handler
virtual ~jsonrpc2_connection_handler()
Definition
jsonrpc_protocol_handler.h:69
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::handle_recv
virtual bool handle_recv(const void *ptr, size_t cb)
Definition
jsonrpc_protocol_handler.h:90
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::release_protocol
bool release_protocol()
Definition
jsonrpc_protocol_handler.h:72
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::thread_init
virtual bool thread_init()
Definition
jsonrpc_protocol_handler.h:76
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::thread_deinit
virtual bool thread_deinit()
Definition
jsonrpc_protocol_handler.h:80
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::jsonrpc2_connection_handler
jsonrpc2_connection_handler(i_service_endpoint *psnd_hndlr, config_type &config, t_connection_context &conn_context)
Definition
jsonrpc_protocol_handler.h:61
epee::net_utils::jsonrpc2::jsonrpc2_connection_handler::config_type
jsonrpc2_server_config< t_connection_context > config_type
Definition
jsonrpc_protocol_handler.h:59
message
std::string message("Message requiring signing")
res
const char * res
Definition
hmac_keccak.cpp:41
jsonrpc_structs.h
LOG_PRINT_L3
#define LOG_PRINT_L3(x)
Definition
misc_log_ex.h:102
CHECK_AND_ASSERT_MES
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition
misc_log_ex.h:181
LOG_ERROR
#define LOG_ERROR(x)
Definition
misc_log_ex.h:98
LOG_PRINT_L0
#define LOG_PRINT_L0(x)
Definition
misc_log_ex.h:99
config
Definition
cryptonote_config.h:194
epee::json_rpc::error_response
response< dummy_result, error > error_response
Definition
jsonrpc_structs.h:106
epee::net_utils::jsonrpc2
Definition
jsonrpc_protocol_handler.h:17
epee::net_utils::jsonrpc2::make_error_resp_json
std::string & make_error_resp_json(int64_t code, const std::string &message, std::string &response_data, const epee::serialization::storage_entry &id=nullptr)
Definition
jsonrpc_protocol_handler.h:19
epee::net_utils
Definition
gzip_encoding.h:40
epee::serialization::store_t_to_json
bool store_t_to_json(t_struct &str_in, std::string &json_buff, size_t indent=0, bool insert_newlines=true)
Definition
portable_storage_template_helper.h:62
epee::serialization::storage_entry
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
Definition
portable_storage_base.h:154
epee
Definition
ado_db_helper.h:67
net_utils_base.h
portable_storage.h
portable_storage_template_helper.h
buf
const char * buf
Definition
slow_memmem.cpp:74
false
#define false
int64_t
signed __int64 int64_t
Definition
stdint.h:135
epee::json_rpc::error::code
int64_t code
Definition
jsonrpc_structs.h:33
epee::json_rpc::error::message
std::string message
Definition
jsonrpc_structs.h:34
epee::json_rpc::response::id
epee::serialization::storage_entry id
Definition
jsonrpc_structs.h:61
epee::json_rpc::response::error
t_error error
Definition
jsonrpc_structs.h:62
epee::json_rpc::response::jsonrpc
std::string jsonrpc
Definition
jsonrpc_structs.h:59
epee::net_utils::i_service_endpoint
Definition
net_utils_base.h:323
epee::net_utils::jsonrpc2::i_jsonrpc2_server_handler
Definition
jsonrpc_protocol_handler.h:35
epee::net_utils::jsonrpc2::i_jsonrpc2_server_handler::~i_jsonrpc2_server_handler
virtual ~i_jsonrpc2_server_handler()
Definition
jsonrpc_protocol_handler.h:36
epee::net_utils::jsonrpc2::i_jsonrpc2_server_handler::init_server_thread
virtual bool init_server_thread()
Definition
jsonrpc_protocol_handler.h:41
epee::net_utils::jsonrpc2::i_jsonrpc2_server_handler::handle_rpc_request
virtual bool handle_rpc_request(const std::string &req_data, std::string &resp_data, t_connection_context &conn_context)=0
epee::net_utils::jsonrpc2::i_jsonrpc2_server_handler::deinit_server_thread
virtual bool deinit_server_thread()
Definition
jsonrpc_protocol_handler.h:43
epee::net_utils::jsonrpc2::jsonrpc2_server_config
Definition
jsonrpc_protocol_handler.h:49
epee::net_utils::jsonrpc2::jsonrpc2_server_config::m_phandler
i_jsonrpc2_server_handler< t_connection_context > * m_phandler
Definition
jsonrpc_protocol_handler.h:50
epee::net_utils::jsonrpc2::jsonrpc2_server_config::m_lock
critical_section m_lock
Definition
jsonrpc_protocol_handler.h:51
contrib
epee
include
net
jsonrpc_protocol_handler.h
Generated on
for Electroneum by
1.17.0