3#include <ossia/network/context.hpp>
4#include <ossia/network/sockets/configuration.hpp>
6#include <boost/asio/io_context.hpp>
7#include <boost/asio/ip/udp.hpp>
8#include <boost/asio/local/datagram_protocol.hpp>
9#include <boost/asio/local/stream_protocol.hpp>
10#include <boost/asio/placeholders.hpp>
11#include <boost/asio/strand.hpp>
12#include <boost/asio/write.hpp>
14#include <nano_signal_slot.hpp>
18#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
19class unix_datagram_socket
21 using proto = boost::asio::local::datagram_protocol;
24 unix_datagram_socket(
const fd_configuration& conf, boost::asio::io_context& ctx)
27 , m_socket{boost::asio::make_strand(ctx)}
33 ::unlink(m_endpoint.path().data());
35 m_socket.bind(m_endpoint);
46 if(m_socket.is_open())
48 boost::asio::post(m_context, [
this] {
58 m_socket.async_receive_from(
59 boost::asio::mutable_buffer(&m_data[0], std::size(m_data)), m_endpoint,
60 [
this, f](boost::system::error_code ec, std::size_t sz) {
61 if(ec == boost::asio::error::operation_aborted)
77 void write(
const char* data, std::size_t sz)
79 m_socket.send_to(boost::asio::buffer(data, sz), m_endpoint);
82 Nano::Signal<void()> on_close;
84 boost::asio::io_context& m_context;
85 proto::endpoint m_endpoint;
86 proto::socket m_socket;
87 alignas(16)
char m_data[65535];
90class unix_stream_listener
93 using proto = boost::asio::local::stream_protocol;
94 unix_stream_listener() =
delete;
95 unix_stream_listener(
const unix_stream_listener&) =
delete;
96 unix_stream_listener& operator=(
const unix_stream_listener&) =
delete;
97 unix_stream_listener(unix_stream_listener&&) =
default;
98 unix_stream_listener& operator=(unix_stream_listener&&) =
default;
99 explicit unix_stream_listener(proto::socket sock)
100 : m_socket{std::move(sock)}
104 void close() { m_socket.close(); }
106 void write(
const boost::asio::const_buffer& buf) { boost::asio::write(m_socket, buf); }
112 proto::socket m_socket;
115class unix_stream_server
118 using proto = boost::asio::local::stream_protocol;
119 using listener = unix_stream_listener;
120 [[no_unique_address]]
struct ensure_reuse
122 explicit ensure_reuse(
const proto::endpoint& endpoint)
124 ::unlink(endpoint.path().data());
128 unix_stream_server(
const fd_configuration& conf, boost::asio::io_context& ctx)
129 : m_ensure_reuse{conf.fd}
131 , m_acceptor{boost::asio::make_strand(ctx), conf.fd}
135 unix_stream_server(
const fd_configuration& conf, ossia::net::network_context_ptr ctx)
136 : unix_stream_server{conf, ctx->context}
140 boost::asio::io_context& m_context;
141 proto::acceptor m_acceptor;
144class unix_stream_client
147 using proto = boost::asio::local::stream_protocol;
148 using socket =
typename proto::socket;
150 unix_stream_client(
const fd_configuration& conf, boost::asio::io_context& ctx)
152 , m_endpoint{conf.fd}
153 , m_socket{boost::asio::make_strand(ctx)}
159 m_socket.connect(m_endpoint);
163 bool connected()
const {
return m_connected; }
167 boost::asio::post(m_context, [
this] {
173 void write(
const char* data, std::size_t sz)
175 boost::asio::write(m_socket, boost::asio::buffer(data, sz));
178 Nano::Signal<void()> on_open;
179 Nano::Signal<void()> on_close;
180 Nano::Signal<void()> on_fail;
182 boost::asio::io_context& m_context;
183 proto::endpoint m_endpoint;
184 proto::socket m_socket;
185 bool m_connected{
false};