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>
17class qml_udp_inbound_socket
19 ,
public Nano::Observer
21 W_OBJECT(qml_udp_inbound_socket)
25 ossia::net::udp_receive_socket socket;
26 std::atomic_bool alive{
true};
29 const ossia::net::inbound_socket_configuration& conf,
30 boost::asio::io_context& ctx)
36 qml_udp_inbound_socket() { }
38 ~qml_udp_inbound_socket()
42 m_state->alive =
false;
47 bool isOpen() const noexcept {
return m_state !=
nullptr; }
50 const ossia::net::inbound_socket_configuration& conf,
51 boost::asio::io_context& ctx)
53 m_state = std::make_shared<state>(conf, ctx);
55 if(onClose.isCallable())
56 m_state->socket.on_close.connect<&qml_udp_inbound_socket::on_close>(*this);
58 m_state->socket.open();
59 if(onOpen.isCallable())
60 onOpen.call({qjsEngine(
this)->newQObject(
this)});
63 auto self = QPointer{
this};
64 st->socket.receive([st, self](
const char* data, std::size_t sz) {
69 [self, arg = QByteArray(data, sz)] {
72 if(self->onMessage.isCallable())
74 self->onMessage.call({qjsEngine(self.get())->toScriptValue(arg)});
83 if(!m_state || !m_state->alive)
85 ossia::qt::run_async(
this, [=,
this] { onClose.call(); }, Qt::AutoConnection);
92 if(!m_state->socket.m_socket.is_open())
95 boost::asio::post(st->socket.m_context, [st] {
98 st->socket.m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_both);
103 st->socket.m_socket.close();
104 st->socket.on_close();
115 std::shared_ptr<state> m_state;
Definition qml_device.cpp:43