From b1817807d084a10f0bfb2ac52b662ee64abab374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Sat, 24 Feb 2024 23:37:04 -0500 Subject: [PATCH] [artnet] Various improvements, safety improvements to sockets, timer is now steady_clock --- 3rdparty/libartnet | 2 +- 3rdparty/libremidi | 2 +- src/ossia/detail/timer.hpp | 13 ++++++--- src/ossia/network/sockets/tcp_socket.hpp | 6 +++-- src/ossia/network/sockets/udp_socket.hpp | 9 ++++--- src/ossia/network/sockets/unix_socket.hpp | 7 ++--- .../protocols/artnet/artnet_protocol.cpp | 27 ++++++++++++------- .../protocols/artnet/artnet_protocol.hpp | 1 + 8 files changed, 42 insertions(+), 25 deletions(-) diff --git a/3rdparty/libartnet b/3rdparty/libartnet index 39e4848ec75..a10f5857ef5 160000 --- a/3rdparty/libartnet +++ b/3rdparty/libartnet @@ -1 +1 @@ -Subproject commit 39e4848ec75fff25d640d5cc1df82283081692b7 +Subproject commit a10f5857ef5e532002df35fa3c6994fc76ffb388 diff --git a/3rdparty/libremidi b/3rdparty/libremidi index 73e6ea40de1..63c69725f96 160000 --- a/3rdparty/libremidi +++ b/3rdparty/libremidi @@ -1 +1 @@ -Subproject commit 73e6ea40de1a1ee35f16022e70fecfb45ae4061d +Subproject commit 63c69725f96712c361d385a5d373094433a041ea diff --git a/src/ossia/detail/timer.hpp b/src/ossia/detail/timer.hpp index 0e6f367c225..32628cafed3 100644 --- a/src/ossia/detail/timer.hpp +++ b/src/ossia/detail/timer.hpp @@ -1,7 +1,9 @@ #pragma once #include -#include +#include +#include +#include namespace ossia { @@ -11,7 +13,7 @@ class timer public: explicit timer(boost::asio::io_context& ctx) : m_ctx{&ctx} - , m_timer{ctx} + , m_timer{boost::asio::make_strand(ctx)} { } @@ -42,13 +44,16 @@ class timer void stop() { - m_ctx->post([tm = std::make_shared( + std::future wait + = boost::asio::post(m_timer.get_executor(), boost::asio::use_future); + m_ctx->post([tm = std::make_shared( std::move(m_timer))]() mutable { tm->cancel(); }); + wait.get(); } private: boost::asio::io_context* m_ctx{}; - boost::asio::high_resolution_timer m_timer; + boost::asio::steady_timer m_timer; std::chrono::milliseconds m_delay{}; }; diff --git a/src/ossia/network/sockets/tcp_socket.hpp b/src/ossia/network/sockets/tcp_socket.hpp index bb81e97b5e5..4a09b5f0569 100644 --- a/src/ossia/network/sockets/tcp_socket.hpp +++ b/src/ossia/network/sockets/tcp_socket.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -49,7 +50,8 @@ class tcp_server tcp_server(const socket_configuration& conf, boost::asio::io_context& ctx) : m_context{ctx} , m_acceptor{ - ctx, proto::endpoint{boost::asio::ip::make_address(conf.host), conf.port}} + boost::asio::make_strand(ctx), + proto::endpoint{boost::asio::ip::make_address(conf.host), conf.port}} { } @@ -66,7 +68,7 @@ class tcp_client tcp_client(const socket_configuration& conf, boost::asio::io_context& ctx) : m_context{ctx} , m_endpoint{boost::asio::ip::make_address(conf.host), conf.port} - , m_socket{ctx} + , m_socket{boost::asio::make_strand(ctx)} { } diff --git a/src/ossia/network/sockets/udp_socket.hpp b/src/ossia/network/sockets/udp_socket.hpp index 6cce9bae444..4d6f4f88723 100644 --- a/src/ossia/network/sockets/udp_socket.hpp +++ b/src/ossia/network/sockets/udp_socket.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -20,14 +21,14 @@ class udp_receive_socket public: udp_receive_socket(boost::asio::io_context& ctx) : m_context{ctx} - , m_socket{ctx} + , m_socket{boost::asio::make_strand(ctx)} { } udp_receive_socket(const socket_configuration& conf, boost::asio::io_context& ctx) : m_context{ctx} , m_endpoint{boost::asio::ip::make_address(conf.host), conf.port} - , m_socket{ctx} + , m_socket{boost::asio::make_strand(ctx)} { } @@ -96,7 +97,7 @@ class udp_send_socket udp_send_socket(const socket_configuration& conf, boost::asio::io_context& ctx) : m_context{ctx} , m_endpoint{conf.broadcast ? boost::asio::ip::address_v4::broadcast() : boost::asio::ip::make_address(conf.host), conf.port} - , m_socket{ctx} + , m_socket{boost::asio::make_strand(ctx)} { } @@ -105,7 +106,7 @@ class udp_send_socket boost::asio::io_context& ctx) : m_context{ctx} , m_endpoint{host, port} - , m_socket{ctx} + , m_socket{boost::asio::make_strand(ctx)} { } diff --git a/src/ossia/network/sockets/unix_socket.hpp b/src/ossia/network/sockets/unix_socket.hpp index 6c70f79b797..0ca46028b1b 100644 --- a/src/ossia/network/sockets/unix_socket.hpp +++ b/src/ossia/network/sockets/unix_socket.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -22,7 +23,7 @@ class unix_datagram_socket unix_datagram_socket(const fd_configuration& conf, boost::asio::io_context& ctx) : m_context{ctx} , m_endpoint{conf.fd} - , m_socket{ctx} + , m_socket{boost::asio::make_strand(ctx)} { } @@ -126,7 +127,7 @@ class unix_stream_server unix_stream_server(const fd_configuration& conf, boost::asio::io_context& ctx) : m_ensure_reuse{conf.fd} , m_context{ctx} - , m_acceptor{ctx, conf.fd} + , m_acceptor{boost::asio::make_strand(ctx), conf.fd} { } @@ -143,7 +144,7 @@ class unix_stream_client unix_stream_client(const fd_configuration& conf, boost::asio::io_context& ctx) : m_context{ctx} , m_endpoint{conf.fd} - , m_socket{ctx} + , m_socket{boost::asio::make_strand(ctx)} { } diff --git a/src/ossia/protocols/artnet/artnet_protocol.cpp b/src/ossia/protocols/artnet/artnet_protocol.cpp index 6134fbf1865..ea8508e9420 100644 --- a/src/ossia/protocols/artnet/artnet_protocol.cpp +++ b/src/ossia/protocols/artnet/artnet_protocol.cpp @@ -128,14 +128,7 @@ artnet_input_protocol::artnet_input_protocol( throw std::runtime_error("Artnet Start failed"); } -artnet_input_protocol::~artnet_input_protocol() -{ - m_socket->close(); - std::future wait - = boost::asio::post(m_context->context, boost::asio::use_future); - wait.get(); - artnet_destroy(m_node); -} +artnet_input_protocol::~artnet_input_protocol() { } void artnet_input_protocol::set_device(ossia::net::device_base& dev) { @@ -156,7 +149,7 @@ void artnet_input_protocol::do_read() boost::asio::ip::udp::socket::wait_read, [this](boost::system::error_code ec) { if(ec == boost::asio::error::operation_aborted) return; - artnet_read(m_node, 1); + artnet_read_one(m_node); do_read(); }); } @@ -165,7 +158,21 @@ void artnet_input_protocol::on_packet(artnet_node n, int port) { int length = 0; auto data = artnet_read_dmx(n, port, &length); - on_dmx(data + 1, std::min(length - 1, 512)); + on_dmx(data, std::min(length, 512)); +} + +void artnet_input_protocol::stop() +{ + if(m_socket) + { + m_socket->close(); + } + + if(m_node) + { + artnet_destroy(m_node); + m_node = {}; + } } } diff --git a/src/ossia/protocols/artnet/artnet_protocol.hpp b/src/ossia/protocols/artnet/artnet_protocol.hpp index 4e031fc6747..460033b00c8 100644 --- a/src/ossia/protocols/artnet/artnet_protocol.hpp +++ b/src/ossia/protocols/artnet/artnet_protocol.hpp @@ -36,6 +36,7 @@ class OSSIA_EXPORT artnet_input_protocol final : public dmx_input_protocol_base private: void on_packet(artnet_node n, int port); void do_read(); + void stop() override; artnet_node m_node; std::unique_ptr m_socket;