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/object_ref.hpp>
15 : #include <boost/corosio/detail/scheduler_op.hpp>
16 : #include <boost/corosio/native/detail/reactor/reactor_service_state.hpp>
17 : #include <boost/capy/ex/execution_context.hpp>
18 :
19 : #include <memory>
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 :
49 : // The intermediate CRTP template below (not Impl itself) is what
50 : // actually reaches into state_->pool_ from retire() -- see
51 : // reactor_socket_finals.hpp.
52 : template<class, class, class, class, class, class>
53 : friend class reactor_acceptor_impl;
54 :
55 : using state_type = reactor_service_state<Scheduler, Impl>;
56 :
57 : protected:
58 : // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
59 HIT 735 : explicit reactor_acceptor_service(capy::execution_context& ctx)
60 735 : : ctx_(ctx)
61 735 : , state_(
62 : std::make_unique<state_type>(
63 735 : ctx.template use_service<Scheduler>()))
64 : {
65 735 : }
66 :
67 : public:
68 735 : ~reactor_acceptor_service() override = default;
69 :
70 735 : void shutdown() override
71 : {
72 758 : state_->pool_.shutdown([](Impl* impl) { impl->close_socket(); });
73 :
74 : // See reactor_socket_service::shutdown(): queued ops hold their
75 : // own reference; the scheduler's drain releases them after us,
76 : // and shutting-down mode deletes rather than recycles at zero.
77 735 : }
78 :
79 943 : io_object::implementation* construct() override
80 : {
81 943 : return state_->pool_.acquire(static_cast<Derived&>(*this));
82 : }
83 :
84 938 : void destroy(io_object::implementation* impl) override
85 : {
86 938 : auto* typed = static_cast<Impl*>(impl);
87 938 : typed->close_socket();
88 938 : release(typed);
89 938 : }
90 :
91 1671 : void close(io_object::handle& h) override
92 : {
93 1671 : static_cast<Impl*>(h.get())->close_socket();
94 1671 : }
95 :
96 1406 : Scheduler& scheduler() const noexcept
97 : {
98 1406 : return state_->sched_;
99 : }
100 :
101 181 : void post(scheduler_op* op)
102 : {
103 181 : state_->sched_.post(op);
104 181 : }
105 :
106 4964 : void work_started() noexcept
107 : {
108 4964 : state_->sched_.work_started();
109 4964 : }
110 :
111 127 : void work_finished() noexcept
112 : {
113 127 : state_->sched_.work_finished();
114 127 : }
115 :
116 4868 : StreamService* stream_service() const noexcept
117 : {
118 4868 : return stream_svc_;
119 : }
120 :
121 : protected:
122 : capy::execution_context& ctx_;
123 : std::unique_ptr<state_type> state_;
124 : StreamService* stream_svc_ = nullptr;
125 :
126 : private:
127 : reactor_acceptor_service(reactor_acceptor_service const&) = delete;
128 : reactor_acceptor_service&
129 : 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
|