9#include <asio/awaitable.hpp>
10#include <asio/deferred.hpp>
11#include <asio/detached.hpp>
12#include <asio/experimental/cancellation_condition.hpp>
13#include <asio/experimental/parallel_group.hpp>
14#include <asio/impl/co_spawn.hpp>
15#include <asio/strand.hpp>
16#include <asio/use_future.hpp>
20#include <fmt/format.h>
21#include <fmt/ranges.h>
26#include <spdlog/spdlog.h>
34 auto remove_once_if(
auto& connections,
auto unitary_opt) ->
bool
36 auto iter = std::ranges::find_if(
37 connections, [unitary_opt](
const auto& connection) ->
bool {
return not unitary_opt(connection); });
38 if (iter == connections.end())
42 std::iter_swap(iter, connections.end() - 1);
43 connections.pop_back();
47 auto print_connections(
const auto& connections) -> std::string
49 auto join_connection = [](
const auto& connection)
50 {
return fmt::format(
"{:02x}", fmt::join(connection->get_response_msg(),
" ")); };
51 return fmt::format(
"{}", fmt::join(connections | std::views::transform(join_connection),
"\n\t"));
57 ,
strand_{ asio::make_strand(io_context.get_executor()) }
64 const std::shared_ptr<SmallConnection>&
connection)
66 auto co_action = asio::co_spawn(
strand_, std::move(action), asio::deferred);
69 auto lock = std::lock_guard{
mut_ };
70 const auto& remote_endpoint =
connection->get_remote_endpoint();
83 asio::experimental::make_parallel_group(std::move(
action_queue_))
84 .async_wait(asio::experimental::wait_for_all(), asio::detached);
96 std::span<char> response,
99 if (connections.empty())
101 spdlog::debug(
"Response from the remote endpoint {} to the local endpoint {} is received. But no further "
102 "message from the endpoint is not required anymore!",
107 auto res = remove_once_if(connections,
108 [response](
const std::shared_ptr<SmallConnection>&
connection) ->
bool
110 auto is_same =
connection->check_response(response);
115 spdlog::debug(
"Response from the remote endpoint {} is recognized by the local endpoint {}",
122 spdlog::warn(
"From remote endpoint {}, socket with local endpoint {} received an unrecognized msg: {:02x}",
125 fmt::join(response,
" "));
133 "Available responses are:\n\t{}",
135 std::views::filter([](
const auto& connections) ->
bool {
return not connections.empty(); }) |
136 std::views::transform([](
const auto& connections) {
return print_connections(connections); }),
143 spdlog::error(
"TIMEOUT from local port {} waiting for the response(s):\n\t{}",
146 std::views::filter([](
const auto& key_value) ->
bool
147 {
return not key_value.second.empty(); }) |
148 std::views::transform(
149 [](
const auto& key_value)
151 return fmt::format(
"Remote endpoint:\t{}\n\tExpected Response:\t{}",
153 print_connections(key_value.second));
160 auto lock = std::lock_guard{
mut_ };
162 spdlog::trace(
"Local port {} received a response from a remote endpoint {}: \n\t{:02x}",
165 fmt::join(response,
" "));
169 spdlog::error(
"Local socket with port {} received an unknown remote endpoint: {}",
get_port(), endpoint);
179 [](
const auto& connections) ->
bool {
return connections.empty(); });
184 if (
auto* report =
get_report(); report !=
nullptr)
186 report->register_switch_socket_result(fmt::format(
"{}",
get_socket().local_endpoint()),
FecCommandSocket(int port_number, io_context_type &io_context)
void deregister_connection(const UDPEndpoint &endpoint, std::span< char > response, SmallConnections &connections)
std::map< UDPEndpoint, SmallConnections > all_connections_
std::vector< ActionType > action_queue_
void response_handler(const UDPEndpoint &endpoint, std::size_t read_size)
auto is_finished() -> bool
void register_send_action_imp(asio::awaitable< void > action, const std::shared_ptr< SmallConnection > &connection)
void launch_send_actions()
void print_available_responses() const
asio::strand< io_context_type::executor_type > strand_
std::chrono::time_point< std::chrono::steady_clock > start_time_
std::vector< std::pair< std::string, AppReport::FecSwitchStat > > time_stamps_
std::vector< char > read_msg_buffer_
std::vector< std::shared_ptr< SmallConnection > > SmallConnections
auto get_report() -> auto *
auto get_port() const -> int
Getter for the port number.
auto get_socket() const -> udp::socket &
Getter for the asio udp socket object.
constexpr auto SMALL_READ_MSG_BUFFER_SIZE
asio::ip::basic_endpoint< asio::ip::udp > UDPEndpoint
asio::thread_pool io_context_type