include/boost/corosio/native/native_tcp_acceptor.hpp

96.3% Lines (78/81) 100.0% List of functions (50/50) 79.4% Branches (54/68)
native_tcp_acceptor.hpp
f(x) Functions (50)
Function Calls Lines Branches Blocks
boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::~native_tcp_acceptor() :61 42x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::~native_tcp_acceptor() :61 40x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::get_impl() :67 15x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::get_impl() :67 14x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable&&) :72 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::~native_wait_awaitable() :72 12x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable&&) :72 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::~native_wait_awaitable() :72 12x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>&, boost::corosio::wait_type) :79 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>&, boost::corosio::wait_type) :79 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::await_ready() const :85 3x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const :85 3x 100.0% 50.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::await_resume() const :92 3x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const :92 3x 100.0% 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::await_suspend(std::__1::coroutine_handle<void>, boost::capy::io_env const*) :99 3x 100.0% 50.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_suspend(std::__1::coroutine_handle<void>, boost::capy::io_env const*) :99 3x 75.0% 50.0% 50.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable&&) :107 44x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::~native_accept_awaitable() :107 66x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable&&) :107 40x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::~native_accept_awaitable() :107 60x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>&, boost::corosio::tcp_socket&) :115 22x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>&, boost::corosio::tcp_socket&) :115 20x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::await_ready() const :122 11x 100.0% 50.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_ready() const :122 10x 100.0% 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::await_resume() const :129 11x 85.7% 50.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_resume() const :129 10x 100.0% 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::await_suspend(std::__1::coroutine_handle<void>, boost::capy::io_env const*) :138 10x 80.0% 50.0% 50.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_suspend(std::__1::coroutine_handle<void>, boost::capy::io_env const*) :138 9x 80.0% 50.0% 50.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_value_awaitable::native_accept_value_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_value_awaitable&&) :147 12x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_value_awaitable::~native_accept_value_awaitable() :147 18x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::native_accept_value_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable&&) :147 12x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::~native_accept_value_awaitable() :147 18x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_value_awaitable::native_accept_value_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>&) :155 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::native_accept_value_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>&) :155 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_value_awaitable::await_ready() const :161 3x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::await_ready() const :161 3x 100.0% 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_value_awaitable::await_resume() :168 3x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::await_resume() :168 3x 100.0% 83.3% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_value_awaitable::await_suspend(std::__1::coroutine_handle<void>, boost::capy::io_env const*) :179 2x 100.0% 50.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::await_suspend(std::__1::coroutine_handle<void>, boost::capy::io_env const*) :179 2x 80.0% 50.0% 50.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_tcp_acceptor(boost::capy::execution_context&) :193 38x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_tcp_acceptor(boost::capy::execution_context&) :193 36x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_tcp_acceptor(boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>&&) :218 4x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_tcp_acceptor(boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>&&) :218 4x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::accept(boost::corosio::tcp_socket&) :248 11x 83.3% 50.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::accept(boost::corosio::tcp_socket&) :248 10x 100.0% 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::accept() :269 4x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::accept() :269 4x 100.0% 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::wait(boost::corosio::wait_type) :290 3x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::wait(boost::corosio::wait_type) :290 3x 100.0% 100.0%
Line Branch 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_TCP_ACCEPTOR_HPP
11 #define BOOST_COROSIO_NATIVE_NATIVE_TCP_ACCEPTOR_HPP
12
13 #include <boost/corosio/tcp_acceptor.hpp>
14 #include <boost/corosio/backend.hpp>
15
16 #ifndef BOOST_COROSIO_MRDOCS
17 #if BOOST_COROSIO_HAS_EPOLL
18 #include <boost/corosio/native/detail/epoll/epoll_types.hpp>
19 #endif
20
21 #if BOOST_COROSIO_HAS_SELECT
22 #include <boost/corosio/native/detail/select/select_types.hpp>
23 #endif
24
25 #if BOOST_COROSIO_HAS_KQUEUE
26 #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp>
27 #endif
28
29 #if BOOST_COROSIO_HAS_IOCP
30 #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp>
31 #endif
32
33 #if BOOST_COROSIO_HAS_URING
34 #include <boost/corosio/native/detail/uring/uring_types.hpp>
35 #endif
36 #endif // !BOOST_COROSIO_MRDOCS
37
38 namespace boost::corosio {
39
40 /** An asynchronous TCP acceptor with devirtualized accept operations.
41
42 This class template inherits from @ref tcp_acceptor and shadows
43 the `accept` operation with a version that calls the backend
44 implementation directly, allowing the compiler to inline through
45 the entire call chain.
46
47 Non-async operations (`listen`, `close`, `cancel`) remain
48 unchanged and dispatch through the compiled library.
49
50 A `native_tcp_acceptor` IS-A `tcp_acceptor` and can be passed
51 to any function expecting `tcp_acceptor&`.
52
53 @tparam Backend A backend tag value (e.g., `epoll`).
54
55 @par Thread Safety
56 Same as @ref tcp_acceptor.
57
58 @see tcp_acceptor, epoll_t, iocp_t
59 */
60 template<auto Backend>
61 class native_tcp_acceptor : public tcp_acceptor
62 {
63 using backend_type = decltype(Backend);
64 using impl_type = typename backend_type::tcp_acceptor_type;
65 using service_type = typename backend_type::tcp_acceptor_service_type;
66
67 29x impl_type& get_impl() noexcept
68 {
69 29x return *static_cast<impl_type*>(h_.get());
70 }
71
72 struct native_wait_awaitable
73 {
74 native_tcp_acceptor& acc_;
75 wait_type w_;
76 std::stop_token token_;
77 mutable std::error_code ec_;
78
79 18x native_wait_awaitable(native_tcp_acceptor& acc, wait_type w) noexcept
80 6x : acc_(acc)
81 6x , w_(w)
82 6x {
83 12x }
84
85 6x bool await_ready() const noexcept
86 {
87 // A pre-set ec_ means the initiator failed before
88 // dispatch (e.g. a closed object).
89
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
6x return static_cast<bool>(ec_) || token_.stop_requested();
90 }
91
92 6x [[nodiscard]] capy::io_result<> await_resume() const noexcept
93 {
94
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
6x if (token_.stop_requested())
95 2x return {make_error_code(std::errc::operation_canceled)};
96 4x return {ec_};
97 6x }
98
99 6x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
100 -> std::coroutine_handle<>
101 {
102 6x token_ = env->stop_token;
103
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6x return acc_.get_impl().wait(h, env->executor, w_, token_, &ec_);
104 }
105 };
106
107 struct native_accept_awaitable
108 {
109 native_tcp_acceptor& acc_;
110 tcp_socket& peer_;
111 std::stop_token token_;
112 mutable std::error_code ec_;
113 21x mutable io_object::implementation* peer_impl_ = nullptr;
114
115 63x native_accept_awaitable(
116 native_tcp_acceptor& acc, tcp_socket& peer) noexcept
117 21x : acc_(acc)
118 21x , peer_(peer)
119 21x {
120 42x }
121
122 21x bool await_ready() const noexcept
123 {
124 // A pre-set ec_ means the initiator failed before
125 // dispatch (e.g. a closed object).
126
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 9 times.
21x return static_cast<bool>(ec_) || token_.stop_requested();
127 }
128
129 21x [[nodiscard]] capy::io_result<> await_resume() const noexcept
130 {
131
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 9 times.
21x if (token_.stop_requested())
132 2x return {make_error_code(std::errc::operation_canceled)};
133
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 8 times.
19x if (!ec_)
134 17x acc_.reset_peer_impl(peer_, peer_impl_);
135 19x return {ec_};
136 21x }
137
138 19x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
139 -> std::coroutine_handle<>
140 {
141 19x token_ = env->stop_token;
142
4/8
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
38x return acc_.get_impl().accept(
143 19x h, env->executor, token_, &ec_, &peer_impl_);
144 }
145 };
146
147 struct native_accept_value_awaitable
148 {
149 native_tcp_acceptor& acc_;
150 tcp_socket peer_;
151 std::stop_token token_;
152 mutable std::error_code ec_;
153 6x mutable io_object::implementation* peer_impl_ = nullptr;
154
155 18x explicit native_accept_value_awaitable(native_tcp_acceptor& acc)
156 6x : acc_(acc)
157 6x , peer_(acc.context())
158 6x {
159 12x }
160
161 6x bool await_ready() const noexcept
162 {
163 // A pre-set ec_ means the initiator failed before
164 // dispatch (e.g. a closed object).
165
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
6x return static_cast<bool>(ec_) || token_.stop_requested();
166 }
167
168 6x [[nodiscard]] capy::io_result<tcp_socket> await_resume() noexcept
169 {
170
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
6x if (token_.stop_requested())
171 2x return {
172 2x make_error_code(std::errc::operation_canceled),
173 2x std::move(peer_)};
174
6/8
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
4x if (!ec_ && peer_impl_)
175 2x acc_.reset_peer_impl(peer_, peer_impl_);
176 4x return {ec_, std::move(peer_)};
177 6x }
178
179 4x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
180 -> std::coroutine_handle<>
181 {
182 4x token_ = env->stop_token;
183
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
8x return acc_.get_impl().accept(
184 4x h, env->executor, token_, &ec_, &peer_impl_);
185 }
186 };
187
188 public:
189 /** Construct a native acceptor from an execution context.
190
191 @param ctx The execution context that will own this acceptor.
192 */
193 74x explicit native_tcp_acceptor(capy::execution_context& ctx)
194 37x : tcp_acceptor(create_handle<service_type>(ctx))
195 74x {
196 74x }
197
198 /** Construct a native acceptor from an executor.
199
200 @param ex The executor whose context will own the acceptor.
201 */
202 template<class Ex>
203 requires(!std::same_as<std::remove_cvref_t<Ex>, native_tcp_acceptor>) &&
204 capy::Executor<Ex>
205 explicit native_tcp_acceptor(Ex const& ex)
206 : native_tcp_acceptor(ex.context())
207 {
208 }
209
210 /** Move construct.
211
212 @param other The acceptor to move from.
213
214 @pre No awaitables returned by @p other's methods exist.
215 @pre The execution context associated with @p other must
216 outlive this acceptor.
217 */
218 8x native_tcp_acceptor(native_tcp_acceptor&&) noexcept = default;
219
220 /** Move assign.
221
222 @param other The acceptor to move from.
223
224 @pre No awaitables returned by either `*this` or @p other's
225 methods exist.
226 @pre The execution context associated with @p other must
227 outlive this acceptor.
228 */
229 native_tcp_acceptor& operator=(native_tcp_acceptor&&) noexcept = default;
230
231 native_tcp_acceptor(native_tcp_acceptor const&) = delete;
232 native_tcp_acceptor& operator=(native_tcp_acceptor const&) = delete;
233
234 /** Asynchronously accept an incoming connection.
235
236 Calls the backend implementation directly, bypassing virtual
237 dispatch. Otherwise identical to @ref tcp_acceptor::accept.
238
239 @param peer The socket to receive the accepted connection.
240
241 @return An awaitable yielding `io_result<>`.
242
243 A closed acceptor reports `errc::bad_file_descriptor`.
244
245 Both this acceptor and @p peer must outlive the returned
246 awaitable.
247 */
248 21x [[nodiscard]] auto accept(tcp_socket& peer)
249 {
250 21x native_accept_awaitable aw(*this, peer);
251
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 1 time.
21x if (!is_open())
252 2x aw.ec_ = make_error_code(std::errc::bad_file_descriptor);
253 21x return aw;
254 21x }
255
256 /** Asynchronously accept an incoming connection, returning the peer.
257
258 Calls the backend implementation directly, bypassing virtual
259 dispatch. Otherwise identical to @ref tcp_acceptor::accept().
260
261 @return An awaitable yielding `io_result<tcp_socket>`.
262
263 A closed acceptor reports `errc::bad_file_descriptor`.
264
265 @throws std::logic_error If the acceptor has been moved from.
266
267 This acceptor must outlive the returned awaitable.
268 */
269 8x [[nodiscard]] auto accept()
270 {
271 // The awaitable builds the peer from context(), which a
272 // moved-from acceptor no longer has.
273
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 time.
8x if (!h_)
274 2x detail::throw_logic_error("accept: acceptor moved-from");
275 6x native_accept_value_awaitable aw(*this);
276
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 time.
6x if (!is_open())
277 2x aw.ec_ = make_error_code(std::errc::bad_file_descriptor);
278 6x return aw;
279 6x }
280
281 /** Asynchronously wait for the acceptor to be ready.
282
283 Calls the backend implementation directly, bypassing virtual
284 dispatch. Otherwise identical to @ref tcp_acceptor::wait.
285
286 @param w The wait direction (typically `wait_type::read`).
287
288 @return An awaitable yielding `io_result<>`.
289 */
290 6x [[nodiscard]] auto wait(wait_type w)
291 {
292 6x return native_wait_awaitable(*this, w);
293 }
294 };
295
296 } // namespace boost::corosio
297
298 #endif
299