30#include <boost/utility/string_ref.hpp>
31#include <boost/utility/value_init.hpp>
37#undef MONERO_DEFAULT_LOG_CATEGORY
38#define MONERO_DEFAULT_LOG_CATEGORY "net"
40template<
typename context_t>
41void on_levin_traffic(
const context_t &context,
bool initiator,
bool sent,
bool error,
size_t bytes,
const char *category);
43template<
typename context_t>
44void on_levin_traffic(
const context_t &context,
bool initiator,
bool sent,
bool error,
size_t bytes,
int command);
59 template<
class t_arg,
class t_result,
class t_transport>
62 const boost::uuids::uuid &conn_id = context.m_connection_id;
64 out_struct.store(stg);
66 std::string buff_to_recv;
69 int res = transport.invoke(command, std::move(to_send), buff_to_recv, conn_id);
72 LOG_PRINT_L1(
"Failed to invoke command " << command <<
" return code " <<
res);
79 LOG_ERROR(
"Failed to load_from_binary on command " << command);
83 return result_struct.load(stg_ret);
86 template<
class t_result,
class t_arg,
class callback_t,
class t_transport>
89 const boost::uuids::uuid &conn_id = context.m_connection_id;
91 const_cast<t_arg&
>(out_struct).store(stg);
94 int res = transport.invoke_async(command, std::move(to_send), conn_id, [cb, command](
int code,
const epee::span<const uint8_t> buff,
typename t_transport::connection_context& context)->
bool
101 LOG_PRINT_L1(
"Failed to invoke command " << command <<
" return code " <<
code);
102 cb(
code, result_struct, context);
109 LOG_ERROR(
"Failed to load_from_binary on command " << command);
113 if (!result_struct.load(stg_ret))
116 LOG_ERROR(
"Failed to load result struct on command " << command);
121 cb(
code, result_struct, context);
126 LOG_PRINT_L1(
"Failed to invoke command " << command <<
" return code " <<
res);
132 template<
class t_arg,
class t_transport>
133 bool notify_remote_command2(
const typename t_transport::connection_context &context,
int command,
const t_arg& out_struct, t_transport& transport)
135 const boost::uuids::uuid &conn_id = context.m_connection_id;
137 out_struct.store(stg);
144 MERROR(
"Failed to notify command " << command <<
" return code " <<
res);
151 template<
class t_owner,
class t_in_type,
class t_out_type,
class t_context,
class callback_t>
158 LOG_ERROR(
"Failed to load_from_binary in command " << command);
161 boost::value_initialized<t_in_type> in_struct;
162 boost::value_initialized<t_out_type> out_struct;
164 if (!
static_cast<t_in_type&
>(in_struct).load(strg))
167 LOG_ERROR(
"Failed to load in_struct in command " << command);
171 int res = cb(command,
static_cast<t_in_type&
>(in_struct),
static_cast<t_out_type&
>(out_struct), context);
173 static_cast<t_out_type&
>(out_struct).store(strg_out);
177 LOG_ERROR(
"Failed to store_to_binary in command" << command);
184 template<
class t_owner,
class t_in_type,
class t_context,
class callback_t>
191 LOG_ERROR(
"Failed to load_from_binary in notify " << command);
194 boost::value_initialized<t_in_type> in_struct;
195 if (!
static_cast<t_in_type&
>(in_struct).load(strg))
198 LOG_ERROR(
"Failed to load in_struct in notify " << command);
202 return cb(command, in_struct, context);
205#define CHAIN_LEVIN_INVOKE_MAP2(context_type) \
206 int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_stream& buff_out, context_type& context) \
208 bool handled = false; \
209 return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \
212#define CHAIN_LEVIN_NOTIFY_MAP2(context_type) \
213 int notify(int command, const epee::span<const uint8_t> in_buff, context_type& context) \
215 bool handled = false; epee::byte_stream fake_str; \
216 return handle_invoke_map(true, command, in_buff, fake_str, context, handled); \
220#define CHAIN_LEVIN_INVOKE_MAP() \
221 int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_stream& buff_out, epee::net_utils::connection_context_base& context) \
223 bool handled = false; \
224 return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \
227#define CHAIN_LEVIN_NOTIFY_MAP() \
228 int notify(int command, const epee::span<const uint8_t> in_buff, epee::net_utils::connection_context_base& context) \
230 bool handled = false; std::string fake_str;\
231 return handle_invoke_map(true, command, in_buff, fake_str, context, handled); \
234#define CHAIN_LEVIN_NOTIFY_STUB() \
235 int notify(int command, const epee::span<const uint8_t> in_buff, epee::net_utils::connection_context_base& context) \
240#define BEGIN_INVOKE_MAP2(owner_type) \
241 template <class t_context> int handle_invoke_map(bool is_notify, int command, const epee::span<const uint8_t> in_buff, epee::byte_stream& buff_out, t_context& context, bool& handled) \
244 typedef owner_type internal_owner_type_name;
246#define HANDLE_INVOKE2(command_id, func, type_name_in, typename_out) \
247 if(!is_notify && command_id == command) \
248 {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);}
250#define HANDLE_INVOKE_T2(COMMAND, func) \
251 if(!is_notify && COMMAND::ID == command) \
252 {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);}
255#define HANDLE_NOTIFY2(command_id, func, type_name_in) \
256 if(is_notify && command_id == command) \
257 {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);}
259#define HANDLE_NOTIFY_T2(NOTIFY, func) \
260 if(is_notify && NOTIFY::ID == command) \
261 {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);}
264#define CHAIN_INVOKE_MAP2(func) \
266 int res = func(is_notify, command, in_buff, buff_out, context, handled); \
271#define CHAIN_INVOKE_MAP_TO_OBJ2(obj) \
273 int res = obj.handle_invoke_map(is_notify, command, in_buff, buff_out, context, handled); \
278#define CHAIN_INVOKE_MAP_TO_OBJ_FORCE_CONTEXT(obj, context_type) \
280 int res = obj.handle_invoke_map(is_notify, command, in_buff, buff_out, static_cast<context_type>(context), handled); \
281 if(handled) return res; \
285#define END_INVOKE_MAP2() \
286 LOG_ERROR("Unknown command:" << command); \
287 on_levin_traffic(context, false, false, true, in_buff.size(), "invalid-command"); \
288 return LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED; \
290 catch (const std::exception &e) { \
291 MERROR("Error in handle_invoke_map: " << e.what()); \
292 return LEVIN_ERROR_CONNECTION_TIMEDOUT; \
A partial drop-in replacement for std::ostream.
Definition byte_stream.h:58
Provides space for levin (p2p) header, so that payload can be sent without copy.
Definition levin_base.h:132
byte_stream buffer
Has space for levin header until a finalize method is used.
Definition levin_base.h:159
byte_slice finalize_notify(uint32_t command)
Definition levin_base.h:152
Definition portable_storage.h:46
bool load_from_binary(const epee::span< const uint8_t > target, const limits_t *limits=nullptr)
Definition portable_storage.cpp:87
bool store_to_binary(byte_slice &target, std::size_t initial_buffer_size=8192)
Definition portable_storage.cpp:46
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
constexpr std::size_t size() const noexcept
Definition span.h:109
constexpr bool empty() const noexcept
Definition span.h:107
const char * res
Definition hmac_keccak.cpp:42
void on_levin_traffic(const context_t &context, bool initiator, bool sent, bool error, size_t bytes, const char *category)
Definition levin_protocol_handler_async.h:54
#define LEVIN_ERROR_FORMAT
Definition levin_base.h:109
#define LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED
Definition levin_base.h:75
void on_levin_traffic(const context_t &context, bool initiator, bool sent, bool error, size_t bytes, const char *category)
Definition levin_protocol_handler_async.h:54
#define AUTO_VAL_INIT(v)
Definition misc_language.h:36
Definition abstract_http_client.h:36
bool invoke_remote_command2(const epee::net_utils::connection_context_base context, int command, const t_arg &out_struct, t_result &result_struct, t_transport &transport)
Definition levin_abstract_invoke2.h:60
bool notify_remote_command2(const typename t_transport::connection_context &context, int command, const t_arg &out_struct, t_transport &transport)
Definition levin_abstract_invoke2.h:133
int buff_to_t_adapter(int command, const epee::span< const uint8_t > in_buff, byte_stream &buff_out, callback_t cb, t_context &context)
Definition levin_abstract_invoke2.h:152
bool async_invoke_remote_command2(const epee::net_utils::connection_context_base &context, int command, const t_arg &out_struct, t_transport &transport, const callback_t &cb, size_t inv_timeout=LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED)
Definition levin_abstract_invoke2.h:87
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
const portMappingElt code
Definition portlistingparse.c:22
Definition net_utils_base.h:367
Definition portable_storage.h:53