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