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