correct whitespacing in epee/*

This commit is contained in:
Erik Kimmo 2014-05-16 20:13:38 +04:00
parent ae315cfa1a
commit 033724ec4d
3 changed files with 199 additions and 202 deletions

View file

@ -55,20 +55,20 @@ namespace net_utils
/************************************************************************/ /************************************************************************/
/* */ /* */
/************************************************************************/ /************************************************************************/
template<class t_connection_context = net_utils::connection_context_base> template<class t_connection_context = net_utils::connection_context_base>
class simple_http_connection_handler class simple_http_connection_handler
{ {
public: public:
typedef t_connection_context connection_context;//t_connection_context net_utils::connection_context_base connection_context; typedef t_connection_context connection_context;//t_connection_context net_utils::connection_context_base connection_context;
typedef http_server_config config_type; typedef http_server_config config_type;
simple_http_connection_handler(i_service_endpoint* psnd_hndlr, config_type& config); simple_http_connection_handler(i_service_endpoint* psnd_hndlr, config_type& config);
virtual ~simple_http_connection_handler(){} virtual ~simple_http_connection_handler(){}
bool release_protocol() bool release_protocol()
{ {
return true; return true;
} }
virtual bool thread_init() virtual bool thread_init()
{ {
@ -138,37 +138,37 @@ namespace net_utils
i_service_endpoint* m_psnd_hndlr; i_service_endpoint* m_psnd_hndlr;
}; };
template<class t_connection_context> template<class t_connection_context>
struct i_http_server_handler struct i_http_server_handler
{ {
virtual ~i_http_server_handler(){} virtual ~i_http_server_handler(){}
virtual bool handle_http_request(const http_request_info& query_info, virtual bool handle_http_request(const http_request_info& query_info,
http_response_info& response, http_response_info& response,
t_connection_context& m_conn_context) = 0; t_connection_context& m_conn_context) = 0;
virtual bool init_server_thread(){return true;} virtual bool init_server_thread(){return true;}
virtual bool deinit_server_thread(){return true;} virtual bool deinit_server_thread(){return true;}
}; };
template<class t_connection_context> template<class t_connection_context>
struct custum_handler_config: public http_server_config struct custum_handler_config: public http_server_config
{ {
i_http_server_handler<t_connection_context>* m_phandler; i_http_server_handler<t_connection_context>* m_phandler;
}; };
/************************************************************************/ /************************************************************************/
/* */ /* */
/************************************************************************/ /************************************************************************/
template<class t_connection_context = net_utils::connection_context_base> template<class t_connection_context = net_utils::connection_context_base>
class http_custom_handler: public simple_http_connection_handler<t_connection_context> class http_custom_handler: public simple_http_connection_handler<t_connection_context>
{ {
public: public:
typedef custum_handler_config<t_connection_context> config_type; typedef custum_handler_config<t_connection_context> config_type;
http_custom_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context) http_custom_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context)
: simple_http_connection_handler<t_connection_context>(psnd_hndlr, config), : simple_http_connection_handler<t_connection_context>(psnd_hndlr, config),
m_config(config), m_config(config),
m_conn_context(conn_context) m_conn_context(conn_context)
{} {}
inline bool handle_request(const http_request_info& query_info, http_response_info& response) inline bool handle_request(const http_request_info& query_info, http_response_info& response)
{ {
@ -190,8 +190,8 @@ namespace net_utils
{ {
return m_config.m_phandler->deinit_server_thread(); return m_config.m_phandler->deinit_server_thread();
} }
void handle_qued_callback() void handle_qued_callback()
{} {}
bool after_init_connection() bool after_init_connection()
{ {
return true; return true;

View file

@ -13,157 +13,154 @@ namespace epee
{ {
namespace net_utils namespace net_utils
{ {
namespace jsonrpc2 namespace jsonrpc2
{
inline
std::string& make_error_resp_json(int64_t code, const std::string& message,
std::string& response_data,
const epee::serialization::storage_entry& id = nullptr)
{
epee::json_rpc::error_response rsp;
rsp.id = id;
rsp.jsonrpc = "2.0";
rsp.error.code = code;
rsp.error.message = message;
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(rsp), response_data, 0, false);
response_data += "\n";
return response_data;
}
template<class t_connection_context>
struct i_jsonrpc2_server_handler
{ {
inline virtual ~i_jsonrpc2_server_handler()
std::string& make_error_resp_json(int64_t code, const std::string& message, {}
std::string& response_data, virtual bool handle_rpc_request(const std::string& req_data,
const epee::serialization::storage_entry& id = nullptr) std::string& resp_data,
{ t_connection_context& conn_context) = 0;
epee::json_rpc::error_response rsp; virtual bool init_server_thread()
rsp.id = id; { return true; }
rsp.jsonrpc = "2.0"; virtual bool deinit_server_thread()
rsp.error.code = code; { return true; }
rsp.error.message = message; };
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(rsp), response_data, 0, false);
response_data += "\n"; template<class t_connection_context>
return response_data; struct jsonrpc2_server_config
{
i_jsonrpc2_server_handler<t_connection_context>* m_phandler;
critical_section m_lock;
};
template<class t_connection_context = net_utils::connection_context_base>
class jsonrpc2_connection_handler
{
public:
typedef t_connection_context connection_context;
typedef jsonrpc2_server_config<t_connection_context> config_type;
jsonrpc2_connection_handler(i_service_endpoint* psnd_hndlr,
config_type& config,
t_connection_context& conn_context)
: m_psnd_hndlr(psnd_hndlr),
m_config(config),
m_conn_context(conn_context),
m_is_stop_handling(false)
{}
virtual ~jsonrpc2_connection_handler()
{}
bool release_protocol()
{
return true;
}
virtual bool thread_init()
{
return true;
}
virtual bool thread_deinit()
{
return true;
}
void handle_qued_callback()
{}
bool after_init_connection()
{
return true;
}
virtual bool handle_recv(const void* ptr, size_t cb)
{
std::string buf((const char*)ptr, cb);
LOG_PRINT_L0("JSONRPC2_RECV: " << ptr << "\r\n" << buf);
bool res = handle_buff_in(buf);
return res;
}
private:
bool handle_buff_in(std::string& buf)
{
if(m_cache.size())
m_cache += buf;
else
m_cache.swap(buf);
m_is_stop_handling = false;
while (!m_is_stop_handling) {
std::string::size_type pos = match_end_of_request(m_cache);
if (std::string::npos == pos) {
m_is_stop_handling = true;
if (m_cache.size() > 4096) {
LOG_ERROR("jsonrpc2_connection_handler::handle_buff_in: Too long request");
return false;
}
break;
} else {
extract_cached_request_and_handle(pos);
}
if (!m_cache.size()) {
m_is_stop_handling = true;
}
} }
template<class t_connection_context> return true;
struct i_jsonrpc2_server_handler }
{ bool extract_cached_request_and_handle(std::string::size_type pos)
virtual ~i_jsonrpc2_server_handler() {
{} std::string request_data(m_cache.begin(), m_cache.begin() + pos);
virtual bool handle_rpc_request(const std::string& req_data, m_cache.erase(0, pos);
std::string& resp_data, return handle_request_and_send_response(request_data);
t_connection_context& conn_context) = 0; }
virtual bool init_server_thread() bool handle_request_and_send_response(const std::string& request_data)
{ return true; } {
virtual bool deinit_server_thread() CHECK_AND_ASSERT_MES(m_config.m_phandler, false, "m_config.m_phandler is NULL!!!!");
{ return true; } std::string response_data;
};
template<class t_connection_context>
struct jsonrpc2_server_config
{
i_jsonrpc2_server_handler<t_connection_context>* m_phandler;
critical_section m_lock;
};
template<class t_connection_context = net_utils::connection_context_base>
class jsonrpc2_connection_handler
{
public:
typedef t_connection_context connection_context;
typedef jsonrpc2_server_config<t_connection_context> config_type;
jsonrpc2_connection_handler(i_service_endpoint* psnd_hndlr, LOG_PRINT_L3("JSONRPC2_REQUEST: >> \r\n" << request_data);
config_type& config, bool rpc_result = m_config.m_phandler->handle_rpc_request(request_data, response_data, m_conn_context);
t_connection_context& conn_context) LOG_PRINT_L3("JSONRPC2_RESPONSE: << \r\n" << response_data);
: m_psnd_hndlr(psnd_hndlr),
m_config(config),
m_conn_context(conn_context),
m_is_stop_handling(false)
{}
virtual ~jsonrpc2_connection_handler()
{}
bool release_protocol() m_psnd_hndlr->do_send((void*)response_data.data(), response_data.size());
{ return rpc_result;
return true; }
} std::string::size_type match_end_of_request(const std::string& buf)
virtual bool thread_init() {
{ std::string::size_type res = buf.find("\n");
return true; if(std::string::npos != res) {
} return res + 2;
virtual bool thread_deinit() }
{ return res;
return true; }
}
void handle_qued_callback()
{}
bool after_init_connection()
{
return true;
}
virtual bool handle_recv(const void* ptr, size_t cb)
{
std::string buf((const char*)ptr, cb);
LOG_PRINT_L0("JSONRPC2_RECV: " << ptr << "\r\n" << buf);
bool res = handle_buff_in(buf); protected:
// if (m_want_close) { i_service_endpoint* m_psnd_hndlr;
// return false;
// }
return res;
}
private:
bool handle_buff_in(std::string& buf)
{
if(m_cache.size())
m_cache += buf;
else
m_cache.swap(buf);
m_is_stop_handling = false; private:
while (!m_is_stop_handling) { config_type& m_config;
std::string::size_type pos = match_end_of_request(m_cache); t_connection_context& m_conn_context;
if (std::string::npos == pos) { std::string m_cache;
m_is_stop_handling = true; bool m_is_stop_handling;
if (m_cache.size() > 4096) { };
LOG_ERROR("jsonrpc2_connection_handler::handle_buff_in: Too long request"); }
return false;
}
break;
} else {
extract_cached_request_and_handle(pos);
}
if (!m_cache.size()) {
m_is_stop_handling = true;
}
}
return true;
}
bool extract_cached_request_and_handle(std::string::size_type pos)
{
std::string request_data(m_cache.begin(), m_cache.begin() + pos);
m_cache.erase(0, pos);
return handle_request_and_send_response(request_data);
}
bool handle_request_and_send_response(const std::string& request_data)
{
CHECK_AND_ASSERT_MES(m_config.m_phandler, false, "m_config.m_phandler is NULL!!!!");
std::string response_data;
LOG_PRINT_L3("JSONRPC2_REQUEST: >> \r\n" << request_data);
bool rpc_result = m_config.m_phandler->handle_rpc_request(request_data, response_data, m_conn_context);
LOG_PRINT_L3("JSONRPC2_RESPONSE: << \r\n" << response_data);
m_psnd_hndlr->do_send((void*)response_data.data(), response_data.size());
return rpc_result;
}
std::string::size_type match_end_of_request(const std::string& buf)
{
std::string::size_type res = buf.find("\n");
if(std::string::npos != res) {
return res + 2;
}
return res;
}
protected:
i_service_endpoint* m_psnd_hndlr;
private:
config_type& m_config;
t_connection_context& m_conn_context;
std::string m_cache;
bool m_is_stop_handling;
};
}
} }
} }

View file

@ -10,31 +10,31 @@
#define BEGIN_JSONRPC2_MAP(t_connection_context) \ #define BEGIN_JSONRPC2_MAP(t_connection_context) \
bool handle_rpc_request(const std::string& req_data, \ bool handle_rpc_request(const std::string& req_data, \
std::string& resp_data, \ std::string& resp_data, \
t_connection_context& m_conn_context) \ t_connection_context& m_conn_context) \
{ \ { \
bool handled = false; \ bool handled = false; \
uint64_t ticks = epee::misc_utils::get_tick_count(); \ uint64_t ticks = epee::misc_utils::get_tick_count(); \
epee::serialization::portable_storage ps; \ epee::serialization::portable_storage ps; \
if (!ps.load_from_json(req_data)) \ if (!ps.load_from_json(req_data)) \
{ \ { \
epee::net_utils::jsonrpc2::make_error_resp_json(-32700, "Parse error", resp_data); \ epee::net_utils::jsonrpc2::make_error_resp_json(-32700, "Parse error", resp_data); \
return true; \ return true; \
} \ } \
epee::serialization::storage_entry id_; \ epee::serialization::storage_entry id_; \
id_ = epee::serialization::storage_entry(std::string()); \ id_ = epee::serialization::storage_entry(std::string()); \
if (!ps.get_value("id", id_, nullptr)) \ if (!ps.get_value("id", id_, nullptr)) \
{ \ { \
epee::net_utils::jsonrpc2::make_error_resp_json(-32600, "Invalid Request", resp_data); \ epee::net_utils::jsonrpc2::make_error_resp_json(-32600, "Invalid Request", resp_data); \
return true; \ return true; \
} \ } \
std::string callback_name; \ std::string callback_name; \
if (!ps.get_value("method", callback_name, nullptr)) \ if (!ps.get_value("method", callback_name, nullptr)) \
{ \ { \
epee::net_utils::jsonrpc2::make_error_resp_json(-32600, "Invalid Request", resp_data, id_); \ epee::net_utils::jsonrpc2::make_error_resp_json(-32600, "Invalid Request", resp_data, id_); \
return true; \ return true; \
} \ } \
if (false) return true; //just a stub to have "else if" if (false) return true; //just a stub to have "else if"
@ -62,25 +62,25 @@ bool handle_rpc_request(const std::string& req_data, \
#define MAP_JSONRPC2_WE(method_name, callback_f, command_type) \ #define MAP_JSONRPC2_WE(method_name, callback_f, command_type) \
else if (callback_name == method_name) \ else if (callback_name == method_name) \
{ \
PREPARE_JSONRPC2_OBJECTS_FROM_JSON(command_type) \
epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \
fail_resp.jsonrpc = "2.0"; \
fail_resp.id = req.id; \
if(!callback_f(req.params, resp.result, fail_resp.error, m_conn_context)) \
{ \ { \
PREPARE_JSONRPC2_OBJECTS_FROM_JSON(command_type) \ epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), resp_data, 0, false); \
epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \ resp_data += "\n"; \
fail_resp.jsonrpc = "2.0"; \
fail_resp.id = req.id; \
if(!callback_f(req.params, resp.result, fail_resp.error, m_conn_context)) \
{ \
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), resp_data, 0, false); \
resp_data += "\n"; \
return true; \
} \
FINALIZE_JSONRPC2_OBJECTS_TO_JSON(method_name) \
return true; \ return true; \
} } \
FINALIZE_JSONRPC2_OBJECTS_TO_JSON(method_name) \
return true; \
}
#define END_JSONRPC2_MAP() \ #define END_JSONRPC2_MAP() \
epee::net_utils::jsonrpc2::make_error_resp_json(-32601, "Method not found", resp_data, id_); \ epee::net_utils::jsonrpc2::make_error_resp_json(-32601, "Method not found", resp_data, id_); \
return true; \ return true; \
} }
#endif /* JSONRPC_SERVER_HANDLERS_MAP_H */ #endif /* JSONRPC_SERVER_HANDLERS_MAP_H */