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