2#include <ossia/network/context.hpp>
3#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
4#include <ossia/network/sockets/unix_socket.hpp>
6#include <ossia-qt/protocols/utils.hpp>
12#include <nano_observer.hpp>
18class qml_unix_datagram_inbound_socket
20 ,
public Nano::Observer
22 W_OBJECT(qml_unix_datagram_inbound_socket)
24 qml_unix_datagram_inbound_socket(
25 const ossia::net::fd_configuration& conf, boost::asio::io_context& ctx)
29 inline boost::asio::io_context& context() noexcept {
return socket.m_context; }
33 if(onClose.isCallable())
34 socket.on_close.connect<&qml_unix_datagram_inbound_socket::on_close>(
this);
37 if(onOpen.isCallable())
39 onOpen.call({qjsEngine(
this)->newQObject(
this)});
41 socket.receive([self = QPointer{
this}](
const char* data, std::size_t sz) {
42 ossia::qt::run_async(self.data(), [self, arg = QString::fromUtf8(data, sz)] {
45 if(self->onMessage.isCallable())
47 self->onMessage.call({arg});
49 }, Qt::AutoConnection);
53 void close() { socket.close(); }
58 run_on_qt_thread({ onClose.call(); });
66 ossia::net::unix_datagram_socket socket;
70class qml_unix_stream_connection
72 ,
public Nano::Observer
74 W_OBJECT(qml_unix_stream_connection)
76 qml_unix_stream_connection(
77 ossia::net::unix_stream_listener listener, boost::asio::io_context& ctx)
79 , m_listener{std::move(listener)}
82 inline boost::asio::io_context& context() noexcept {
return m_context; }
84 void write(QByteArray buffer)
87 { m_listener.write(boost::asio::buffer(buffer.data(), buffer.size())); });
93 auto& socket = m_listener.m_socket;
94 socket.async_read_some(
95 boost::asio::buffer(m_data,
sizeof(m_data)),
97 = QPointer{
this}](boost::system::error_code ec, std::size_t bytes_transferred) {
100 ossia::qt::run_async(
102 [self, arg = QString::fromUtf8(self->m_data, bytes_transferred)] {
105 if(self->onMessage.isCallable())
107 self->onMessage.call({arg});
110 self->startReceive();
118 boost::asio::io_context& m_context;
119 ossia::net::unix_stream_listener m_listener;
123class qml_unix_stream_inbound_socket
125 ,
public Nano::Observer
127 W_OBJECT(qml_unix_stream_inbound_socket)
129 qml_unix_stream_inbound_socket(
130 const ossia::net::fd_configuration& conf, boost::asio::io_context& ctx)
134 inline boost::asio::io_context& context() noexcept {
return server.m_context; }
139 if(onOpen.isCallable())
141 onOpen.call({qjsEngine(
this)->newQObject(
this)});
146 server.m_acceptor.close();
147 if(onClose.isCallable())
154 run_on_qt_thread({ onClose.call(); });
160 QJSValue onConnection;
162 ossia::net::unix_stream_server server;
167 server.m_acceptor.async_accept(
168 [self = QPointer{
this}](
169 boost::system::error_code ec,
170 ossia::net::unix_stream_server::proto::socket socket) {
173 ossia::qt::run_async(self.get(), [self, socket = std::move(socket)]()
mutable {
174 auto conn = new qml_unix_stream_connection{
175 ossia::net::unix_stream_listener{std::move(socket)},
176 self->server.m_context};
177 conn->onMessage = self->onConnection;
178 conn->startReceive();
180 if(self->onConnection.isCallable())
182 self->onConnection.call(
183 {qjsEngine(self)->newQObject(
static_cast<QObject*
>(conn))});
Definition qml_device.cpp:43