30 #include <boost/utility/value_init.hpp> 35 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY 36 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net" 42 template<
class t_arg,
class t_result,
class t_transport>
49 out_struct.store(stg);
53 int res =
transport.invoke(command, buff_to_send, buff_to_recv);
56 MERROR(
"Failed to invoke command " << command <<
" return code " <<
res);
62 LOG_ERROR(
"Failed to load_from_binary on command " << command);
65 return result_struct.load(stg_ret);
68 template<
class t_arg,
class t_transport>
75 out_struct.store(&stg);
82 LOG_ERROR(
"Failed to notify command " << command <<
" return code " <<
res);
88 template<
class t_arg,
class t_result,
class t_transport>
93 out_struct.store(stg);
97 int res =
transport.invoke(command, buff_to_send, buff_to_recv, conn_id);
100 LOG_PRINT_L1(
"Failed to invoke command " << command <<
" return code " <<
res);
106 LOG_ERROR(
"Failed to load_from_binary on command " << command);
109 return result_struct.load(stg_ret);
112 template<
class t_result,
class t_arg,
class callback_t,
class t_transport>
116 const_cast<t_arg&
>(out_struct).store(stg);
124 LOG_PRINT_L1(
"Failed to invoke command " << command <<
" return code " << code);
125 cb(code, result_struct,
context);
131 LOG_ERROR(
"Failed to load_from_binary on command " << command);
135 if (!result_struct.load(stg_ret))
137 LOG_ERROR(
"Failed to load result struct on command " << command);
141 cb(code, result_struct,
context);
146 LOG_PRINT_L1(
"Failed to invoke command " << command <<
" return code " <<
res);
152 template<
class t_arg,
class t_transport>
157 out_struct.store(stg);
161 int res =
transport.notify(command, epee::strspan<uint8_t>(buff_to_send), conn_id);
164 MERROR(
"Failed to notify command " << command <<
" return code " <<
res);
171 template<
class t_owner,
class t_in_type,
class t_out_type,
class t_context,
class callback_t>
177 LOG_ERROR(
"Failed to load_from_binary in command " << command);
180 boost::value_initialized<t_in_type> in_struct;
181 boost::value_initialized<t_out_type> out_struct;
183 if (!static_cast<t_in_type&>(in_struct).
load(strg))
185 LOG_ERROR(
"Failed to load in_struct in command " << command);
188 int res = cb(command, static_cast<t_in_type&>(in_struct), static_cast<t_out_type&>(out_struct),
context);
190 static_cast<t_out_type&
>(out_struct).store(strg_out);
194 LOG_ERROR(
"Failed to store_to_binary in command" << command);
201 template<
class t_owner,
class t_in_type,
class t_context,
class callback_t>
207 LOG_ERROR(
"Failed to load_from_binary in notify " << command);
210 boost::value_initialized<t_in_type> in_struct;
211 if (!static_cast<t_in_type&>(in_struct).
load(strg))
213 LOG_ERROR(
"Failed to load in_struct in notify " << command);
216 return cb(command, in_struct,
context);
219 #define CHAIN_LEVIN_INVOKE_MAP2(context_type) \ 220 int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, context_type& context) \ 222 bool handled = false; \ 223 return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \ 226 #define CHAIN_LEVIN_NOTIFY_MAP2(context_type) \ 227 int notify(int command, const epee::span<const uint8_t> in_buff, context_type& context) \ 229 bool handled = false; std::string fake_str;\ 230 return handle_invoke_map(true, command, in_buff, fake_str, context, handled); \ 234 #define CHAIN_LEVIN_INVOKE_MAP() \ 235 int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, epee::net_utils::connection_context_base& context) \ 237 bool handled = false; \ 238 return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \ 241 #define CHAIN_LEVIN_NOTIFY_MAP() \ 242 int notify(int command, const epee::span<const uint8_t> in_buff, epee::net_utils::connection_context_base& context) \ 244 bool handled = false; std::string fake_str;\ 245 return handle_invoke_map(true, command, in_buff, fake_str, context, handled); \ 248 #define CHAIN_LEVIN_NOTIFY_STUB() \ 249 int notify(int command, const epee::span<const uint8_t> in_buff, epee::net_utils::connection_context_base& context) \ 254 #define BEGIN_INVOKE_MAP2(owner_type) \ 255 template <class t_context> int handle_invoke_map(bool is_notify, int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, t_context& context, bool& handled) \ 257 typedef owner_type internal_owner_type_name; 259 #define HANDLE_INVOKE2(command_id, func, type_name_in, typename_out) \ 260 if(!is_notify && command_id == command) \ 261 {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, type_name_in, typename_out>(this, command, in_buff, buff_out, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), context);} 263 #define HANDLE_INVOKE_T2(COMMAND, func) \ 264 if(!is_notify && COMMAND::ID == command) \ 265 {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, typename COMMAND::request, typename COMMAND::response>(command, in_buff, buff_out, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), context);} 268 #define HANDLE_NOTIFY2(command_id, func, type_name_in) \ 269 if(is_notify && command_id == command) \ 270 {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, type_name_in>(this, command, in_buff, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), context);} 272 #define HANDLE_NOTIFY_T2(NOTIFY, func) \ 273 if(is_notify && NOTIFY::ID == command) \ 274 {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, typename NOTIFY::request>(this, command, in_buff, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), context);} 277 #define CHAIN_INVOKE_MAP2(func) \ 279 int res = func(is_notify, command, in_buff, buff_out, context, handled); \ 284 #define CHAIN_INVOKE_MAP_TO_OBJ2(obj) \ 286 int res = obj.handle_invoke_map(is_notify, command, in_buff, buff_out, context, handled); \ 291 #define CHAIN_INVOKE_MAP_TO_OBJ_FORCE_CONTEXT(obj, context_type) \ 293 int res = obj.handle_invoke_map(is_notify, command, in_buff, buff_out, static_cast<context_type>(context), handled); \ 294 if(handled) return res; \ 298 #define END_INVOKE_MAP2() \ 299 LOG_ERROR("Unknown command:" << command); \ 300 return LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED; \
#define LEVIN_ERROR_FORMAT
bool notify_remote_command2(int command, const t_arg &out_struct, t_transport &transport)
Non-owning sequence of data. Does not deep copy.
bool load_from_binary(const epee::span< const uint8_t > target)
bool invoke_remote_command2(int command, const t_arg &out_struct, t_result &result_struct, t_transport &transport)
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
void load(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
int buff_to_t_adapter(int command, const epee::span< const uint8_t > in_buff, std::string &buff_out, callback_t cb, t_context &context)
bool store_to_binary(binarybuffer &target)
bool async_invoke_remote_command2(boost::uuids::uuid conn_id, int command, const t_arg &out_struct, t_transport &transport, const callback_t &cb, size_t inv_timeout=LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED)
#define LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED
std::shared_ptr< Transport > transport(const std::string &path)