dncurrency/nano/lib/asio.hpp
clemahieu 920d73a337
Renaming nano::transport::channel_loopback to nano::transport::inproc::channel. (#3805)
* Renaming nano::transport::channel_loopback to nano::transport::inproc::channel.

The name 'inproc' better indicates it's implementation through memory transfer, in contrast to the name 'loopback' might indicate an association with a network stack.

* Adding shared_const_buffer::to_bytes which copies all bytes in the buffer to a vector.

* Call the callback function and add some comments

Co-authored-by: Dimitrios Siganos <dimitris@siganos.org>
2022-05-04 21:54:45 +01:00

50 lines
1.8 KiB
C++

#pragma once
#include <nano/boost/asio/write.hpp>
namespace nano
{
class shared_const_buffer
{
public:
using value_type = boost::asio::const_buffer;
using const_iterator = boost::asio::const_buffer const *;
explicit shared_const_buffer (std::vector<uint8_t> const & data);
explicit shared_const_buffer (uint8_t data);
explicit shared_const_buffer (std::string const & data);
explicit shared_const_buffer (std::vector<uint8_t> && data);
explicit shared_const_buffer (std::shared_ptr<std::vector<uint8_t>> const & data);
boost::asio::const_buffer const * begin () const;
boost::asio::const_buffer const * end () const;
std::size_t size () const;
std::vector<uint8_t> to_bytes () const;
private:
std::shared_ptr<std::vector<uint8_t>> m_data;
boost::asio::const_buffer m_buffer;
};
static_assert (boost::asio::is_const_buffer_sequence<shared_const_buffer>::value, "Not ConstBufferSequence compliant");
template <typename AsyncWriteStream, typename WriteHandler>
BOOST_ASIO_INITFN_RESULT_TYPE (WriteHandler, void (boost::system::error_code, std::size_t))
async_write (AsyncWriteStream & s, nano::shared_const_buffer const & buffer, WriteHandler && handler)
{
return boost::asio::async_write (s, buffer, std::forward<WriteHandler> (handler));
}
/**
* Alternative to nano::async_write where scatter/gather is desired for best performance, and where
* the buffer originates from Flatbuffers.
* @warning The buffers must be captured in the handler to ensure their lifetimes are properly extended.
*/
template <typename AsyncWriteStream, typename BufferType, typename WriteHandler>
BOOST_ASIO_INITFN_RESULT_TYPE (WriteHandler, void (boost::system::error_code, std::size_t))
unsafe_async_write (AsyncWriteStream & s, BufferType && buffer, WriteHandler && handler)
{
return boost::asio::async_write (s, buffer, std::forward<WriteHandler> (handler));
}
}