srs-control 0.2.2
Loading...
Searching...
No Matches
FecSwitchSocket.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <asio/awaitable.hpp>
8#include <asio/deferred.hpp>
9#include <asio/impl/co_spawn.hpp>
10#include <asio/strand.hpp>
11#include <asio/system_timer.hpp>
12#include <chrono>
13#include <cstddef>
14#include <fmt/ranges.h>
15#include <map>
16#include <memory>
17#include <mutex>
18#include <span>
19#include <spdlog/spdlog.h>
20#include <string>
21#include <utility>
22#include <vector>
23
24namespace srs::connection
25{
26 class CommandBase;
27
29 {
30 public:
33 using US = std::chrono::microseconds;
34 using USCount = decltype(US{}.count());
35 using StartEndTime = std::pair<USCount, USCount>;
36
37 // getters:
38 auto get_strand() -> auto& { return strand_; }
39 auto get_time_us() const -> USCount
40 {
41 return std::chrono::duration_cast<US>(std::chrono::steady_clock::now() - start_time_).count();
42 }
44
45 void log_time_stamps(std::string remote_port, AppReport::FecSwitchStat stat)
46 {
47 time_stamps_.emplace_back(std::move(remote_port), stat);
48 }
49
51
52 private:
54 using SmallConnections = std::vector<std::shared_ptr<SmallConnection>>;
55 asio::strand<io_context_type::executor_type> strand_;
56 std::mutex mut_;
57 std::map<UDPEndpoint, SmallConnections> all_connections_;
58 std::vector<char> read_msg_buffer_;
59 std::chrono::time_point<std::chrono::steady_clock> start_time_ = std::chrono::steady_clock::now();
60 std::vector<std::pair<std::string, AppReport::FecSwitchStat>> time_stamps_;
61
62 using ActionType = decltype(asio::co_spawn(strand_, asio::awaitable<void>(), asio::deferred));
63 std::vector<ActionType> action_queue_;
64
65 // constructor
66 explicit FecCommandSocket(int port_number, io_context_type& io_context);
67
68 void register_send_action_imp(asio::awaitable<void> action, const std::shared_ptr<SmallConnection>& connection);
69 auto get_response_msg_buffer() -> std::span<char> { return std::span{ read_msg_buffer_ }; }
70 auto is_finished() -> bool;
71 void print_error() const;
72 void response_handler(const UDPEndpoint& endpoint, std::size_t read_size);
73
74 void deregister_connection(const UDPEndpoint& endpoint,
75 std::span<char> response,
76 SmallConnections& connections);
77 void print_available_responses() const;
78
79 void register_report();
80 };
81
82} // namespace srs::connection
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_
auto get_time_us() const -> USCount
std::vector< ActionType > action_queue_
auto get_response_msg_buffer() -> std::span< char >
void response_handler(const UDPEndpoint &endpoint, std::size_t read_size)
void log_time_stamps(std::string remote_port, AppReport::FecSwitchStat stat)
void register_send_action_imp(asio::awaitable< void > action, const std::shared_ptr< SmallConnection > &connection)
asio::strand< io_context_type::executor_type > strand_
std::chrono::time_point< std::chrono::steady_clock > start_time_
decltype(asio::co_spawn(strand_, asio::awaitable< void >(), asio::deferred)) ActionType
std::vector< std::pair< std::string, AppReport::FecSwitchStat > > time_stamps_
std::pair< USCount, USCount > StartEndTime
std::vector< std::shared_ptr< SmallConnection > > SmallConnections
asio::ip::basic_endpoint< asio::ip::udp > UDPEndpoint
asio::thread_pool io_context_type