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_outbound_socket
20 ,
public Nano::Observer
21 ,
public protocols_sender
23 W_OBJECT(qml_unix_datagram_outbound_socket)
27 ossia::net::unix_datagram_socket socket;
28 std::atomic_bool alive{
true};
30 state(
const ossia::net::fd_configuration& conf, boost::asio::io_context& ctx)
37 std::shared_ptr<state> m_state;
40 ossia::net::unix_datagram_socket& socket;
42 qml_unix_datagram_outbound_socket(
43 const ossia::net::fd_configuration& conf, boost::asio::io_context& ctx)
44 : m_state{std::make_shared<state>(conf, ctx)}
45 , socket{m_state->socket}
49 ~qml_unix_datagram_outbound_socket() { m_state->alive =
false; }
51 inline boost::asio::io_context& context() noexcept {
return m_state->socket.m_context; }
55 if(onClose.isCallable())
56 m_state->socket.on_close.connect<&qml_unix_datagram_outbound_socket::on_close>(
this);
58 m_state->socket.connect();
60 if(onOpen.isCallable())
62 onOpen.call({qjsEngine(
this)->newQObject(
this)});
65 void close() { m_state->socket.close(); }
72 ossia::qt::run_async(
this, [=,
this] { onClose.call(); }, Qt::AutoConnection);
75 void write(QByteArray buffer)
78 boost::asio::dispatch(st->socket.m_context, [st, buffer] {
80 st->socket.write(buffer.data(), buffer.size());
85 void osc(QByteArray address, QJSValueList values) { this->send_osc(address, values); }
93class qml_unix_stream_outbound_socket
95 ,
public Nano::Observer
96 ,
public protocols_sender
98 W_OBJECT(qml_unix_stream_outbound_socket)
102 ossia::net::unix_stream_client socket;
103 std::atomic_bool alive{
true};
105 state(
const ossia::net::fd_configuration& conf, boost::asio::io_context& ctx)
112 std::shared_ptr<state> m_state;
115 ossia::net::unix_stream_client& socket;
117 qml_unix_stream_outbound_socket(
118 const ossia::net::fd_configuration& conf, boost::asio::io_context& ctx)
119 : m_state{std::make_shared<state>(conf, ctx)}
120 , socket{m_state->socket}
124 ~qml_unix_stream_outbound_socket() { m_state->alive =
false; }
126 inline boost::asio::io_context& context() noexcept {
return m_state->socket.m_context; }
132 if(onOpen.isCallable())
133 m_state->socket.on_open.connect<&qml_unix_stream_outbound_socket::on_open>(
this);
134 if(onClose.isCallable())
135 m_state->socket.on_close.connect<&qml_unix_stream_outbound_socket::on_close>(
this);
136 if(onError.isCallable())
137 m_state->socket.on_fail.connect<&qml_unix_stream_outbound_socket::on_fail>(
this);
138 m_state->socket.connect();
140 catch(
const std::exception& e)
142 if(onError.isCallable())
144 onError.call({QString::fromStdString(e.what())});
149 void write(QByteArray buffer)
152 boost::asio::dispatch(st->socket.m_context, [st, buffer] {
154 st->socket.write(buffer.data(), buffer.size());
159 void close() { m_state->socket.close(); }
166 ossia::qt::run_async(
167 this, [=,
this] { onOpen.call({qjsEngine(
this)->newQObject(
this)}); },
174 ossia::qt::run_async(
this, [=,
this] { onError.call(); }, Qt::AutoConnection);
180 ossia::qt::run_async(
this, [=,
this] { onClose.call(); }, Qt::AutoConnection);
183 void osc(QByteArray address, QJSValueList values) { this->send_osc(address, values); }
Definition qml_device.cpp:43