SRS-control 0.1.4
Loading...
Searching...
No Matches
SpecialSocketBase.cpp
Go to the documentation of this file.
4#include <boost/asio/as_tuple.hpp>
5#include <boost/asio/awaitable.hpp>
6#include <boost/asio/ip/basic_endpoint.hpp>
7#include <boost/asio/use_awaitable.hpp>
8#include <chrono>
9#include <future>
10#include <memory>
11#include <optional>
12#include <spdlog/spdlog.h>
13
14namespace srs::connection
15{
16 SpecialSocket::SpecialSocket(int port_number, io_context_type& io_context)
17 : port_number_{ port_number }
18 , cancel_timer_{ io_context }
19 {
20 socket_ = std::make_unique<udp::socket>(io_context);
21 socket_->open(udp::v4());
22 cancel_timer_.expires_at(std::chrono::system_clock::time_point::max());
23 }
24
25 auto SpecialSocket::cancel_coroutine() -> asio::awaitable<void>
26 {
27 // NOLINTBEGIN (clang-analyzer-core.NullDereference)
28 [[maybe_unused]] auto err_code = co_await cancel_timer_.async_wait(asio::as_tuple(asio::use_awaitable));
29 // NOLINTEND (clang-analyzer-core.NullDereference)
30 spdlog::trace("Coroutine for the local socket with port {} is cancelled.", port_number_);
31 }
32
34 {
35 socket_->bind(udp::endpoint{ udp::v4(), static_cast<asio::ip::port_type>(port_number_) }, socket_ec_);
36 }
38
39 auto SpecialSocket::wait_for_listen_finish(std::chrono::seconds time) -> std::optional<std::future_status>
40 {
41 if (not listen_future_.valid())
42 {
43 return {};
44 }
45 return listen_future_.wait_for(time);
46 }
47} // namespace srs::connection
asio::system_timer cancel_timer_
Used for cancel the unfinished coroutine.
auto cancel_coroutine() -> asio::awaitable< void >
auto wait_for_listen_finish(std::chrono::seconds time) -> std::optional< std::future_status >
std::unique_ptr< udp::socket > socket_
boost::system::error_code socket_ec_
SpecialSocket(int port_number, io_context_type &io_context)
std::shared_future< std::variant< std::monostate, std::monostate > > listen_future_
asio::thread_pool io_context_type