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] {
51 m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
65 m_socket.async_receive_from(
66 boost::asio::mutable_buffer(&m_data[0], std::size(m_data)), m_endpoint,
67 [
this, f](boost::system::error_code ec, std::size_t sz) {
68 if(ec == boost::asio::error::operation_aborted)
84 void write(
const char* data, std::size_t sz)
86 m_socket.send_to(boost::asio::buffer(data, sz), m_endpoint);
89 ~unix_datagram_socket()
98 if(m_socket.is_open())
100 boost::system::error_code ec;
104 while(m_context.poll() > 0) { }
107 Nano::Signal<void()> on_close;
109 boost::asio::io_context& m_context;
110 proto::endpoint m_endpoint;
111 proto::socket m_socket;
112 alignas(16)
char m_data[65535];
115class unix_stream_listener
118 using proto = boost::asio::local::stream_protocol;
119 unix_stream_listener() =
delete;
120 unix_stream_listener(
const unix_stream_listener&) =
delete;
121 unix_stream_listener& operator=(
const unix_stream_listener&) =
delete;
122 unix_stream_listener(unix_stream_listener&&) =
default;
123 unix_stream_listener& operator=(unix_stream_listener&&) =
default;
124 explicit unix_stream_listener(proto::socket sock)
125 : m_socket{std::move(sock)}
134 m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
142 void write(
const boost::asio::const_buffer& buf) { boost::asio::write(m_socket, buf); }
148 proto::socket m_socket;
151class unix_stream_server
154 using proto = boost::asio::local::stream_protocol;
155 using listener = unix_stream_listener;
156 [[no_unique_address]]
struct ensure_reuse
158 explicit ensure_reuse(
const proto::endpoint& endpoint)
160 ::unlink(endpoint.path().data());
164 unix_stream_server(
const fd_configuration& conf, boost::asio::io_context& ctx)
165 : m_ensure_reuse{conf.fd}
167 , m_acceptor{boost::asio::make_strand(ctx), conf.fd}
171 unix_stream_server(
const fd_configuration& conf, ossia::net::network_context_ptr ctx)
172 : unix_stream_server{conf, ctx->context}
176 boost::asio::io_context& m_context;
177 proto::acceptor m_acceptor;
180class unix_stream_client
183 using proto = boost::asio::local::stream_protocol;
184 using socket =
typename proto::socket;
186 unix_stream_client(
const fd_configuration& conf, boost::asio::io_context& ctx)
188 , m_endpoint{conf.fd}
189 , m_socket{boost::asio::make_strand(ctx)}
195 m_socket.connect(m_endpoint);
199 bool connected()
const {
return m_connected; }
203 boost::asio::post(m_context, [
this] {
206 m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
216 void write(
const char* data, std::size_t sz)
218 boost::asio::write(m_socket, boost::asio::buffer(data, sz));
221 Nano::Signal<void()> on_open;
222 Nano::Signal<void()> on_close;
223 Nano::Signal<void()> on_fail;
225 boost::asio::io_context& m_context;
226 proto::endpoint m_endpoint;
227 proto::socket m_socket;
228 bool m_connected{
false};