44 void UDPInterface::update_connections_(
47 auto it = connections_.find(ip_address);
49 if (it == connections_.end())
51 it = connections_.insert(
52 {ip_address, connection_factory_->get(
str(ip_address))}).first;
53 connection_pool_->add(it->second);
56 it->second->add_address(mav_address);
72 std::unique_ptr<UDPSocket> socket,
73 std::shared_ptr<ConnectionPool> connection_pool,
75 : socket_(
std::move(socket)),
76 connection_pool_(
std::move(connection_pool)),
77 connection_factory_(
std::move(connection_factory)),
80 if (socket_ ==
nullptr)
82 throw std::invalid_argument(
"Given socket pointer is null.");
85 if (connection_pool_ ==
nullptr)
87 throw std::invalid_argument(
"Given connection pool pointer is null.");
90 if (connection_factory_ ==
nullptr)
92 throw std::invalid_argument(
93 "Given connection factory pointer is null.");
105 bool not_first =
false;
108 if (connection_factory_->wait_for_packet(timeout))
110 for (
auto &conn : connections_)
112 auto packet = conn.second->next_packet();
115 if (packet !=
nullptr)
117 socket_->send(packet->data(), conn.first);
122 connection_factory_->wait_for_packet(0s);
140 auto [buffer, ip_address] = socket_->receive(timeout);
146 if (ip_address != last_ip_address_)
149 last_ip_address_ = ip_address;
153 for (
auto byte : buffer)
157 if (packet !=
nullptr)
159 update_connections_(packet->source(), ip_address);
162 packet->connection(connections_[ip_address]);
163 connection_pool_->send(std::move(packet));
std::string str(const T &object)
void receive_packet(const std::chrono::nanoseconds &timeout) final
std::ostream & print_(std::ostream &os) const final
UDPInterface(std::unique_ptr< UDPSocket > socket, std::shared_ptr< ConnectionPool > connection_pool, std::unique_ptr< ConnectionFactory<>> connection_factory)
void send_packet(const std::chrono::nanoseconds &timeout) final
std::unique_ptr< Packet > parse_byte(uint8_t byte)