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 1031 : explicit reactor_acceptor_service(capy::execution_context& ctx)
60 1031 : : ctx_(ctx)
61 1031 : , state_(
62 : std::make_unique<state_type>(
63 1031 : ctx.template use_service<Scheduler>()))
64 : {
65 1031 : }
66 :
67 : public:
68 1031 : ~reactor_acceptor_service() override = default;
69 :
70 1031 : void shutdown() override
71 : {
72 1869 : 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 1031 : }
78 :
79 2497 : io_object::implementation* construct() override
80 : {
81 2497 : return state_->pool_.acquire(static_cast<Derived&>(*this));
82 : }
83 :
84 2497 : void destroy(io_object::implementation* impl) override
85 : {
86 2497 : auto* typed = static_cast<Impl*>(impl);
87 2497 : typed->close_socket();
88 2497 : release(typed);
89 2497 : }
90 :
91 4010 : void close(io_object::handle& h) override
92 : {
93 4010 : static_cast<Impl*>(h.get())->close_socket();
94 4010 : }
95 :
96 12014 : Scheduler& scheduler() const noexcept
97 : {
98 12014 : return state_->sched_;
99 : }
100 :
101 200 : void post(scheduler_op* op)
102 : {
103 200 : state_->sched_.post(op);
104 200 : }
105 :
106 6745 : void work_started() noexcept
107 : {
108 6745 : state_->sched_.work_started();
109 6745 : }
110 :
111 6513 : StreamService* stream_service() const noexcept
112 : {
113 6513 : return stream_svc_;
114 : }
115 :
116 : protected:
117 : capy::execution_context& ctx_;
118 : std::unique_ptr<state_type> state_;
119 : StreamService* stream_svc_ = nullptr;
120 :
121 : private:
122 : reactor_acceptor_service(reactor_acceptor_service const&) = delete;
123 : reactor_acceptor_service&
124 : operator=(reactor_acceptor_service const&) = delete;
125 : };
126 :
127 : } // namespace boost::corosio::detail
128 :
129 : #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_ACCEPTOR_SERVICE_HPP
|