Merge pull request #4976

85665003 epee: better network buffer data structure (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2019-01-16 19:04:22 +02:00
commit 846362842c
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
22 changed files with 325 additions and 85 deletions

View file

@ -97,7 +97,7 @@ namespace epee
return false;
}
return serialization::load_t_from_binary(result_struct, pri->m_body);
return serialization::load_t_from_binary(result_struct, epee::strspan<uint8_t>(pri->m_body));
}
template<class t_request, class t_response, class t_transport>

View file

@ -28,6 +28,7 @@
#include "portable_storage_template_helper.h"
#include <boost/utility/value_init.hpp>
#include "span.h"
#include "net/levin_base.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@ -114,7 +115,7 @@ namespace epee
const_cast<t_arg&>(out_struct).store(stg);//TODO: add true const support to searilzation
std::string buff_to_send;
stg.store_to_binary(buff_to_send);
int res = transport.invoke_async(command, buff_to_send, conn_id, [cb, command](int code, const std::string& buff, typename t_transport::connection_context& context)->bool
int res = transport.invoke_async(command, epee::strspan<uint8_t>(buff_to_send), conn_id, [cb, command](int code, const epee::span<const uint8_t> buff, typename t_transport::connection_context& context)->bool
{
t_result result_struct = AUTO_VAL_INIT(result_struct);
if( code <=0 )
@ -156,7 +157,7 @@ namespace epee
std::string buff_to_send;
stg.store_to_binary(buff_to_send);
int res = transport.notify(command, buff_to_send, conn_id);
int res = transport.notify(command, epee::strspan<uint8_t>(buff_to_send), conn_id);
if(res <=0 )
{
MERROR("Failed to notify command " << command << " return code " << res);
@ -167,7 +168,7 @@ namespace epee
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
template<class t_owner, class t_in_type, class t_out_type, class t_context, class callback_t>
int buff_to_t_adapter(int command, const std::string& in_buff, std::string& buff_out, callback_t cb, t_context& context )
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 )
{
serialization::portable_storage strg;
if(!strg.load_from_binary(in_buff))
@ -197,7 +198,7 @@ namespace epee
}
template<class t_owner, class t_in_type, class t_context, class callback_t>
int buff_to_t_adapter(t_owner* powner, int command, const std::string& in_buff, callback_t cb, t_context& context)
int buff_to_t_adapter(t_owner* powner, int command, const epee::span<const uint8_t> in_buff, callback_t cb, t_context& context)
{
serialization::portable_storage strg;
if(!strg.load_from_binary(in_buff))
@ -215,14 +216,14 @@ namespace epee
}
#define CHAIN_LEVIN_INVOKE_MAP2(context_type) \
int invoke(int command, const std::string& in_buff, std::string& buff_out, context_type& context) \
int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, context_type& context) \
{ \
bool handled = false; \
return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \
}
#define CHAIN_LEVIN_NOTIFY_MAP2(context_type) \
int notify(int command, const std::string& in_buff, context_type& context) \
int notify(int command, const epee::span<const uint8_t> in_buff, context_type& context) \
{ \
bool handled = false; std::string fake_str;\
return handle_invoke_map(true, command, in_buff, fake_str, context, handled); \
@ -230,27 +231,27 @@ namespace epee
#define CHAIN_LEVIN_INVOKE_MAP() \
int invoke(int command, const std::string& in_buff, std::string& buff_out, epee::net_utils::connection_context_base& context) \
int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, epee::net_utils::connection_context_base& context) \
{ \
bool handled = false; \
return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \
}
#define CHAIN_LEVIN_NOTIFY_MAP() \
int notify(int command, const std::string& in_buff, epee::net_utils::connection_context_base& context) \
int notify(int command, const epee::span<const uint8_t> in_buff, epee::net_utils::connection_context_base& context) \
{ \
bool handled = false; std::string fake_str;\
return handle_invoke_map(true, command, in_buff, fake_str, context, handled); \
}
#define CHAIN_LEVIN_NOTIFY_STUB() \
int notify(int command, const std::string& in_buff, epee::net_utils::connection_context_base& context) \
int notify(int command, const epee::span<const uint8_t> in_buff, epee::net_utils::connection_context_base& context) \
{ \
return -1; \
}
#define BEGIN_INVOKE_MAP2(owner_type) \
template <class t_context> int handle_invoke_map(bool is_notify, int command, const std::string& in_buff, std::string& buff_out, t_context& context, bool& handled) \
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) \
{ \
typedef owner_type internal_owner_type_name;

View file

@ -35,6 +35,7 @@
#include "portable_storage_to_json.h"
#include "portable_storage_from_json.h"
#include "portable_storage_val_converters.h"
#include "span.h"
#include "int-util.h"
namespace epee
@ -81,7 +82,8 @@ namespace epee
//-------------------------------------------------------------------------------
bool store_to_binary(binarybuffer& target);
bool load_from_binary(const binarybuffer& target);
bool load_from_binary(const epee::span<const uint8_t> target);
bool load_from_binary(const std::string& target) { return load_from_binary(epee::strspan<uint8_t>(target)); }
template<class trace_policy>
bool dump_as_xml(std::string& targetObj, const std::string& root_name = "");
bool dump_as_json(std::string& targetObj, size_t indent = 0, bool insert_newlines = true);
@ -146,7 +148,7 @@ namespace epee
CATCH_ENTRY("portable_storage::store_to_binary", false)
}
inline
bool portable_storage::load_from_binary(const binarybuffer& source)
bool portable_storage::load_from_binary(const epee::span<const uint8_t> source)
{
m_root.m_entries.clear();
if(source.size() < sizeof(storage_block_header))

View file

@ -84,7 +84,7 @@ namespace epee
}
//-----------------------------------------------------------------------------------------------------------
template<class t_struct>
bool load_t_from_binary(t_struct& out, const std::string& binary_buff)
bool load_t_from_binary(t_struct& out, const epee::span<const uint8_t> binary_buff)
{
portable_storage ps;
bool rs = ps.load_from_binary(binary_buff);
@ -95,6 +95,12 @@ namespace epee
}
//-----------------------------------------------------------------------------------------------------------
template<class t_struct>
bool load_t_from_binary(t_struct& out, const std::string& binary_buff)
{
return load_t_from_binary(out, epee::strspan<uint8_t>(binary_buff));
}
//-----------------------------------------------------------------------------------------------------------
template<class t_struct>
bool load_t_from_binary_file(t_struct& out, const std::string& binary_file)
{
std::string f_buff;