OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
qml_udp_inbound_socket.hpp
1#pragma once
2#include <ossia/network/context.hpp>
3#include <ossia/network/sockets/udp_socket.hpp>
4
5#include <ossia-qt/protocols/utils.hpp>
6
7#include <QJSValue>
8#include <QObject>
9#include <QQmlEngine>
10
11#include <nano_observer.hpp>
12
13#include <verdigris>
14
15namespace ossia::qt
16{
17class qml_udp_inbound_socket
18 : public QObject
19 , public Nano::Observer
20{
21 W_OBJECT(qml_udp_inbound_socket)
22public:
23 qml_udp_inbound_socket(
24 const ossia::net::inbound_socket_configuration& conf, boost::asio::io_context& ctx)
25 : socket{conf, ctx}
26 {
27 }
28 inline boost::asio::io_context& context() noexcept { return socket.m_context; }
29
30 void open()
31 {
32 if(onClose.isCallable())
33 socket.on_close.connect<&qml_udp_inbound_socket::on_close>(*this);
34
35 socket.open();
36 if(onOpen.isCallable())
37
38 onOpen.call({qjsEngine(this)->newQObject(this)});
39
40 socket.receive([this](const char* data, std::size_t sz) {
41 ossia::qt::run_async(this, [this, arg = QString::fromUtf8(data, sz)] {
42 if(onMessage.isCallable())
43 {
44 onMessage.call({arg});
45 }
46 }, Qt::AutoConnection);
47 });
48 }
49
50 void on_close()
51 {
52 run_on_qt_thread({ onClose.call(); });
53 }
54
55 void close() { socket.close(); }
56 W_SLOT(close)
57
58 QJSValue onOpen;
59 QJSValue onClose;
60 QJSValue onError;
61 QJSValue onMessage;
62
63 ossia::net::udp_receive_socket socket;
64};
65
66}
Definition qml_device.cpp:43