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 : public:
51 : /// Propagated from Scheduler for register_op's write notification.
52 : static constexpr bool needs_write_notification =
53 : Scheduler::needs_write_notification;
54 :
55 : private:
56 HIT 1174 : explicit reactor_acceptor_service(capy::execution_context& ctx)
57 1174 : : ctx_(ctx)
58 1174 : , state_(
59 : std::make_unique<state_type>(
60 1174 : ctx.template use_service<Scheduler>()))
61 : {
62 1174 : }
63 :
64 : public:
65 1174 : ~reactor_acceptor_service() override = default;
66 :
67 1174 : void shutdown() override
68 : {
69 1174 : std::lock_guard lock(state_->mutex_);
70 :
71 1174 : while (auto* impl = state_->impl_list_.pop_front())
72 MIS 0 : impl->close_socket();
73 HIT 1174 : }
74 :
75 163 : io_object::implementation* construct() override
76 : {
77 163 : auto impl = std::make_shared<Impl>(static_cast<Derived&>(*this));
78 163 : auto* raw = impl.get();
79 :
80 163 : std::lock_guard lock(state_->mutex_);
81 163 : state_->impl_ptrs_.emplace(raw, std::move(impl));
82 163 : state_->impl_list_.push_back(raw);
83 :
84 163 : return raw;
85 163 : }
86 :
87 163 : void destroy(io_object::implementation* impl) override
88 : {
89 163 : auto* typed = static_cast<Impl*>(impl);
90 163 : typed->close_socket();
91 163 : std::lock_guard lock(state_->mutex_);
92 163 : state_->impl_list_.remove(typed);
93 163 : state_->impl_ptrs_.erase(typed);
94 163 : }
95 :
96 321 : void close(io_object::handle& h) override
97 : {
98 321 : static_cast<Impl*>(h.get())->close_socket();
99 321 : }
100 :
101 286 : Scheduler& scheduler() const noexcept
102 : {
103 286 : return state_->sched_;
104 : }
105 :
106 18 : void post(scheduler_op* op)
107 : {
108 18 : state_->sched_.post(op);
109 18 : }
110 :
111 8524 : void work_started() noexcept
112 : {
113 8524 : state_->sched_.work_started();
114 8524 : }
115 :
116 12 : void work_finished() noexcept
117 : {
118 12 : state_->sched_.work_finished();
119 12 : }
120 :
121 8518 : StreamService* stream_service() const noexcept
122 : {
123 8518 : return stream_svc_;
124 : }
125 :
126 : protected:
127 : capy::execution_context& ctx_;
128 : std::unique_ptr<state_type> state_;
129 : StreamService* stream_svc_ = nullptr;
130 :
131 : private:
132 : reactor_acceptor_service(reactor_acceptor_service const&) = delete;
133 : reactor_acceptor_service& operator=(reactor_acceptor_service const&) = delete;
134 : };
135 :
136 : } // namespace boost::corosio::detail
137 :
138 : #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_ACCEPTOR_SERVICE_HPP
|