From 7414e2bac11cbf9163ca16dc1b1510b4fd9a0d64 Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Tue, 13 Oct 2020 15:15:07 +0000 Subject: Change epee binary output from std::stringstream to byte_stream --- .../epee/include/storages/http_abstract_invoke.h | 7 ++--- .../epee/include/storages/levin_abstract_invoke2.h | 31 ++++++++++++---------- contrib/epee/include/storages/portable_storage.h | 20 ++------------ .../storages/portable_storage_template_helper.h | 11 ++++---- 4 files changed, 29 insertions(+), 40 deletions(-) (limited to 'contrib/epee/include/storages') diff --git a/contrib/epee/include/storages/http_abstract_invoke.h b/contrib/epee/include/storages/http_abstract_invoke.h index a8bc945a8..c4cb91130 100644 --- a/contrib/epee/include/storages/http_abstract_invoke.h +++ b/contrib/epee/include/storages/http_abstract_invoke.h @@ -29,6 +29,7 @@ #include #include #include +#include "byte_slice.h" #include "portable_storage_template_helper.h" #include "net/http_base.h" #include "net/http_server_handlers_map2.h" @@ -74,12 +75,12 @@ namespace epee template bool invoke_http_bin(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "POST") { - std::string req_param; - if(!serialization::store_t_to_binary(out_struct, req_param)) + byte_slice req_param; + if(!serialization::store_t_to_binary(out_struct, req_param, 16 * 1024)) return false; const http::http_response_info* pri = NULL; - if(!transport.invoke(uri, method, req_param, timeout, std::addressof(pri))) + if(!transport.invoke(uri, method, boost::string_ref{reinterpret_cast(req_param.data()), req_param.size()}, timeout, std::addressof(pri))) { LOG_PRINT_L1("Failed to invoke http request to " << uri); return false; diff --git a/contrib/epee/include/storages/levin_abstract_invoke2.h b/contrib/epee/include/storages/levin_abstract_invoke2.h index cf1262486..95f0bb410 100644 --- a/contrib/epee/include/storages/levin_abstract_invoke2.h +++ b/contrib/epee/include/storages/levin_abstract_invoke2.h @@ -27,8 +27,10 @@ #pragma once #include "portable_storage_template_helper.h" +#include #include #include +#include "byte_slice.h" #include "span.h" #include "net/levin_base.h" @@ -110,11 +112,12 @@ namespace epee const boost::uuids::uuid &conn_id = context.m_connection_id; typename serialization::portable_storage stg; out_struct.store(stg); - std::string buff_to_send, buff_to_recv; - stg.store_to_binary(buff_to_send); + byte_slice buff_to_send; + std::string buff_to_recv; + stg.store_to_binary(buff_to_send, 16 * 1024); on_levin_traffic(context, true, true, false, buff_to_send.size(), command); - int res = transport.invoke(command, buff_to_send, buff_to_recv, conn_id); + int res = transport.invoke(command, boost::string_ref{reinterpret_cast(buff_to_send.data()), buff_to_send.size()}, buff_to_recv, conn_id); if( res <=0 ) { LOG_PRINT_L1("Failed to invoke command " << command << " return code " << res); @@ -137,10 +140,10 @@ namespace epee const boost::uuids::uuid &conn_id = context.m_connection_id; typename serialization::portable_storage stg; const_cast(out_struct).store(stg);//TODO: add true const support to searilzation - std::string buff_to_send; - stg.store_to_binary(buff_to_send); + byte_slice buff_to_send; + stg.store_to_binary(buff_to_send, 16 * 1024); on_levin_traffic(context, true, true, false, buff_to_send.size(), command); - int res = transport.invoke_async(command, epee::strspan(buff_to_send), conn_id, [cb, command](int code, const epee::span buff, typename t_transport::connection_context& context)->bool + int res = transport.invoke_async(command, epee::to_span(buff_to_send), conn_id, [cb, command](int code, const epee::span buff, typename t_transport::connection_context& context)->bool { t_result result_struct = AUTO_VAL_INIT(result_struct); if( code <=0 ) @@ -184,11 +187,11 @@ namespace epee const boost::uuids::uuid &conn_id = context.m_connection_id; serialization::portable_storage stg; out_struct.store(stg); - std::string buff_to_send; + byte_slice buff_to_send; stg.store_to_binary(buff_to_send); on_levin_traffic(context, true, true, false, buff_to_send.size(), command); - int res = transport.notify(command, epee::strspan(buff_to_send), conn_id); + int res = transport.notify(command, epee::to_span(buff_to_send), conn_id); if(res <=0 ) { MERROR("Failed to notify command " << command << " return code " << res); @@ -199,7 +202,7 @@ namespace epee //---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- template - int buff_to_t_adapter(int command, const epee::span in_buff, std::string& buff_out, callback_t cb, t_context& context ) + int buff_to_t_adapter(int command, const epee::span in_buff, byte_slice& buff_out, callback_t cb, t_context& context ) { serialization::portable_storage strg; if(!strg.load_from_binary(in_buff)) @@ -222,7 +225,7 @@ namespace epee serialization::portable_storage strg_out; static_cast(out_struct).store(strg_out); - if(!strg_out.store_to_binary(buff_out)) + if(!strg_out.store_to_binary(buff_out, 32 * 1024)) { LOG_ERROR("Failed to store_to_binary in command" << command); return -1; @@ -254,7 +257,7 @@ namespace epee } #define CHAIN_LEVIN_INVOKE_MAP2(context_type) \ - int invoke(int command, const epee::span in_buff, std::string& buff_out, context_type& context) \ + int invoke(int command, const epee::span in_buff, epee::byte_slice& buff_out, context_type& context) \ { \ bool handled = false; \ return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \ @@ -263,13 +266,13 @@ namespace epee #define CHAIN_LEVIN_NOTIFY_MAP2(context_type) \ int notify(int command, const epee::span in_buff, context_type& context) \ { \ - bool handled = false; std::string fake_str;\ + bool handled = false; epee::byte_slice fake_str; \ return handle_invoke_map(true, command, in_buff, fake_str, context, handled); \ } #define CHAIN_LEVIN_INVOKE_MAP() \ - int invoke(int command, const epee::span in_buff, std::string& buff_out, epee::net_utils::connection_context_base& context) \ + int invoke(int command, const epee::span in_buff, epee::byte_slice& buff_out, epee::net_utils::connection_context_base& context) \ { \ bool handled = false; \ return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \ @@ -289,7 +292,7 @@ namespace epee } #define BEGIN_INVOKE_MAP2(owner_type) \ - template int handle_invoke_map(bool is_notify, int command, const epee::span in_buff, std::string& buff_out, t_context& context, bool& handled) \ + template int handle_invoke_map(bool is_notify, int command, const epee::span in_buff, epee::byte_slice& buff_out, t_context& context, bool& handled) \ { \ try { \ typedef owner_type internal_owner_type_name; diff --git a/contrib/epee/include/storages/portable_storage.h b/contrib/epee/include/storages/portable_storage.h index 4b759a24f..589e6ad63 100644 --- a/contrib/epee/include/storages/portable_storage.h +++ b/contrib/epee/include/storages/portable_storage.h @@ -32,7 +32,6 @@ #include "misc_language.h" #include "portable_storage_base.h" -#include "portable_storage_to_bin.h" #include "portable_storage_from_bin.h" #include "portable_storage_to_json.h" #include "portable_storage_from_json.h" @@ -42,6 +41,7 @@ namespace epee { + class byte_slice; namespace serialization { /************************************************************************/ @@ -83,7 +83,7 @@ namespace epee bool delete_entry(const std::string& pentry_name, hsection hparent_section = nullptr); //------------------------------------------------------------------------------- - bool store_to_binary(binarybuffer& target); + bool store_to_binary(byte_slice& target, std::size_t initial_buffer_size = 8192); bool load_from_binary(const epee::span target); bool load_from_binary(const std::string& target) { return load_from_binary(epee::strspan(target)); } template @@ -133,22 +133,6 @@ namespace epee { return false;//TODO: don't think i ever again will use xml - ambiguous and "overtagged" format } - - inline - bool portable_storage::store_to_binary(binarybuffer& target) - { - TRY_ENTRY(); - std::stringstream ss; - storage_block_header sbh = AUTO_VAL_INIT(sbh); - sbh.m_signature_a = SWAP32LE(PORTABLE_STORAGE_SIGNATUREA); - sbh.m_signature_b = SWAP32LE(PORTABLE_STORAGE_SIGNATUREB); - sbh.m_ver = PORTABLE_STORAGE_FORMAT_VER; - ss.write((const char*)&sbh, sizeof(storage_block_header)); - pack_entry_to_buff(ss, m_root); - target = ss.str(); - return true; - CATCH_ENTRY("portable_storage::store_to_binary", false) - } inline bool portable_storage::load_from_binary(const epee::span source) { diff --git a/contrib/epee/include/storages/portable_storage_template_helper.h b/contrib/epee/include/storages/portable_storage_template_helper.h index 13c870d44..39f900c8d 100644 --- a/contrib/epee/include/storages/portable_storage_template_helper.h +++ b/contrib/epee/include/storages/portable_storage_template_helper.h @@ -28,6 +28,7 @@ #include +#include "byte_slice.h" #include "parserse_base_utils.h" #include "portable_storage.h" #include "file_io_utils.h" @@ -111,18 +112,18 @@ namespace epee } //----------------------------------------------------------------------------------------------------------- template - bool store_t_to_binary(t_struct& str_in, std::string& binary_buff, size_t indent = 0) + bool store_t_to_binary(t_struct& str_in, byte_slice& binary_buff, size_t initial_buffer_size = 8192) { portable_storage ps; str_in.store(ps); - return ps.store_to_binary(binary_buff); + return ps.store_to_binary(binary_buff, initial_buffer_size); } //----------------------------------------------------------------------------------------------------------- template - std::string store_t_to_binary(t_struct& str_in, size_t indent = 0) + byte_slice store_t_to_binary(t_struct& str_in, size_t initial_buffer_size = 8192) { - std::string binary_buff; - store_t_to_binary(str_in, binary_buff, indent); + byte_slice binary_buff; + store_t_to_binary(str_in, binary_buff, initial_buffer_size); return binary_buff; } } -- cgit v1.2.3