51 using io_state_chg_cb =
58 std::atomic_int m_started;
59 io_state_chg_cb m_io_state_chg_cb;
64 net_entity_common() noexcept : m_started(0), m_io_state_chg_cb(), m_error_cb() { }
67 bool is_started()
const noexcept {
return m_started == 1; }
69 bool is_stopped()
const noexcept {
return m_started == 2; }
74 void set_stopped()
noexcept { m_started = 2; }
76 template <
typename F1,
typename F2,
typename SF>
77 std::error_code start(F1&& io_state_chg_func, F2&& err_func,
78 const asio::any_io_executor& exec,
81 if (!m_started.compare_exchange_strong(expected, 1)) {
82 return std::make_error_code(net_ip_errc::net_entity_already_started);
84 m_io_state_chg_cb = io_state_chg_func;
85 m_error_cb = err_func;
86 std::promise<std::error_code> prom;
87 auto fut = prom.get_future();
89 asio::post(exec, [&start_func, p = std::move(prom)] ()
mutable {
90 p.set_value(start_func());
97 template <
typename SF>
98 std::error_code stop(
const asio::any_io_executor& exec,
101 if (!m_started.compare_exchange_strong(expected, 2)) {
102 return std::make_error_code(net_ip_errc::net_entity_already_stopped);
104 std::promise<std::error_code> prom;
105 auto fut = prom.get_future();
107 asio::post(exec, [&stop_func, p = std::move(prom)] ()
mutable {
108 p.set_value(stop_func());
114 void call_io_state_chg_cb(std::shared_ptr<IOT> p, std::size_t sz,
bool starting) {
118 void call_error_cb(std::shared_ptr<IOT> p,
const std::error_code& err) {