2#include <ossia/network/context.hpp>
3#include <ossia/network/sockets/udp_socket.hpp>
5#include <ossia-qt/protocols/utils.hpp>
11#include <nano_observer.hpp>
18class qml_udp_outbound_socket
20 ,
public Nano::Observer
21 ,
public protocols_sender
23 W_OBJECT(qml_udp_outbound_socket)
27 ossia::net::udp_send_socket socket;
28 std::atomic_bool alive{
true};
31 const ossia::net::outbound_socket_configuration& conf,
32 boost::asio::io_context& ctx)
38 ossia::net::udp_send_socket* socket =
nullptr;
40 qml_udp_outbound_socket() { }
42 ~qml_udp_outbound_socket()
46 m_state->alive =
false;
51 bool isOpen() const noexcept {
return m_state !=
nullptr; }
54 const ossia::net::outbound_socket_configuration& conf,
55 boost::asio::io_context& ctx)
57 m_state = std::make_shared<state>(conf, ctx);
58 socket = &m_state->socket;
60 if(onClose.isCallable())
61 m_state->socket.on_close.connect<&qml_udp_outbound_socket::on_close>(*this);
63 m_state->socket.connect();
65 if(onOpen.isCallable())
66 onOpen.call({qjsEngine(
this)->newQObject(
this)});
73 if(!m_state->socket.m_socket.is_open())
76 boost::asio::post(st->socket.m_context, [st] {
79 st->socket.m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
84 st->socket.m_socket.close();
85 st->socket.on_close();
92 if(!m_state || !m_state->alive)
94 ossia::qt::run_async(
this, [=,
this] { onClose.call(); }, Qt::AutoConnection);
97 void write(QByteArray buffer)
102 boost::asio::dispatch(st->socket.m_context, [st, buffer] {
104 st->socket.write(buffer.data(), buffer.size());
109 void osc(QByteArray address, QJSValueList values)
112 this->send_osc(address, values);
121 std::shared_ptr<state> m_state;
Definition qml_device.cpp:43