Skip to content

Commit

Permalink
[artnet] Various improvements, safety improvements to sockets, timer …
Browse files Browse the repository at this point in the history
…is now steady_clock
  • Loading branch information
jcelerier committed Feb 25, 2024
1 parent 6616653 commit b181780
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/libartnet
2 changes: 1 addition & 1 deletion 3rdparty/libremidi
13 changes: 9 additions & 4 deletions src/ossia/detail/timer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once
#include <ossia/detail/logger.hpp>

#include <boost/asio/high_resolution_timer.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/use_future.hpp>

namespace ossia
{
Expand All @@ -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)}
{
}

Expand Down Expand Up @@ -42,13 +44,16 @@ class timer

void stop()
{
m_ctx->post([tm = std::make_shared<boost::asio::high_resolution_timer>(
std::future<void> wait
= boost::asio::post(m_timer.get_executor(), boost::asio::use_future);
m_ctx->post([tm = std::make_shared<boost::asio::steady_timer>(
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{};
};

Expand Down
6 changes: 4 additions & 2 deletions src/ossia/network/sockets/tcp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <boost/asio/ip/udp.hpp>
#include <boost/asio/local/datagram_protocol.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/write.hpp>

#include <nano_signal_slot.hpp>
Expand Down Expand Up @@ -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}}
{
}

Expand All @@ -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)}
{
}

Expand Down
9 changes: 5 additions & 4 deletions src/ossia/network/sockets/udp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <boost/asio/ip/udp.hpp>
#include <boost/asio/local/datagram_protocol.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/write.hpp>

#include <nano_signal_slot.hpp>
Expand All @@ -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)}
{
}

Expand Down Expand Up @@ -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)}
{
}

Expand All @@ -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)}
{
}

Expand Down
7 changes: 4 additions & 3 deletions src/ossia/network/sockets/unix_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <boost/asio/local/datagram_protocol.hpp>
#include <boost/asio/local/stream_protocol.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/write.hpp>

#include <nano_signal_slot.hpp>
Expand All @@ -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)}
{
}

Expand Down Expand Up @@ -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}
{
}

Expand All @@ -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)}
{
}

Expand Down
27 changes: 17 additions & 10 deletions src/ossia/protocols/artnet/artnet_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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)
{
Expand All @@ -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();
});
}
Expand All @@ -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 = {};
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ossia/protocols/artnet/artnet_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ossia::net::udp_receive_socket> m_socket;
Expand Down

0 comments on commit b181780

Please sign in to comment.