2#include <ossia/network/sockets/size_prefix_framing.hpp>
3#include <ossia/network/sockets/slip_framing.hpp>
4#include <ossia/network/sockets/tcp_socket.hpp>
5#include <ossia/network/sockets/udp_socket.hpp>
6#include <ossia/network/sockets/unix_socket.hpp>
7#include <ossia/network/sockets/writers.hpp>
12template <
typename Base,
typename Framing>
13struct framed_listener :
public Base
15 using proto =
typename Base::proto;
16 using socket =
typename proto::socket;
17 using decoder =
typename Framing::template decoder<socket>;
20 framed_listener(socket&& sock, F f)
21 : Base{std::move(sock)}
22 , m_decoder{this->m_socket}
24 m_decoder.receive(stream_processor<framed_listener, F>{*
this, std::move(f)});
30template <
typename Base,
typename Framing>
31struct framed_server : Base
34 using proto =
typename Base::proto;
35 using socket =
typename proto::socket;
36 using listener = framed_listener<typename Base::listener, Framing>;
38 typename Framing::template encoder<multi_socket_writer<std::unique_ptr<listener>>>;
39 using decoder =
typename Framing::template decoder<socket>;
41 template <
typename... Args>
42 framed_server(Args&&... args)
43 : Base{std::forward<Args>(args)...}
50 this->m_context.post([
this, f] {
51 for(
auto& sock : m_sockets)
62 this->m_acceptor.async_accept(
63 [
this, f = std::move(f)](std::error_code ec, socket socket) {
66 m_sockets.push_back(std::make_unique<listener>(std::move(socket), f));
73 void write(
const char* data, std::size_t sz)
75 multi_socket_writer<std::unique_ptr<listener>> wr{this->m_sockets};
76 encoder{wr}.write(data, sz);
79 std::vector<std::unique_ptr<listener>> m_sockets;
82template <
typename Base,
typename Framing>
83struct framed_client : Base
85 using socket =
typename Base::socket;
86 using encoder =
typename Framing::template encoder<socket>;
87 using decoder =
typename Framing::template decoder<socket>;
89 template <
typename... Args>
90 framed_client(Args&&... args)
91 : Base{std::forward<Args>(args)...}
92 , m_decoder{this->m_socket}
99 m_decoder.receive(stream_processor<framed_client, F>{*
this, std::move(f)});
102 void write(
const char* data, std::size_t sz)
104 encoder{this->m_socket}.write(data, sz);
110template <
typename Base,
typename Framing>
111struct framed_socket : Base
113 using socket =
typename Base::socket;
114 using encoder =
typename Framing::template encoder<socket>;
115 using decoder =
typename Framing::template decoder<socket>;
117 template <
typename... Args>
118 framed_socket(Args&&... args)
119 : Base{std::forward<Args>(args)...}
120 , m_decoder{this->m_socket}
124 template <
typename F>
128 m_decoder.receive(stream_processor<framed_socket, F>{*
this, std::move(f)});
131 template <
typename F>
134 m_decoder.receive(stream_processor<framed_socket, F>{*
this, std::move(f)});
137 void write(
const char* data, std::size_t sz)
139 encoder{this->m_socket}.write(data, sz);
145struct tcp_size_prefix_server : framed_server<tcp_server, size_prefix_framing>
147 using framed_server::framed_server;
149struct tcp_slip_server : framed_server<tcp_server, slip_framing>
151 using framed_server::framed_server;
154struct tcp_size_prefix_client : framed_client<tcp_client, size_prefix_framing>
156 using framed_client::framed_client;
158struct tcp_slip_client : framed_client<tcp_client, slip_framing>
160 using framed_client::framed_client;
163#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
164struct unix_stream_size_prefix_server
165 : framed_server<unix_stream_server, size_prefix_framing>
167 using framed_server::framed_server;
169struct unix_stream_slip_server : framed_server<unix_stream_server, slip_framing>
171 using framed_server::framed_server;
174struct unix_stream_size_prefix_client
175 : framed_socket<unix_stream_client, size_prefix_framing>
177 using framed_socket::framed_socket;
179struct unix_stream_slip_client : framed_socket<unix_stream_client, slip_framing>
181 using framed_socket::framed_socket;