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] {
49 m_socket.shutdown(boost::asio::local::datagram_protocol::socket::shutdown_both);
59 m_socket.async_receive_from(
60 boost::asio::mutable_buffer(&m_data[0], std::size(m_data)), m_endpoint,
61 [
this, f](boost::system::error_code ec, std::size_t sz) {
62 if(ec == boost::asio::error::operation_aborted)
78 void write(
const char* data, std::size_t sz)
80 m_socket.send_to(boost::asio::buffer(data, sz), m_endpoint);
83 Nano::Signal<void()> on_close;
85 boost::asio::io_context& m_context;
86 proto::endpoint m_endpoint;
87 proto::socket m_socket;
88 alignas(16)
char m_data[65535];
91class unix_stream_listener
94 using proto = boost::asio::local::stream_protocol;
95 unix_stream_listener() =
delete;
96 unix_stream_listener(
const unix_stream_listener&) =
delete;
97 unix_stream_listener& operator=(
const unix_stream_listener&) =
delete;
98 unix_stream_listener(unix_stream_listener&&) =
default;
99 unix_stream_listener& operator=(unix_stream_listener&&) =
default;
100 explicit unix_stream_listener(proto::socket sock)
101 : m_socket{std::move(sock)}
107 m_socket.shutdown(boost::asio::local::stream_protocol::socket::shutdown_both);
111 void write(
const boost::asio::const_buffer& buf) { boost::asio::write(m_socket, buf); }
117 proto::socket m_socket;
120class unix_stream_server
123 using proto = boost::asio::local::stream_protocol;
124 using listener = unix_stream_listener;
125 [[no_unique_address]]
struct ensure_reuse
127 explicit ensure_reuse(
const proto::endpoint& endpoint)
129 ::unlink(endpoint.path().data());
133 unix_stream_server(
const fd_configuration& conf, boost::asio::io_context& ctx)
134 : m_ensure_reuse{conf.fd}
136 , m_acceptor{boost::asio::make_strand(ctx), conf.fd}
140 unix_stream_server(
const fd_configuration& conf, ossia::net::network_context_ptr ctx)
141 : unix_stream_server{conf, ctx->context}
145 boost::asio::io_context& m_context;
146 proto::acceptor m_acceptor;
149class unix_stream_client
152 using proto = boost::asio::local::stream_protocol;
153 using socket =
typename proto::socket;
155 unix_stream_client(
const fd_configuration& conf, boost::asio::io_context& ctx)
157 , m_endpoint{conf.fd}
158 , m_socket{boost::asio::make_strand(ctx)}
164 m_socket.connect(m_endpoint);
168 bool connected()
const {
return m_connected; }
172 boost::asio::post(m_context, [
this] {
173 m_socket.shutdown(boost::asio::local::stream_protocol::socket::shutdown_both);
179 void write(
const char* data, std::size_t sz)
181 boost::asio::write(m_socket, boost::asio::buffer(data, sz));
184 Nano::Signal<void()> on_open;
185 Nano::Signal<void()> on_close;
186 Nano::Signal<void()> on_fail;
188 boost::asio::io_context& m_context;
189 proto::endpoint m_endpoint;
190 proto::socket m_socket;
191 bool m_connected{
false};