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_SOCKET_FINALS_HPP
11 : #define BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_SOCKET_FINALS_HPP
12 :
13 : /* Parameterized socket, datagram, and acceptor implementation bases.
14 :
15 : Named per-backend classes (e.g. epoll_tcp_socket) inherit from
16 : these templates, supplying the concrete service/peer types. The
17 : per-backend type files ({backend}_types.hpp) define the final classes.
18 : */
19 :
20 : #include <boost/corosio/tcp_socket.hpp>
21 : #include <boost/corosio/udp_socket.hpp>
22 : #include <boost/corosio/local_stream_socket.hpp>
23 : #include <boost/corosio/local_datagram_socket.hpp>
24 : #include <boost/corosio/tcp_acceptor.hpp>
25 : #include <boost/corosio/local_stream_acceptor.hpp>
26 : #include <boost/corosio/shutdown_type.hpp>
27 :
28 : #include <boost/corosio/native/detail/reactor/reactor_stream_socket.hpp>
29 : #include <boost/corosio/native/detail/reactor/reactor_datagram_socket.hpp>
30 : #include <boost/corosio/native/detail/reactor/reactor_acceptor.hpp>
31 : #include <boost/corosio/native/detail/reactor/reactor_stream_ops.hpp>
32 : #include <boost/corosio/native/detail/reactor/reactor_datagram_ops.hpp>
33 :
34 : #include <boost/corosio/native/detail/make_err.hpp>
35 :
36 : namespace boost::corosio::detail {
37 :
38 : // ============================================================
39 : // Stream socket implementation base
40 : // ============================================================
41 :
42 : /** Intermediate base for reactor stream sockets.
43 :
44 : Holds the per-socket hook (e.g., kqueue SO_LINGER tracking),
45 : the set_option override, and the close/release shadows.
46 : Named per-backend classes inherit from this as final.
47 :
48 : @tparam Derived The named final class (CRTP self).
49 : @tparam Traits Backend traits (epoll_traits, etc.).
50 : @tparam Service The concrete service type.
51 : @tparam AcceptorType The concrete acceptor type (for op base).
52 : @tparam ImplBase The public vtable base.
53 : @tparam Endpoint endpoint or local_endpoint.
54 : */
55 : template<class Derived, class Traits, class Service,
56 : class AcceptorType, class ImplBase, class Endpoint>
57 : class reactor_stream_socket_impl
58 : : public reactor_stream_socket<
59 : Derived,
60 : Service,
61 : reactor_stream_connect_op<Traits, Derived, AcceptorType, Endpoint>,
62 : reactor_stream_read_op<Traits, Derived, AcceptorType, Endpoint>,
63 : reactor_stream_write_op<Traits, Derived, AcceptorType, Endpoint>,
64 : reactor_stream_wait_op<Traits, Derived, AcceptorType, Endpoint>,
65 : typename Traits::desc_state_type,
66 : ImplBase,
67 : Endpoint>
68 : {
69 : friend Derived;
70 : friend Service;
71 :
72 HIT 25372 : explicit reactor_stream_socket_impl(Service& svc) noexcept
73 25372 : : reactor_stream_socket_impl::reactor_stream_socket(svc)
74 : {
75 25372 : }
76 :
77 : public:
78 : using impl_base_type = ImplBase;
79 :
80 : // Per-socket hook state (e.g., kqueue SO_LINGER tracking).
81 : [[no_unique_address]] typename Traits::stream_socket_hook hook_;
82 :
83 25372 : ~reactor_stream_socket_impl() override = default;
84 :
85 72 : std::error_code set_option(
86 : int level, int optname,
87 : void const* data, std::size_t size) noexcept override
88 : {
89 72 : return hook_.on_set_option(this->fd_, level, optname, data, size);
90 : }
91 :
92 : // Shadows reactor_stream_socket::close_socket so the hook fires on
93 : // every fd close path.
94 76133 : void close_socket() noexcept
95 : {
96 76133 : hook_.pre_shutdown(this->fd_);
97 76133 : this->do_close_socket();
98 76133 : }
99 : };
100 :
101 : // ============================================================
102 : // Datagram socket implementation base
103 : // ============================================================
104 :
105 : /** Intermediate base for reactor datagram sockets.
106 :
107 : @tparam Derived The named final class (CRTP self).
108 : @tparam Traits Backend traits.
109 : @tparam Service The concrete datagram service type.
110 : @tparam AcceptorType The concrete acceptor type (placeholder for op base).
111 : @tparam ImplBase The public vtable base.
112 : @tparam Endpoint endpoint or local_endpoint.
113 : */
114 : template<class Derived, class Traits, class Service,
115 : class AcceptorType, class ImplBase, class Endpoint>
116 : class reactor_dgram_socket_impl
117 : : public reactor_datagram_socket<
118 : Derived,
119 : Service,
120 : reactor_dgram_connect_op<Traits, Derived, AcceptorType, Endpoint>,
121 : reactor_dgram_send_to_op<Traits, Derived, AcceptorType, Endpoint>,
122 : reactor_dgram_recv_from_op<Traits, Derived, AcceptorType, Endpoint>,
123 : reactor_dgram_send_op<Traits, Derived, AcceptorType, Endpoint>,
124 : reactor_dgram_recv_op<Traits, Derived, AcceptorType, Endpoint>,
125 : reactor_dgram_wait_op<Traits, Derived, AcceptorType, Endpoint>,
126 : typename Traits::desc_state_type,
127 : ImplBase,
128 : Endpoint>
129 : {
130 : friend Derived;
131 : friend Service;
132 :
133 126 : explicit reactor_dgram_socket_impl(Service& svc) noexcept
134 126 : : reactor_dgram_socket_impl::reactor_datagram_socket(svc)
135 : {
136 126 : }
137 :
138 : public:
139 : using impl_base_type = ImplBase;
140 :
141 126 : ~reactor_dgram_socket_impl() override = default;
142 : };
143 :
144 : // ============================================================
145 : // Acceptor implementation base
146 : // ============================================================
147 :
148 : /** Intermediate base for reactor stream acceptors.
149 :
150 : @tparam Derived The named final class (CRTP self).
151 : @tparam Traits Backend traits.
152 : @tparam Service The concrete acceptor service type.
153 : @tparam SocketFinal The concrete stream socket type (for accept).
154 : @tparam AccImplBase The public vtable base.
155 : @tparam Endpoint endpoint or local_endpoint.
156 : */
157 : template<class Derived, class Traits, class Service,
158 : class SocketFinal, class AccImplBase, class Endpoint>
159 : class reactor_acceptor_impl
160 : : public reactor_acceptor<
161 : Derived,
162 : Service,
163 : reactor_stream_base_op<Traits, SocketFinal, Derived, Endpoint>,
164 : reactor_stream_accept_op<Traits, SocketFinal, Derived, Endpoint>,
165 : reactor_stream_wait_op<Traits, SocketFinal, Derived, Endpoint>,
166 : typename Traits::desc_state_type,
167 : AccImplBase,
168 : Endpoint>
169 : {
170 : friend Derived;
171 : friend Service;
172 :
173 195 : explicit reactor_acceptor_impl(Service& svc) noexcept
174 195 : : reactor_acceptor_impl::reactor_acceptor(svc)
175 : {
176 195 : }
177 :
178 : public:
179 : using impl_base_type = AccImplBase;
180 :
181 195 : ~reactor_acceptor_impl() override = default;
182 :
183 : std::coroutine_handle<> accept(
184 : std::coroutine_handle<>,
185 : capy::executor_ref,
186 : std::stop_token,
187 : std::error_code*,
188 : io_object::implementation**) override;
189 : };
190 :
191 : } // namespace boost::corosio::detail
192 :
193 : #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_SOCKET_FINALS_HPP
|