TLA Line data Source code
1 : //
2 : // Copyright (c) 2026 Steve Gerbino
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_NATIVE_LOCAL_STREAM_ACCEPTOR_HPP
11 : #define BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_ACCEPTOR_HPP
12 :
13 : #include <boost/corosio/local_stream_acceptor.hpp>
14 : #include <boost/corosio/native/native_local_stream_socket.hpp>
15 : #include <boost/corosio/backend.hpp>
16 :
17 : #ifndef BOOST_COROSIO_MRDOCS
18 : #if BOOST_COROSIO_HAS_EPOLL
19 : #include <boost/corosio/native/detail/epoll/epoll_types.hpp>
20 : #endif
21 :
22 : #if BOOST_COROSIO_HAS_SELECT
23 : #include <boost/corosio/native/detail/select/select_types.hpp>
24 : #endif
25 :
26 : #if BOOST_COROSIO_HAS_KQUEUE
27 : #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp>
28 : #endif
29 :
30 : #if BOOST_COROSIO_HAS_IO_URING
31 : #include <boost/corosio/native/detail/io_uring/io_uring_types.hpp>
32 : #endif
33 :
34 : #if BOOST_COROSIO_HAS_IOCP
35 : #include <boost/corosio/native/detail/iocp/win_local_stream_acceptor_service.hpp>
36 : #endif
37 : #endif // !BOOST_COROSIO_MRDOCS
38 :
39 : namespace boost::corosio {
40 :
41 : /** An asynchronous Unix stream acceptor with devirtualized accept.
42 :
43 : This class template inherits from @ref local_stream_acceptor
44 : and shadows both `accept` overloads (the peer-reference form
45 : and the move-return form) with versions that call the backend
46 : implementation directly, allowing the compiler to inline
47 : through the entire call chain. The move-return form yields a
48 : @ref native_local_stream_socket so subsequent I/O on the peer
49 : is also devirtualized.
50 :
51 : Non-async operations (`listen`, `close`, `cancel`) remain
52 : unchanged and dispatch through the compiled library.
53 :
54 : A `native_local_stream_acceptor` IS-A `local_stream_acceptor`
55 : and can be passed to any function expecting
56 : `local_stream_acceptor&`.
57 :
58 : @tparam Backend A backend tag value (e.g., `epoll`).
59 :
60 : @par Thread Safety
61 : Same as @ref local_stream_acceptor.
62 :
63 : @see local_stream_acceptor, epoll_t, iocp_t
64 : */
65 : template<auto Backend>
66 : class native_local_stream_acceptor : public local_stream_acceptor
67 : {
68 : using backend_type = decltype(Backend);
69 : using impl_type = typename backend_type::local_stream_acceptor_type;
70 : using service_type =
71 : typename backend_type::local_stream_acceptor_service_type;
72 :
73 HIT 8 : impl_type& get_impl() noexcept
74 : {
75 8 : return *static_cast<impl_type*>(h_.get());
76 : }
77 :
78 : struct native_wait_awaitable
79 : {
80 : native_local_stream_acceptor& acc_;
81 : wait_type w_;
82 : std::stop_token token_;
83 : mutable std::error_code ec_;
84 :
85 2 : native_wait_awaitable(
86 : native_local_stream_acceptor& acc, wait_type w) noexcept
87 2 : : acc_(acc)
88 2 : , w_(w)
89 : {
90 2 : }
91 :
92 2 : bool await_ready() const noexcept
93 : {
94 2 : return token_.stop_requested();
95 : }
96 :
97 2 : capy::io_result<> await_resume() const noexcept
98 : {
99 2 : if (token_.stop_requested())
100 MIS 0 : return {make_error_code(std::errc::operation_canceled)};
101 HIT 2 : return {ec_};
102 : }
103 :
104 2 : auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
105 : -> std::coroutine_handle<>
106 : {
107 2 : token_ = env->stop_token;
108 6 : return acc_.get_impl().wait(
109 6 : h, env->executor, w_, token_, &ec_);
110 : }
111 : };
112 :
113 : struct native_accept_awaitable
114 : {
115 : native_local_stream_acceptor& acc_;
116 : local_stream_socket& peer_;
117 : std::stop_token token_;
118 : mutable std::error_code ec_;
119 : mutable io_object::implementation* peer_impl_ = nullptr;
120 :
121 4 : native_accept_awaitable(
122 : native_local_stream_acceptor& acc,
123 : local_stream_socket& peer) noexcept
124 4 : : acc_(acc)
125 4 : , peer_(peer)
126 : {
127 4 : }
128 :
129 4 : bool await_ready() const noexcept
130 : {
131 4 : return token_.stop_requested();
132 : }
133 :
134 4 : capy::io_result<> await_resume() const noexcept
135 : {
136 4 : if (token_.stop_requested())
137 MIS 0 : return {make_error_code(std::errc::operation_canceled)};
138 HIT 4 : if (!ec_)
139 4 : acc_.reset_peer_impl(peer_, peer_impl_);
140 4 : return {ec_};
141 : }
142 :
143 4 : auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
144 : -> std::coroutine_handle<>
145 : {
146 4 : token_ = env->stop_token;
147 12 : return acc_.get_impl().accept(
148 12 : h, env->executor, token_, &ec_, &peer_impl_);
149 : }
150 : };
151 :
152 : struct native_move_accept_awaitable
153 : {
154 : native_local_stream_acceptor& acc_;
155 : std::stop_token token_;
156 : mutable std::error_code ec_;
157 : mutable io_object::implementation* peer_impl_ = nullptr;
158 :
159 2 : explicit native_move_accept_awaitable(
160 : native_local_stream_acceptor& acc) noexcept
161 2 : : acc_(acc)
162 : {
163 2 : }
164 :
165 2 : bool await_ready() const noexcept
166 : {
167 2 : return token_.stop_requested();
168 : }
169 :
170 : capy::io_result<native_local_stream_socket<Backend>>
171 2 : await_resume() const noexcept
172 : {
173 2 : if (token_.stop_requested())
174 : return {
175 MIS 0 : make_error_code(std::errc::operation_canceled),
176 0 : native_local_stream_socket<Backend>(acc_.context())};
177 HIT 2 : if (ec_ || !peer_impl_)
178 : return {
179 : ec_,
180 MIS 0 : native_local_stream_socket<Backend>(acc_.context())};
181 :
182 HIT 2 : native_local_stream_socket<Backend> peer(acc_.context());
183 2 : acc_.reset_peer_impl(peer, peer_impl_);
184 2 : return {ec_, std::move(peer)};
185 2 : }
186 :
187 2 : auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
188 : -> std::coroutine_handle<>
189 : {
190 2 : token_ = env->stop_token;
191 6 : return acc_.get_impl().accept(
192 6 : h, env->executor, token_, &ec_, &peer_impl_);
193 : }
194 : };
195 :
196 : public:
197 : /** Construct a native acceptor from an execution context.
198 :
199 : @param ctx The execution context that will own this acceptor.
200 : */
201 12 : explicit native_local_stream_acceptor(capy::execution_context& ctx)
202 12 : : local_stream_acceptor(create_handle<service_type>(ctx), ctx)
203 : {
204 12 : }
205 :
206 : /** Construct a native acceptor from an executor.
207 :
208 : @param ex The executor whose context will own the acceptor.
209 : */
210 : template<class Ex>
211 : requires(!std::same_as<
212 : std::remove_cvref_t<Ex>,
213 : native_local_stream_acceptor>) &&
214 : capy::Executor<Ex>
215 : explicit native_local_stream_acceptor(Ex const& ex)
216 : : native_local_stream_acceptor(ex.context())
217 : {
218 : }
219 :
220 : /// Move construct.
221 : native_local_stream_acceptor(native_local_stream_acceptor&&) noexcept =
222 : default;
223 :
224 : /// Move assign.
225 : native_local_stream_acceptor&
226 : operator=(native_local_stream_acceptor&&) noexcept = default;
227 :
228 : native_local_stream_acceptor(native_local_stream_acceptor const&) = delete;
229 : native_local_stream_acceptor&
230 : operator=(native_local_stream_acceptor const&) = delete;
231 :
232 : /** Asynchronously accept an incoming connection.
233 :
234 : Calls the backend implementation directly, bypassing virtual
235 : dispatch. Otherwise identical to @ref local_stream_acceptor::accept.
236 :
237 : @param peer The socket to receive the accepted connection.
238 :
239 : @return An awaitable yielding `io_result<>`.
240 :
241 : @throws std::logic_error if the acceptor is not listening.
242 :
243 : Both this acceptor and @p peer must outlive the returned
244 : awaitable.
245 : */
246 6 : auto accept(local_stream_socket& peer)
247 : {
248 6 : if (!is_open())
249 2 : detail::throw_logic_error("accept: acceptor not listening");
250 4 : return native_accept_awaitable(*this, peer);
251 : }
252 :
253 : /** Asynchronously accept an incoming connection, returning the peer.
254 :
255 : Calls the backend implementation directly, bypassing virtual
256 : dispatch. The accepted peer is returned as a
257 : @ref native_local_stream_socket so that subsequent I/O on it
258 : is also devirtualized.
259 :
260 : @return An awaitable yielding
261 : `io_result<native_local_stream_socket<Backend>>`.
262 :
263 : @throws std::logic_error if the acceptor is not listening.
264 :
265 : This acceptor must outlive the returned awaitable.
266 : */
267 4 : auto accept()
268 : {
269 4 : if (!is_open())
270 2 : detail::throw_logic_error("accept: acceptor not listening");
271 2 : return native_move_accept_awaitable(*this);
272 : }
273 :
274 : /** Asynchronously wait for the acceptor to be ready.
275 :
276 : Calls the backend implementation directly, bypassing virtual
277 : dispatch. Otherwise identical to @ref local_stream_acceptor::wait.
278 :
279 : @param w The wait direction (typically `wait_type::read`).
280 :
281 : @return An awaitable yielding `io_result<>`.
282 : */
283 2 : [[nodiscard]] auto wait(wait_type w)
284 : {
285 2 : return native_wait_awaitable(*this, w);
286 : }
287 : };
288 :
289 : } // namespace boost::corosio
290 :
291 : #endif // BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_ACCEPTOR_HPP
|