2#include <ossia/network/context.hpp>
3#include <ossia/network/sockets/tcp_socket.hpp>
5#include <ossia-qt/protocols/utils.hpp>
11#include <nano_observer.hpp>
17class qml_tcp_outbound_socket
19 ,
public Nano::Observer
20 ,
public protocols_sender
22 W_OBJECT(qml_tcp_outbound_socket)
26 ossia::net::tcp_client socket;
27 std::atomic_bool alive{
true};
30 const ossia::net::outbound_socket_configuration& conf,
31 boost::asio::io_context& ctx)
38 std::shared_ptr<state> m_state;
41 ossia::net::tcp_client& socket;
43 qml_tcp_outbound_socket(
44 const ossia::net::outbound_socket_configuration& conf,
45 boost::asio::io_context& ctx)
46 : m_state{std::make_shared<state>(conf, ctx)}
47 , socket{m_state->socket}
51 ~qml_tcp_outbound_socket() { m_state->alive =
false; }
53 inline boost::asio::io_context& context() noexcept {
return m_state->socket.m_context; }
59 if(onOpen.isCallable())
60 m_state->socket.on_open.connect<&qml_tcp_outbound_socket::on_open>(
this);
61 if(onClose.isCallable())
62 m_state->socket.on_close.connect<&qml_tcp_outbound_socket::on_close>(
this);
63 if(onError.isCallable())
64 m_state->socket.on_fail.connect<&qml_tcp_outbound_socket::on_fail>(
this);
65 m_state->socket.connect();
67 catch(
const std::exception& e)
69 if(onError.isCallable())
71 onError.call({QString::fromStdString(e.what())});
76 void write(QByteArray buffer)
79 boost::asio::dispatch(st->socket.m_context, [st, buffer] {
81 st->socket.write(buffer.data(), buffer.size());
86 void close() { m_state->socket.close(); }
94 this, [=,
this] { onOpen.call({qjsEngine(
this)->newQObject(
this)}); },
101 ossia::qt::run_async(
this, [=,
this] { onError.call(); }, Qt::AutoConnection);
107 ossia::qt::run_async(
this, [=,
this] { onClose.call(); }, Qt::AutoConnection);
110 void osc(QByteArray address, QJSValueList values) { this->send_osc(address, values); }
Definition qml_device.cpp:43