include/boost/corosio/native/native_local_stream_acceptor.hpp

92.2% Lines (59/64) 100.0% List of functions (32/32)
native_local_stream_acceptor.hpp
f(x) Functions (32)
Function Calls Lines Blocks
boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::get_impl() :73 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::get_impl() :73 4x 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 1x 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 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::await_ready() const :92 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const :92 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::await_resume() const :97 1x 75.0% 67.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const :97 1x 75.0% 67.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*) :104 1x 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*) :104 1x 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&) :121 2x 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&) :121 2x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_ready() const :129 2x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_ready() const :129 2x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_resume() const :134 2x 83.3% 78.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_resume() const :134 2x 83.3% 78.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*) :143 2x 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*) :143 2x 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{}>&) :159 1x 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{}>&) :159 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::await_ready() const :165 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::await_ready() const :165 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::await_resume() const :171 1x 70.0% 59.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::await_resume() const :171 1x 70.0% 59.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*) :187 1x 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*) :187 1x 100.0% 82.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_local_stream_acceptor(boost::capy::execution_context&) :201 6x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_local_stream_acceptor(boost::capy::execution_context&) :201 6x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::accept(boost::corosio::local_stream_socket&) :246 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::accept(boost::corosio::local_stream_socket&) :246 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::accept() :267 2x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::accept() :267 2x 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_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 8x impl_type& get_impl() noexcept
74 {
75 8x 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 2x native_wait_awaitable(
86 native_local_stream_acceptor& acc, wait_type w) noexcept
87 2x : acc_(acc)
88 2x , w_(w)
89 {
90 2x }
91
92 2x bool await_ready() const noexcept
93 {
94 2x return token_.stop_requested();
95 }
96
97 2x capy::io_result<> await_resume() const noexcept
98 {
99 2x if (token_.stop_requested())
100 return {make_error_code(std::errc::operation_canceled)};
101 2x return {ec_};
102 }
103
104 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
105 -> std::coroutine_handle<>
106 {
107 2x token_ = env->stop_token;
108 6x return acc_.get_impl().wait(
109 6x 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 4x native_accept_awaitable(
122 native_local_stream_acceptor& acc,
123 local_stream_socket& peer) noexcept
124 4x : acc_(acc)
125 4x , peer_(peer)
126 {
127 4x }
128
129 4x bool await_ready() const noexcept
130 {
131 4x return token_.stop_requested();
132 }
133
134 4x capy::io_result<> await_resume() const noexcept
135 {
136 4x if (token_.stop_requested())
137 return {make_error_code(std::errc::operation_canceled)};
138 4x if (!ec_)
139 4x acc_.reset_peer_impl(peer_, peer_impl_);
140 4x return {ec_};
141 }
142
143 4x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
144 -> std::coroutine_handle<>
145 {
146 4x token_ = env->stop_token;
147 12x return acc_.get_impl().accept(
148 12x 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 2x explicit native_move_accept_awaitable(
160 native_local_stream_acceptor& acc) noexcept
161 2x : acc_(acc)
162 {
163 2x }
164
165 2x bool await_ready() const noexcept
166 {
167 2x return token_.stop_requested();
168 }
169
170 capy::io_result<native_local_stream_socket<Backend>>
171 2x await_resume() const noexcept
172 {
173 2x if (token_.stop_requested())
174 return {
175 make_error_code(std::errc::operation_canceled),
176 native_local_stream_socket<Backend>(acc_.context())};
177 2x if (ec_ || !peer_impl_)
178 return {
179 ec_,
180 native_local_stream_socket<Backend>(acc_.context())};
181
182 2x native_local_stream_socket<Backend> peer(acc_.context());
183 2x acc_.reset_peer_impl(peer, peer_impl_);
184 2x return {ec_, std::move(peer)};
185 2x }
186
187 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
188 -> std::coroutine_handle<>
189 {
190 2x token_ = env->stop_token;
191 6x return acc_.get_impl().accept(
192 6x 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 12x explicit native_local_stream_acceptor(capy::execution_context& ctx)
202 12x : local_stream_acceptor(create_handle<service_type>(ctx), ctx)
203 {
204 12x }
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 6x auto accept(local_stream_socket& peer)
247 {
248 6x if (!is_open())
249 2x detail::throw_logic_error("accept: acceptor not listening");
250 4x 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 4x auto accept()
268 {
269 4x if (!is_open())
270 2x detail::throw_logic_error("accept: acceptor not listening");
271 2x 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 2x [[nodiscard]] auto wait(wait_type w)
284 {
285 2x return native_wait_awaitable(*this, w);
286 }
287 };
288
289 } // namespace boost::corosio
290
291 #endif // BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_ACCEPTOR_HPP
292