Radix Relay
Hybrid mesh communications with Signal Protocol encryption
Loading...
Searching...
No Matches
connection_monitor_processor.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <boost/asio/awaitable.hpp>
5#include <boost/asio/cancellation_signal.hpp>
6#include <boost/asio/experimental/channel_error.hpp>
7#include <boost/asio/io_context.hpp>
9#include <core/events.hpp>
10#include <memory>
11#include <spdlog/spdlog.h>
12#include <variant>
13
14namespace radix_relay::core {
15
20{
21public:
29 connection_monitor_processor(const std::shared_ptr<boost::asio::io_context> &io_context,
30 const std::shared_ptr<async::async_queue<events::connection_monitor::in_t>> &in_queue,
31 const std::shared_ptr<connection_monitor> &monitor)
32 : io_context_(io_context), in_queue_(in_queue), monitor_(monitor)
33 {}
34
41 // NOLINTNEXTLINE(performance-unnecessary-value-param)
42 auto run_once(std::shared_ptr<boost::asio::cancellation_slot> cancel_slot = nullptr) -> boost::asio::awaitable<void>
43 {
44 auto evt = co_await in_queue_->pop(cancel_slot);
45 std::visit([&](auto &&event) { monitor_->handle(std::forward<decltype(event)>(event)); }, evt);
46 co_return;
47 }
48
55 // NOLINTNEXTLINE(performance-unnecessary-value-param)
56 auto run(std::shared_ptr<boost::asio::cancellation_slot> cancel_slot = nullptr) -> boost::asio::awaitable<void>
57 {
58 try {
59 while (true) { co_await run_once(cancel_slot); }
60 } catch (const boost::system::system_error &e) {
61 if (e.code() == boost::asio::error::operation_aborted
62 or e.code() == boost::asio::experimental::error::channel_cancelled
63 or e.code() == boost::asio::experimental::error::channel_closed) {
64 spdlog::debug("[connection_monitor_processor] Cancelled, exiting run loop");
65 co_return;
66 } else {
67 spdlog::error("[connection_monitor_processor] Unexpected error in run loop: {}", e.what());
68 throw;
69 }
70 }
71 }
72
73private:
74 std::shared_ptr<boost::asio::io_context> io_context_;
75 std::shared_ptr<async::async_queue<events::connection_monitor::in_t>> in_queue_;
76 std::shared_ptr<connection_monitor> monitor_;
77};
78
79}// namespace radix_relay::core
Thread-safe asynchronous queue for message passing between coroutines.
Processes connection monitor events (transport status and queries).
auto run(std::shared_ptr< boost::asio::cancellation_slot > cancel_slot=nullptr) -> boost::asio::awaitable< void >
Continuously processes events until cancelled.
connection_monitor_processor(const std::shared_ptr< boost::asio::io_context > &io_context, const std::shared_ptr< async::async_queue< events::connection_monitor::in_t > > &in_queue, const std::shared_ptr< connection_monitor > &monitor)
Constructs a connection monitor processor.
auto run_once(std::shared_ptr< boost::asio::cancellation_slot > cancel_slot=nullptr) -> boost::asio::awaitable< void >
Processes a single event from the queue.