2#include <ossia/detail/pod_vector.hpp>
3#include <ossia/network/sockets/writers.hpp>
5#include <boost/asio/buffer.hpp>
6#include <boost/asio/error.hpp>
7#include <boost/asio/read.hpp>
8#include <boost/asio/write.hpp>
13template <
typename Socket>
14struct fixed_length_decoder
17 std::size_t frame_size{64};
18 ossia::pod_vector<char> m_data;
19 lifetime_token m_lifetime;
21 explicit fixed_length_decoder(Socket& socket)
29 m_data.resize(frame_size, boost::container::default_init);
30 boost::asio::async_read(
31 socket, boost::asio::mutable_buffer(m_data.data(), frame_size),
32 boost::asio::transfer_exactly(frame_size),
33 [
this, alive = m_lifetime.watch(),
34 f = std::move(f)](boost::system::error_code ec, std::size_t sz)
mutable {
39 if(!f.validate_stream(ec))
46 f((const unsigned char*)m_data.data(), sz);
53 this->receive(std::move(f));
58template <
typename Socket>
59struct fixed_length_encoder
63 void write(
const char* data, std::size_t sz)
65 this->do_write(socket, boost::asio::buffer(data, sz));
69 void do_write(T& sock,
const boost::asio::const_buffer& buf)
71 boost::asio::write(sock, buf);
75 void do_write(multi_socket_writer<T>& sock,
const boost::asio::const_buffer& buf)
81struct fixed_length_framing
83 template <
typename Socket>
84 using encoder = fixed_length_encoder<Socket>;
85 template <
typename Socket>
86 using decoder = fixed_length_decoder<Socket>;