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};
29 ossia::net::encoding enc{ossia::net::encoding::none};
32 const ossia::net::outbound_socket_configuration& conf,
33 boost::asio::io_context& ctx,
34 ossia::net::encoding e = ossia::net::encoding::none)
41 ossia::net::udp_send_socket* socket =
nullptr;
43 qml_udp_outbound_socket() { }
45 ~qml_udp_outbound_socket()
49 m_state->alive =
false;
54 bool isOpen() const noexcept {
return m_state !=
nullptr; }
57 const ossia::net::outbound_socket_configuration& conf,
58 boost::asio::io_context& ctx,
59 ossia::net::encoding e = ossia::net::encoding::none)
61 m_state = std::make_shared<state>(conf, ctx, e);
62 socket = &m_state->socket;
64 if(onClose.isCallable())
65 m_state->socket.on_close.connect<&qml_udp_outbound_socket::on_close>(*this);
67 m_state->socket.connect();
69 if(onOpen.isCallable())
70 onOpen.call({qjsEngine(
this)->newQObject(
this)});
77 if(!m_state->socket.m_socket.is_open())
80 boost::asio::post(st->socket.m_context, [st] {
83 st->socket.m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
88 st->socket.m_socket.close();
89 st->socket.on_close();
96 if(!m_state || !m_state->alive)
98 ossia::qt::run_async(
this, [=,
this] { onClose.call(); }, Qt::AutoConnection);
101 void write(QByteArray buffer)
106 if(st->enc != ossia::net::encoding::none)
107 buffer = apply_encoding(st->enc, buffer);
108 boost::asio::dispatch(st->socket.m_context, [st, buffer = std::move(buffer)] {
110 st->socket.write(buffer.data(), buffer.size());
115 void osc(QByteArray address, QJSValueList values)
118 this->send_osc(address, values);
127 std::shared_ptr<state> m_state;
Definition qml_device.cpp:43