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 onOpen.call({qjsEngine(this)->newQObject(this)});
38
39 socket.receive([this](const char* data, std::size_t sz) {
40 ossia::qt::run_async(this, [this, arg = QByteArray(data, sz)] {
41 if(onMessage.isCallable())
42 {
43 onMessage.call({qjsEngine(this)->toScriptValue(arg)});
44 }
45 }, Qt::AutoConnection);
46 });
47 }
48
49 void on_close()
50 {
51 run_on_qt_thread({ onClose.call(); });
52 }
53
54 void close() { socket.close(); }
55 W_SLOT(close)
56
57 QJSValue onOpen;
58 QJSValue onClose;
59 QJSValue onError;
60 QJSValue onMessage;
61
62 ossia::net::udp_receive_socket socket;
63};
64
65}
Definition qml_device.cpp:43