srs-control 0.2.2
Loading...
Searching...
No Matches
ConnectionBase.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "srs/utils/UDPFormatters.hpp" // IWYU pragma: keep
9#include <algorithm>
10#include <asio/awaitable.hpp>
11#include <asio/ip/udp.hpp>
12#include <cstdint>
13#include <fmt/base.h>
14#include <gsl/gsl-lite.hpp>
15#include <memory>
16#include <span>
17#include <spdlog/spdlog.h>
18#include <string_view>
19#include <utility>
20#include <vector>
21
22namespace srs::connection
23{
24 class FecCommandSocket;
25
26 class CommandBase : public std::enable_shared_from_this<CommandBase>
27 {
28 public:
29 explicit CommandBase(const std::string_view name)
30 : name_{ name }
31 {
32 }
33
40 void send_message_from(this auto&& self, const std::shared_ptr<FecCommandSocket>& socket)
41 {
42 self.communicate(self.get_suffix(), common::NULL_ADDRESS, socket);
43 }
44
45 [[nodiscard]] auto check_response(std::span<char> response_msg) const -> bool;
46
47 // Setters:
48 void set_remote_endpoint(asio::ip::udp::endpoint endpoint) { remote_endpoint_ = std::move(endpoint); }
49
50 // Getters:
51 [[nodiscard]] auto get_name() const -> std::string_view { return name_; }
52 auto get_remote_endpoint() -> const udp::endpoint& { return remote_endpoint_; }
53 [[nodiscard]] auto get_response_msg() const -> std::string_view { return write_msg_response_buffer_.data(); }
54 [[nodiscard]] auto get_remote_ip_string() const { return remote_endpoint_.address().to_string(); }
55 [[nodiscard]] auto get_remote_port() const { return remote_endpoint_.port(); }
56
57 protected:
58 void set_write_response_msg(const std::vector<CommunicateEntryType>& msg, uint16_t address)
59 {
61 }
62
63 private:
65 udp::endpoint remote_endpoint_;
68 std::string_view name_;
69
71 uint32_t counter,
72 std::span<const CommunicateEntryType> data,
73 uint16_t address);
74
75 static auto timer_countdown(auto* connection) -> asio::awaitable<void>;
76
77 static auto send_message(std::shared_ptr<FecCommandSocket> socket, std::shared_ptr<CommandBase> connection)
78 -> asio::awaitable<void>;
79
80 void communicate(this auto&& self,
81 std::span<const CommunicateEntryType> data,
82 uint16_t address,
83 const std::shared_ptr<FecCommandSocket>& socket)
84 {
85 self.encode_write_msg(self.write_msg_buffer_, self.counter_, data, address);
86 auto send_action = send_message(socket, self.shared_from_this());
87 socket->register_send_action(std::move(send_action), common::get_shared_from_this(self));
88 }
89 };
90} // namespace srs::connection
process::SerializableMsgBuffer write_msg_response_buffer_
static auto send_message(std::shared_ptr< FecCommandSocket > socket, std::shared_ptr< CommandBase > connection) -> asio::awaitable< void >
void set_write_response_msg(const std::vector< CommunicateEntryType > &msg, uint16_t address)
auto get_response_msg() const -> std::string_view
auto check_response(std::span< char > response_msg) const -> bool
static auto timer_countdown(auto *connection) -> asio::awaitable< void >
void communicate(this auto &&self, std::span< const CommunicateEntryType > data, uint16_t address, const std::shared_ptr< FecCommandSocket > &socket)
void send_message_from(this auto &&self, const std::shared_ptr< FecCommandSocket > &socket)
Register the send action in the socket.
auto get_remote_endpoint() -> const udp::endpoint &
void set_remote_endpoint(asio::ip::udp::endpoint endpoint)
process::SerializableMsgBuffer write_msg_buffer_
static void encode_write_msg(process::SerializableMsgBuffer &buffer, uint32_t counter, std::span< const CommunicateEntryType > data, uint16_t address)
auto get_name() const -> std::string_view
CommandBase(const std::string_view name)
constexpr auto INIT_COUNT_VALUE
constexpr auto NULL_ADDRESS
constexpr auto get_shared_from_this(auto &&obj)