OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
unix_socket.hpp
1#pragma once
3#include <ossia/network/context.hpp>
4#include <ossia/network/sockets/configuration.hpp>
5
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>
13
14#include <nano_signal_slot.hpp>
15
16namespace ossia::net
17{
18#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
19class unix_datagram_socket
20{
21 using proto = boost::asio::local::datagram_protocol;
22
23public:
24 unix_datagram_socket(const fd_configuration& conf, boost::asio::io_context& ctx)
25 : m_context{ctx}
26 , m_endpoint{conf.fd}
27 , m_socket{boost::asio::make_strand(ctx)}
28 {
29 }
30
31 void open()
32 {
33 ::unlink(m_endpoint.path().data());
34 m_socket.open();
35 m_socket.bind(m_endpoint);
36 }
37
38 void connect()
39 {
40 m_socket.open();
41 // m_socket.connect(m_endpoint);
42 }
43
44 void close()
45 {
46 if(m_socket.is_open())
47 {
48 boost::asio::post(m_context, [this] {
49 try
50 {
51 m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
52 }
53 catch(...)
54 {
55 }
56 m_socket.close();
57 on_close();
58 });
59 }
60 }
61
62 template <typename F>
63 void receive(F f)
64 {
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)
69 return;
70
71 if(!ec && sz > 0)
72 try
73 {
74 f(m_data, sz);
75 }
76 catch(...)
77 {
78 }
79
80 this->receive(f);
81 });
82 }
83
84 void write(const char* data, std::size_t sz)
85 {
86 m_socket.send_to(boost::asio::buffer(data, sz), m_endpoint);
87 }
88
89 Nano::Signal<void()> on_close;
90
91 boost::asio::io_context& m_context;
92 proto::endpoint m_endpoint;
93 proto::socket m_socket;
94 alignas(16) char m_data[65535];
95};
96
97class unix_stream_listener
98{
99public:
100 using proto = boost::asio::local::stream_protocol;
101 unix_stream_listener() = delete;
102 unix_stream_listener(const unix_stream_listener&) = delete;
103 unix_stream_listener& operator=(const unix_stream_listener&) = delete;
104 unix_stream_listener(unix_stream_listener&&) = default;
105 unix_stream_listener& operator=(unix_stream_listener&&) = default;
106 explicit unix_stream_listener(proto::socket sock)
107 : m_socket{std::move(sock)}
108 {
109 }
110
111 void close()
112 {
113 // FIXME async?
114 try
115 {
116 m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
117 }
118 catch(...)
119 {
120 }
121 m_socket.close();
122 }
123
124 void write(const boost::asio::const_buffer& buf) { boost::asio::write(m_socket, buf); }
125
126 void on_close() { }
127
128 void on_fail() { }
129
130 proto::socket m_socket;
131};
132
133class unix_stream_server
134{
135public:
136 using proto = boost::asio::local::stream_protocol;
137 using listener = unix_stream_listener;
138 [[no_unique_address]] struct ensure_reuse
139 {
140 explicit ensure_reuse(const proto::endpoint& endpoint)
141 {
142 ::unlink(endpoint.path().data());
143 }
144 } m_ensure_reuse;
145
146 unix_stream_server(const fd_configuration& conf, boost::asio::io_context& ctx)
147 : m_ensure_reuse{conf.fd}
148 , m_context{ctx}
149 , m_acceptor{boost::asio::make_strand(ctx), conf.fd}
150 {
151 }
152
153 unix_stream_server(const fd_configuration& conf, ossia::net::network_context_ptr ctx)
154 : unix_stream_server{conf, ctx->context}
155 {
156 }
157
158 boost::asio::io_context& m_context;
159 proto::acceptor m_acceptor;
160};
161
162class unix_stream_client
163{
164public:
165 using proto = boost::asio::local::stream_protocol;
166 using socket = typename proto::socket;
167
168 unix_stream_client(const fd_configuration& conf, boost::asio::io_context& ctx)
169 : m_context{ctx}
170 , m_endpoint{conf.fd}
171 , m_socket{boost::asio::make_strand(ctx)}
172 {
173 }
174
175 void connect()
176 {
177 m_socket.connect(m_endpoint);
178 on_open();
179 }
180
181 bool connected() const { return m_connected; }
182
183 void close()
184 {
185 boost::asio::post(m_context, [this] {
186 try
187 {
188 m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
189 }
190 catch(...)
191 {
192 }
193 m_socket.close();
194 on_close();
195 });
196 }
197
198 void write(const char* data, std::size_t sz)
199 {
200 boost::asio::write(m_socket, boost::asio::buffer(data, sz));
201 }
202
203 Nano::Signal<void()> on_open;
204 Nano::Signal<void()> on_close;
205 Nano::Signal<void()> on_fail;
206
207 boost::asio::io_context& m_context;
208 proto::endpoint m_endpoint;
209 proto::socket m_socket;
210 bool m_connected{false};
211};
212#endif
213}