include/boost/corosio/native/native_local_stream_acceptor.hpp

100.0% Lines (68/0/68) 100.0% List of functions (34/0/34)
native_local_stream_acceptor.hpp
f(x) Functions (34)
Function Calls Lines Blocks
boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::get_impl() :73 8x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::get_impl() :73 8x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>&, boost::corosio::wait_type) :85 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>&, boost::corosio::wait_type) :85 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::await_ready() const :92 3x 100.0% 88.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const :92 3x 100.0% 88.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::await_resume() const :99 3x 100.0% 86.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const :99 3x 100.0% 86.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :106 3x 100.0% 82.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :106 3x 100.0% 82.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>&, boost::corosio::local_stream_socket&) :122 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>&, boost::corosio::local_stream_socket&) :122 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_ready() const :130 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_ready() const :130 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_resume() const :137 4x 100.0% 70.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_resume() const :137 4x 100.0% 70.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :146 3x 100.0% 82.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :146 3x 100.0% 82.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::native_move_accept_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>&) :162 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::native_move_accept_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>&) :162 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::await_ready() const :168 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::await_ready() const :168 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::await_resume() const :176 3x 100.0% 78.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::await_resume() const :176 3x 100.0% 78.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :191 2x 100.0% 82.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :191 2x 100.0% 82.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_local_stream_acceptor(boost::capy::execution_context&) :205 9x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_local_stream_acceptor(boost::capy::execution_context&) :205 9x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_local_stream_acceptor(boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>&&) :225 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_local_stream_acceptor(boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>&&) :225 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::accept(boost::corosio::local_stream_socket&) :250 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::accept(boost::corosio::local_stream_socket&) :250 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::accept() :274 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::accept() :274 4x 100.0% 100.0%
Line TLA Hits 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_URING
31 #include <boost/corosio/native/detail/uring/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 16x impl_type& get_impl() noexcept
74 {
75 16x 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 6x native_wait_awaitable(
86 native_local_stream_acceptor& acc, wait_type w) noexcept
87 6x : acc_(acc)
88 6x , w_(w)
89 {
90 6x }
91
92 6x bool await_ready() const noexcept
93 {
94 // A pre-set ec_ means the initiator failed before
95 // dispatch (e.g. a closed object).
96 6x return static_cast<bool>(ec_) || token_.stop_requested();
97 }
98
99 6x [[nodiscard]] capy::io_result<> await_resume() const noexcept
100 {
101 6x if (token_.stop_requested())
102 2x return {make_error_code(std::errc::operation_canceled)};
103 4x return {ec_};
104 }
105
106 6x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
107 -> std::coroutine_handle<>
108 {
109 6x token_ = env->stop_token;
110 6x return acc_.get_impl().wait(h, env->executor, w_, token_, &ec_);
111 }
112 };
113
114 struct native_accept_awaitable
115 {
116 native_local_stream_acceptor& acc_;
117 local_stream_socket& peer_;
118 std::stop_token token_;
119 mutable std::error_code ec_;
120 mutable io_object::implementation* peer_impl_ = nullptr;
121
122 8x native_accept_awaitable(
123 native_local_stream_acceptor& acc,
124 local_stream_socket& peer) noexcept
125 8x : acc_(acc)
126 8x , peer_(peer)
127 {
128 8x }
129
130 8x bool await_ready() const noexcept
131 {
132 // A pre-set ec_ means the initiator failed before
133 // dispatch (e.g. a closed object).
134 8x return static_cast<bool>(ec_) || token_.stop_requested();
135 }
136
137 8x [[nodiscard]] capy::io_result<> await_resume() const noexcept
138 {
139 8x if (token_.stop_requested())
140 2x return {make_error_code(std::errc::operation_canceled)};
141 6x if (!ec_)
142 4x acc_.reset_peer_impl(peer_, peer_impl_);
143 6x return {ec_};
144 }
145
146 6x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
147 -> std::coroutine_handle<>
148 {
149 6x token_ = env->stop_token;
150 18x return acc_.get_impl().accept(
151 18x h, env->executor, token_, &ec_, &peer_impl_);
152 }
153 };
154
155 struct native_move_accept_awaitable
156 {
157 native_local_stream_acceptor& acc_;
158 std::stop_token token_;
159 mutable std::error_code ec_;
160 mutable io_object::implementation* peer_impl_ = nullptr;
161
162 6x explicit native_move_accept_awaitable(
163 native_local_stream_acceptor& acc) noexcept
164 6x : acc_(acc)
165 {
166 6x }
167
168 6x bool await_ready() const noexcept
169 {
170 // A pre-set ec_ means the initiator failed before
171 // dispatch (e.g. a closed object).
172 6x return static_cast<bool>(ec_) || token_.stop_requested();
173 }
174
175 [[nodiscard]] capy::io_result<native_local_stream_socket<Backend>>
176 6x await_resume() const noexcept
177 {
178 6x if (token_.stop_requested())
179 return {
180 2x make_error_code(std::errc::operation_canceled),
181 2x native_local_stream_socket<Backend>(acc_.context())};
182 4x if (ec_ || !peer_impl_)
183 return {
184 2x ec_, native_local_stream_socket<Backend>(acc_.context())};
185
186 2x native_local_stream_socket<Backend> peer(acc_.context());
187 2x acc_.reset_peer_impl(peer, peer_impl_);
188 2x return {ec_, std::move(peer)};
189 2x }
190
191 4x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
192 -> std::coroutine_handle<>
193 {
194 4x token_ = env->stop_token;
195 12x return acc_.get_impl().accept(
196 12x h, env->executor, token_, &ec_, &peer_impl_);
197 }
198 };
199
200 public:
201 /** Construct a native acceptor from an execution context.
202
203 @param ctx The execution context that will own this acceptor.
204 */
205 18x explicit native_local_stream_acceptor(capy::execution_context& ctx)
206 18x : local_stream_acceptor(create_handle<service_type>(ctx), ctx)
207 {
208 18x }
209
210 /** Construct a native acceptor from an executor.
211
212 @param ex The executor whose context will own the acceptor.
213 */
214 template<class Ex>
215 requires(!std::same_as<
216 std::remove_cvref_t<Ex>,
217 native_local_stream_acceptor>) &&
218 capy::Executor<Ex>
219 explicit native_local_stream_acceptor(Ex const& ex)
220 : native_local_stream_acceptor(ex.context())
221 {
222 }
223
224 /// Move construct.
225 2x native_local_stream_acceptor(native_local_stream_acceptor&&) noexcept =
226 default;
227
228 /// Move assign.
229 native_local_stream_acceptor&
230 operator=(native_local_stream_acceptor&&) noexcept = default;
231
232 native_local_stream_acceptor(native_local_stream_acceptor const&) = delete;
233 native_local_stream_acceptor&
234 operator=(native_local_stream_acceptor const&) = delete;
235
236 /** Asynchronously accept an incoming connection.
237
238 Calls the backend implementation directly, bypassing virtual
239 dispatch. Otherwise identical to @ref local_stream_acceptor::accept.
240
241 @param peer The socket to receive the accepted connection.
242
243 @return An awaitable yielding `io_result<>`.
244
245 A closed acceptor reports `errc::bad_file_descriptor`.
246
247 Both this acceptor and @p peer must outlive the returned
248 awaitable.
249 */
250 8x [[nodiscard]] auto accept(local_stream_socket& peer)
251 {
252 8x native_accept_awaitable aw(*this, peer);
253 8x if (!is_open())
254 2x aw.ec_ = make_error_code(std::errc::bad_file_descriptor);
255 8x return aw;
256 }
257
258 /** Asynchronously accept an incoming connection, returning the peer.
259
260 Calls the backend implementation directly, bypassing virtual
261 dispatch. The accepted peer is returned as a
262 @ref native_local_stream_socket so that subsequent I/O on it
263 is also devirtualized.
264
265 @return An awaitable yielding
266 `io_result<native_local_stream_socket<Backend>>`.
267
268 A closed acceptor reports `errc::bad_file_descriptor`.
269
270 @throws std::logic_error If the acceptor has been moved from.
271
272 This acceptor must outlive the returned awaitable.
273 */
274 8x [[nodiscard]] auto accept()
275 {
276 // The awaitable builds the peer from context(), which a
277 // moved-from acceptor no longer has.
278 8x if (!h_)
279 2x detail::throw_logic_error("accept: acceptor moved-from");
280 6x native_move_accept_awaitable aw(*this);
281 6x if (!is_open())
282 2x aw.ec_ = make_error_code(std::errc::bad_file_descriptor);
283 6x return aw;
284 }
285
286 /** Asynchronously wait for the acceptor to be ready.
287
288 Calls the backend implementation directly, bypassing virtual
289 dispatch. Otherwise identical to @ref local_stream_acceptor::wait.
290
291 @param w The wait direction (typically `wait_type::read`).
292
293 @return An awaitable yielding `io_result<>`.
294 */
295 6x [[nodiscard]] auto wait(wait_type w)
296 {
297 6x return native_wait_awaitable(*this, w);
298 }
299 };
300
301 } // namespace boost::corosio
302
303 #endif // BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_ACCEPTOR_HPP
304