SRS-control 0.1.4
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 <boost/asio/awaitable.hpp>
11#include <boost/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
34 void send_message_from(this auto&& self, const std::shared_ptr<FecCommandSocket>& socket)
35 {
36 self.communicate(self.get_suffix(), common::NULL_ADDRESS, socket);
37 }
38
39 [[nodiscard]] auto check_response(std::span<char> response_msg) const -> bool;
40
41 // Setters:
42 void set_remote_endpoint(asio::ip::udp::endpoint endpoint) { remote_endpoint_ = std::move(endpoint); }
43
44 // Getters:
45 [[nodiscard]] auto get_name() const -> std::string_view { return name_; }
46 auto get_remote_endpoint() -> const udp::endpoint& { return remote_endpoint_; }
47 [[nodiscard]] auto get_response_msg() const -> std::string_view { return write_msg_response_buffer_.data(); }
48 [[nodiscard]] auto get_remote_ip_string() const { return remote_endpoint_.address().to_string(); }
49 [[nodiscard]] auto get_remote_port() const { return remote_endpoint_.port(); }
50
51 protected:
52 void set_write_response_msg(const std::vector<CommunicateEntryType>& msg, uint16_t address)
53 {
55 }
56
57 private:
59 udp::endpoint remote_endpoint_;
62 std::string_view name_;
63
65 uint32_t counter,
66 std::span<const CommunicateEntryType> data,
67 uint16_t address);
68
69 static auto timer_countdown(auto* connection) -> asio::awaitable<void>;
70
71 static auto send_message(std::shared_ptr<FecCommandSocket> socket, std::shared_ptr<CommandBase> connection)
72 -> asio::awaitable<void>;
73
74 void communicate(this auto&& self,
75 std::span<const CommunicateEntryType> data,
76 uint16_t address,
77 const std::shared_ptr<FecCommandSocket>& socket)
78 {
79 self.encode_write_msg(self.write_msg_buffer_, self.counter_, data, address);
80 auto send_action = send_message(socket, self.shared_from_this());
81 socket->register_send_action(std::move(send_action), common::get_shared_from_this(self));
82 }
83 };
84} // 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)
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)