include/boost/corosio/native/native_tcp_acceptor.hpp
92.3% Lines (24/26)
100.0% List of functions (8/8)
62.5% Branches (5/8)
Functions (8)
Function
Calls
Lines
Branches
Blocks
boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>::get_impl()
:63
5x
100.0%
–
100.0%
boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>&, boost::corosio::tcp_socket&)
:76
5x
100.0%
–
100.0%
boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>::native_accept_awaitable::await_ready() const
:83
5x
100.0%
–
100.0%
boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>::native_accept_awaitable::await_resume() const
:88
5x
83.3%
50.0%
87.5%
boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>::native_accept_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:97
5x
100.0%
100.0%
81.8%
boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>::native_tcp_acceptor(boost::capy::execution_context&)
:111
8x
100.0%
100.0%
100.0%
boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>::native_tcp_acceptor(boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>&&)
:136
1x
100.0%
–
100.0%
boost::corosio::native_tcp_acceptor<boost::corosio::iocp_t{}>::accept(boost::corosio::tcp_socket&)
:166
5x
75.0%
50.0%
80.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_tcp_acceptor_service.hpp> | |||
| 19 | #endif | |||
| 20 | ||||
| 21 | #if BOOST_COROSIO_HAS_SELECT | |||
| 22 | #include <boost/corosio/native/detail/select/select_tcp_acceptor_service.hpp> | |||
| 23 | #endif | |||
| 24 | ||||
| 25 | #if BOOST_COROSIO_HAS_KQUEUE | |||
| 26 | #include <boost/corosio/native/detail/kqueue/kqueue_tcp_acceptor_service.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 | #endif // !BOOST_COROSIO_MRDOCS | |||
| 33 | ||||
| 34 | namespace boost::corosio { | |||
| 35 | ||||
| 36 | /** An asynchronous TCP acceptor with devirtualized accept operations. | |||
| 37 | ||||
| 38 | This class template inherits from @ref tcp_acceptor and shadows | |||
| 39 | the `accept` operation with a version that calls the backend | |||
| 40 | implementation directly, allowing the compiler to inline through | |||
| 41 | the entire call chain. | |||
| 42 | ||||
| 43 | Non-async operations (`listen`, `close`, `cancel`) remain | |||
| 44 | unchanged and dispatch through the compiled library. | |||
| 45 | ||||
| 46 | A `native_tcp_acceptor` IS-A `tcp_acceptor` and can be passed | |||
| 47 | to any function expecting `tcp_acceptor&`. | |||
| 48 | ||||
| 49 | @tparam Backend A backend tag value (e.g., `epoll`). | |||
| 50 | ||||
| 51 | @par Thread Safety | |||
| 52 | Same as @ref tcp_acceptor. | |||
| 53 | ||||
| 54 | @see tcp_acceptor, epoll_t, iocp_t | |||
| 55 | */ | |||
| 56 | template<auto Backend> | |||
| 57 | class native_tcp_acceptor : public tcp_acceptor | |||
| 58 | { | |||
| 59 | using backend_type = decltype(Backend); | |||
| 60 | using impl_type = typename backend_type::tcp_acceptor_type; | |||
| 61 | using service_type = typename backend_type::tcp_acceptor_service_type; | |||
| 62 | ||||
| 63 | 5x | impl_type& get_impl() noexcept | ||
| 64 | { | |||
| 65 | 5x | return *static_cast<impl_type*>(h_.get()); | ||
| 66 | } | |||
| 67 | ||||
| 68 | struct native_accept_awaitable | |||
| 69 | { | |||
| 70 | native_tcp_acceptor& acc_; | |||
| 71 | tcp_socket& peer_; | |||
| 72 | std::stop_token token_; | |||
| 73 | mutable std::error_code ec_; | |||
| 74 | mutable io_object::implementation* peer_impl_ = nullptr; | |||
| 75 | ||||
| 76 | 5x | native_accept_awaitable( | ||
| 77 | native_tcp_acceptor& acc, tcp_socket& peer) noexcept | |||
| 78 | 5x | : acc_(acc) | ||
| 79 | 5x | , peer_(peer) | ||
| 80 | { | |||
| 81 | 5x | } | ||
| 82 | ||||
| 83 | 5x | bool await_ready() const noexcept | ||
| 84 | { | |||
| 85 | 5x | return token_.stop_requested(); | ||
| 86 | } | |||
| 87 | ||||
| 88 | 5x | capy::io_result<> await_resume() const noexcept | ||
| 89 | { | |||
| 90 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 5 times.
|
5x | if (token_.stop_requested()) | |
| 91 | ✗ | return {make_error_code(std::errc::operation_canceled)}; | ||
| 92 |
1/2✓ Branch 6 → 7 taken 5 times.
✗ Branch 6 → 8 not taken.
|
5x | if (!ec_) | |
| 93 | 5x | acc_.reset_peer_impl(peer_, peer_impl_); | ||
| 94 | 5x | return {ec_}; | ||
| 95 | } | |||
| 96 | ||||
| 97 | 5x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | ||
| 98 | -> std::coroutine_handle<> | |||
| 99 | { | |||
| 100 | 5x | token_ = env->stop_token; | ||
| 101 |
1/1✓ Branch 5 → 6 taken 5 times.
|
15x | return acc_.get_impl().accept( | |
| 102 | 15x | h, env->executor, token_, &ec_, &peer_impl_); | ||
| 103 | } | |||
| 104 | }; | |||
| 105 | ||||
| 106 | public: | |||
| 107 | /** Construct a native acceptor from an execution context. | |||
| 108 | ||||
| 109 | @param ctx The execution context that will own this acceptor. | |||
| 110 | */ | |||
| 111 | 8x | explicit native_tcp_acceptor(capy::execution_context& ctx) | ||
| 112 |
1/1✓ Branch 2 → 3 taken 8 times.
|
8x | : tcp_acceptor(create_handle<service_type>(ctx)) | |
| 113 | { | |||
| 114 | 8x | } | ||
| 115 | ||||
| 116 | /** Construct a native acceptor from an executor. | |||
| 117 | ||||
| 118 | @param ex The executor whose context will own the acceptor. | |||
| 119 | */ | |||
| 120 | template<class Ex> | |||
| 121 | requires(!std::same_as<std::remove_cvref_t<Ex>, native_tcp_acceptor>) && | |||
| 122 | capy::Executor<Ex> | |||
| 123 | explicit native_tcp_acceptor(Ex const& ex) | |||
| 124 | : native_tcp_acceptor(ex.context()) | |||
| 125 | { | |||
| 126 | } | |||
| 127 | ||||
| 128 | /** Move construct. | |||
| 129 | ||||
| 130 | @param other The acceptor to move from. | |||
| 131 | ||||
| 132 | @pre No awaitables returned by @p other's methods exist. | |||
| 133 | @pre The execution context associated with @p other must | |||
| 134 | outlive this acceptor. | |||
| 135 | */ | |||
| 136 | 1x | native_tcp_acceptor(native_tcp_acceptor&&) noexcept = default; | ||
| 137 | ||||
| 138 | /** Move assign. | |||
| 139 | ||||
| 140 | @param other The acceptor to move from. | |||
| 141 | ||||
| 142 | @pre No awaitables returned by either `*this` or @p other's | |||
| 143 | methods exist. | |||
| 144 | @pre The execution context associated with @p other must | |||
| 145 | outlive this acceptor. | |||
| 146 | */ | |||
| 147 | native_tcp_acceptor& operator=(native_tcp_acceptor&&) noexcept = default; | |||
| 148 | ||||
| 149 | native_tcp_acceptor(native_tcp_acceptor const&) = delete; | |||
| 150 | native_tcp_acceptor& operator=(native_tcp_acceptor const&) = delete; | |||
| 151 | ||||
| 152 | /** Asynchronously accept an incoming connection. | |||
| 153 | ||||
| 154 | Calls the backend implementation directly, bypassing virtual | |||
| 155 | dispatch. Otherwise identical to @ref tcp_acceptor::accept. | |||
| 156 | ||||
| 157 | @param peer The socket to receive the accepted connection. | |||
| 158 | ||||
| 159 | @return An awaitable yielding `io_result<>`. | |||
| 160 | ||||
| 161 | @throws std::logic_error if the acceptor is not listening. | |||
| 162 | ||||
| 163 | Both this acceptor and @p peer must outlive the returned | |||
| 164 | awaitable. | |||
| 165 | */ | |||
| 166 | 5x | auto accept(tcp_socket& peer) | ||
| 167 | { | |||
| 168 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 5 times.
|
5x | if (!is_open()) | |
| 169 | ✗ | detail::throw_logic_error("accept: acceptor not listening"); | ||
| 170 | 5x | return native_accept_awaitable(*this, peer); | ||
| 171 | } | |||
| 172 | }; | |||
| 173 | ||||
| 174 | } // namespace boost::corosio | |||
| 175 | ||||
| 176 | #endif | |||
| 177 |