TLA Line data Source code
1 : //
2 : // Copyright (c) 2026 Michael Vandeberg
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/cppalliance/corosio
8 : //
9 :
10 : #ifndef BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_ACCEPTOR_SERVICE_HPP
11 : #define BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_ACCEPTOR_SERVICE_HPP
12 :
13 : #include <boost/corosio/io/io_object.hpp>
14 : #include <boost/corosio/detail/scheduler_op.hpp>
15 : #include <boost/corosio/native/detail/reactor/reactor_service_state.hpp>
16 : #include <boost/capy/ex/execution_context.hpp>
17 :
18 : #include <memory>
19 : #include <mutex>
20 :
21 : namespace boost::corosio::detail {
22 :
23 : /* CRTP base for reactor-backed acceptor service implementations.
24 :
25 : Provides the shared construct/destroy/shutdown/close/post/work
26 : logic that is identical across all reactor backends and acceptor
27 : types (TCP and local stream). Derived classes add only
28 : protocol-specific open/bind/listen/stream_service.
29 :
30 : @tparam Derived The concrete service type (CRTP).
31 : @tparam ServiceBase The abstract service base
32 : (tcp_acceptor_service or
33 : local_stream_acceptor_service).
34 : @tparam Scheduler The backend's scheduler type.
35 : @tparam Impl The backend's acceptor impl type.
36 : @tparam StreamService The concrete stream service type returned
37 : by stream_service().
38 : */
39 : template<
40 : class Derived,
41 : class ServiceBase,
42 : class Scheduler,
43 : class Impl,
44 : class StreamService>
45 : class reactor_acceptor_service : public ServiceBase
46 : {
47 : friend Derived;
48 : using state_type = reactor_service_state<Scheduler, Impl>;
49 :
50 : protected:
51 : // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
52 HIT 1170 : explicit reactor_acceptor_service(capy::execution_context& ctx)
53 1170 : : ctx_(ctx)
54 1170 : , state_(
55 : std::make_unique<state_type>(
56 1170 : ctx.template use_service<Scheduler>()))
57 : {
58 1170 : }
59 :
60 : public:
61 1170 : ~reactor_acceptor_service() override = default;
62 :
63 1170 : void shutdown() override
64 : {
65 1170 : std::lock_guard lock(state_->mutex_);
66 :
67 1170 : while (auto* impl = state_->impl_list_.pop_front())
68 MIS 0 : impl->close_socket();
69 HIT 1170 : }
70 :
71 161 : io_object::implementation* construct() override
72 : {
73 161 : auto impl = std::make_shared<Impl>(static_cast<Derived&>(*this));
74 161 : auto* raw = impl.get();
75 :
76 161 : std::lock_guard lock(state_->mutex_);
77 161 : state_->impl_ptrs_.emplace(raw, std::move(impl));
78 161 : state_->impl_list_.push_back(raw);
79 :
80 161 : return raw;
81 161 : }
82 :
83 161 : void destroy(io_object::implementation* impl) override
84 : {
85 161 : auto* typed = static_cast<Impl*>(impl);
86 161 : typed->close_socket();
87 161 : std::lock_guard lock(state_->mutex_);
88 161 : state_->impl_list_.remove(typed);
89 161 : state_->impl_ptrs_.erase(typed);
90 161 : }
91 :
92 317 : void close(io_object::handle& h) override
93 : {
94 317 : static_cast<Impl*>(h.get())->close_socket();
95 317 : }
96 :
97 282 : Scheduler& scheduler() const noexcept
98 : {
99 282 : return state_->sched_;
100 : }
101 :
102 18 : void post(scheduler_op* op)
103 : {
104 18 : state_->sched_.post(op);
105 18 : }
106 :
107 8224 : void work_started() noexcept
108 : {
109 8224 : state_->sched_.work_started();
110 8224 : }
111 :
112 12 : void work_finished() noexcept
113 : {
114 12 : state_->sched_.work_finished();
115 12 : }
116 :
117 8218 : StreamService* stream_service() const noexcept
118 : {
119 8218 : return stream_svc_;
120 : }
121 :
122 : protected:
123 : capy::execution_context& ctx_;
124 : std::unique_ptr<state_type> state_;
125 : StreamService* stream_svc_ = nullptr;
126 :
127 : private:
128 : reactor_acceptor_service(reactor_acceptor_service const&) = delete;
129 : reactor_acceptor_service& operator=(reactor_acceptor_service const&) = delete;
130 : };
131 :
132 : } // namespace boost::corosio::detail
133 :
134 : #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_ACCEPTOR_SERVICE_HPP
|