92.00% Lines (23/25)
100.00% Functions (7/7)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2026 Steve Gerbino | 2 | // Copyright (c) 2026 Steve Gerbino | |||||
| 3 | // | 3 | // | |||||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 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) | 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 6 | // | 6 | // | |||||
| 7 | // Official repository: https://github.com/cppalliance/corosio | 7 | // Official repository: https://github.com/cppalliance/corosio | |||||
| 8 | // | 8 | // | |||||
| 9 | 9 | |||||||
| 10 | #ifndef BOOST_COROSIO_NATIVE_NATIVE_TCP_ACCEPTOR_HPP | 10 | #ifndef BOOST_COROSIO_NATIVE_NATIVE_TCP_ACCEPTOR_HPP | |||||
| 11 | #define BOOST_COROSIO_NATIVE_NATIVE_TCP_ACCEPTOR_HPP | 11 | #define BOOST_COROSIO_NATIVE_NATIVE_TCP_ACCEPTOR_HPP | |||||
| 12 | 12 | |||||||
| 13 | #include <boost/corosio/tcp_acceptor.hpp> | 13 | #include <boost/corosio/tcp_acceptor.hpp> | |||||
| 14 | #include <boost/corosio/backend.hpp> | 14 | #include <boost/corosio/backend.hpp> | |||||
| 15 | 15 | |||||||
| 16 | #ifndef BOOST_COROSIO_MRDOCS | 16 | #ifndef BOOST_COROSIO_MRDOCS | |||||
| 17 | #if BOOST_COROSIO_HAS_EPOLL | 17 | #if BOOST_COROSIO_HAS_EPOLL | |||||
| 18 | #include <boost/corosio/native/detail/epoll/epoll_types.hpp> | 18 | #include <boost/corosio/native/detail/epoll/epoll_types.hpp> | |||||
| 19 | #endif | 19 | #endif | |||||
| 20 | 20 | |||||||
| 21 | #if BOOST_COROSIO_HAS_SELECT | 21 | #if BOOST_COROSIO_HAS_SELECT | |||||
| 22 | #include <boost/corosio/native/detail/select/select_types.hpp> | 22 | #include <boost/corosio/native/detail/select/select_types.hpp> | |||||
| 23 | #endif | 23 | #endif | |||||
| 24 | 24 | |||||||
| 25 | #if BOOST_COROSIO_HAS_KQUEUE | 25 | #if BOOST_COROSIO_HAS_KQUEUE | |||||
| 26 | #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp> | 26 | #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp> | |||||
| 27 | #endif | 27 | #endif | |||||
| 28 | 28 | |||||||
| 29 | #if BOOST_COROSIO_HAS_IOCP | 29 | #if BOOST_COROSIO_HAS_IOCP | |||||
| 30 | #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp> | 30 | #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp> | |||||
| 31 | #endif | 31 | #endif | |||||
| 32 | #endif // !BOOST_COROSIO_MRDOCS | 32 | #endif // !BOOST_COROSIO_MRDOCS | |||||
| 33 | 33 | |||||||
| 34 | namespace boost::corosio { | 34 | namespace boost::corosio { | |||||
| 35 | 35 | |||||||
| 36 | /** An asynchronous TCP acceptor with devirtualized accept operations. | 36 | /** An asynchronous TCP acceptor with devirtualized accept operations. | |||||
| 37 | 37 | |||||||
| 38 | This class template inherits from @ref tcp_acceptor and shadows | 38 | This class template inherits from @ref tcp_acceptor and shadows | |||||
| 39 | the `accept` operation with a version that calls the backend | 39 | the `accept` operation with a version that calls the backend | |||||
| 40 | implementation directly, allowing the compiler to inline through | 40 | implementation directly, allowing the compiler to inline through | |||||
| 41 | the entire call chain. | 41 | the entire call chain. | |||||
| 42 | 42 | |||||||
| 43 | Non-async operations (`listen`, `close`, `cancel`) remain | 43 | Non-async operations (`listen`, `close`, `cancel`) remain | |||||
| 44 | unchanged and dispatch through the compiled library. | 44 | unchanged and dispatch through the compiled library. | |||||
| 45 | 45 | |||||||
| 46 | A `native_tcp_acceptor` IS-A `tcp_acceptor` and can be passed | 46 | A `native_tcp_acceptor` IS-A `tcp_acceptor` and can be passed | |||||
| 47 | to any function expecting `tcp_acceptor&`. | 47 | to any function expecting `tcp_acceptor&`. | |||||
| 48 | 48 | |||||||
| 49 | @tparam Backend A backend tag value (e.g., `epoll`). | 49 | @tparam Backend A backend tag value (e.g., `epoll`). | |||||
| 50 | 50 | |||||||
| 51 | @par Thread Safety | 51 | @par Thread Safety | |||||
| 52 | Same as @ref tcp_acceptor. | 52 | Same as @ref tcp_acceptor. | |||||
| 53 | 53 | |||||||
| 54 | @see tcp_acceptor, epoll_t, iocp_t | 54 | @see tcp_acceptor, epoll_t, iocp_t | |||||
| 55 | */ | 55 | */ | |||||
| 56 | template<auto Backend> | 56 | template<auto Backend> | |||||
| 57 | class native_tcp_acceptor : public tcp_acceptor | 57 | class native_tcp_acceptor : public tcp_acceptor | |||||
| 58 | { | 58 | { | |||||
| 59 | using backend_type = decltype(Backend); | 59 | using backend_type = decltype(Backend); | |||||
| 60 | using impl_type = typename backend_type::tcp_acceptor_type; | 60 | using impl_type = typename backend_type::tcp_acceptor_type; | |||||
| 61 | using service_type = typename backend_type::tcp_acceptor_service_type; | 61 | using service_type = typename backend_type::tcp_acceptor_service_type; | |||||
| 62 | 62 | |||||||
| HITCBC | 63 | 4 | impl_type& get_impl() noexcept | 63 | 4 | impl_type& get_impl() noexcept | ||
| 64 | { | 64 | { | |||||
| HITCBC | 65 | 4 | return *static_cast<impl_type*>(h_.get()); | 65 | 4 | return *static_cast<impl_type*>(h_.get()); | ||
| 66 | } | 66 | } | |||||
| 67 | 67 | |||||||
| 68 | + | struct native_wait_awaitable | ||||||
| 69 | + | { | ||||||
| 70 | + | native_tcp_acceptor& acc_; | ||||||
| 71 | + | wait_type w_; | ||||||
| 72 | + | std::stop_token token_; | ||||||
| 73 | + | mutable std::error_code ec_; | ||||||
| 74 | + | |||||||
| 75 | + | native_wait_awaitable(native_tcp_acceptor& acc, wait_type w) noexcept | ||||||
| 76 | + | : acc_(acc) | ||||||
| 77 | + | , w_(w) | ||||||
| 78 | + | { | ||||||
| 79 | + | } | ||||||
| 80 | + | |||||||
| 81 | + | bool await_ready() const noexcept | ||||||
| 82 | + | { | ||||||
| 83 | + | return token_.stop_requested(); | ||||||
| 84 | + | } | ||||||
| 85 | + | |||||||
| 86 | + | capy::io_result<> await_resume() const noexcept | ||||||
| 87 | + | { | ||||||
| 88 | + | if (token_.stop_requested()) | ||||||
| 89 | + | return {make_error_code(std::errc::operation_canceled)}; | ||||||
| 90 | + | return {ec_}; | ||||||
| 91 | + | } | ||||||
| 92 | + | |||||||
| 93 | + | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | ||||||
| 94 | + | -> std::coroutine_handle<> | ||||||
| 95 | + | { | ||||||
| 96 | + | token_ = env->stop_token; | ||||||
| 97 | + | return acc_.get_impl().wait( | ||||||
| 98 | + | h, env->executor, w_, token_, &ec_); | ||||||
| 99 | + | } | ||||||
| 100 | + | }; | ||||||
| 101 | + | |||||||
| 68 | struct native_accept_awaitable | 102 | struct native_accept_awaitable | |||||
| 69 | { | 103 | { | |||||
| 70 | native_tcp_acceptor& acc_; | 104 | native_tcp_acceptor& acc_; | |||||
| 71 | tcp_socket& peer_; | 105 | tcp_socket& peer_; | |||||
| 72 | std::stop_token token_; | 106 | std::stop_token token_; | |||||
| 73 | mutable std::error_code ec_; | 107 | mutable std::error_code ec_; | |||||
| 74 | mutable io_object::implementation* peer_impl_ = nullptr; | 108 | mutable io_object::implementation* peer_impl_ = nullptr; | |||||
| 75 | 109 | |||||||
| HITCBC | 76 | 4 | native_accept_awaitable( | 110 | 4 | native_accept_awaitable( | ||
| 77 | native_tcp_acceptor& acc, tcp_socket& peer) noexcept | 111 | native_tcp_acceptor& acc, tcp_socket& peer) noexcept | |||||
| HITCBC | 78 | 4 | : acc_(acc) | 112 | 4 | : acc_(acc) | ||
| HITCBC | 79 | 4 | , peer_(peer) | 113 | 4 | , peer_(peer) | ||
| 80 | { | 114 | { | |||||
| HITCBC | 81 | 4 | } | 115 | 4 | } | ||
| 82 | 116 | |||||||
| HITCBC | 83 | 4 | bool await_ready() const noexcept | 117 | 4 | bool await_ready() const noexcept | ||
| 84 | { | 118 | { | |||||
| HITCBC | 85 | 4 | return token_.stop_requested(); | 119 | 4 | return token_.stop_requested(); | ||
| 86 | } | 120 | } | |||||
| 87 | 121 | |||||||
| HITCBC | 88 | 4 | capy::io_result<> await_resume() const noexcept | 122 | 4 | capy::io_result<> await_resume() const noexcept | ||
| 89 | { | 123 | { | |||||
| HITCBC | 90 | 4 | if (token_.stop_requested()) | 124 | 4 | if (token_.stop_requested()) | ||
| MISUBC | 91 | ✗ | return {make_error_code(std::errc::operation_canceled)}; | 125 | ✗ | return {make_error_code(std::errc::operation_canceled)}; | ||
| HITCBC | 92 | 4 | if (!ec_) | 126 | 4 | if (!ec_) | ||
| HITCBC | 93 | 4 | acc_.reset_peer_impl(peer_, peer_impl_); | 127 | 4 | acc_.reset_peer_impl(peer_, peer_impl_); | ||
| HITCBC | 94 | 4 | return {ec_}; | 128 | 4 | return {ec_}; | ||
| 95 | } | 129 | } | |||||
| 96 | 130 | |||||||
| HITCBC | 97 | 4 | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | 131 | 4 | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | ||
| 98 | -> std::coroutine_handle<> | 132 | -> std::coroutine_handle<> | |||||
| 99 | { | 133 | { | |||||
| HITCBC | 100 | 4 | token_ = env->stop_token; | 134 | 4 | token_ = env->stop_token; | ||
| HITCBC | 101 | 12 | return acc_.get_impl().accept( | 135 | 12 | return acc_.get_impl().accept( | ||
| HITCBC | 102 | 12 | h, env->executor, token_, &ec_, &peer_impl_); | 136 | 12 | h, env->executor, token_, &ec_, &peer_impl_); | ||
| 103 | } | 137 | } | |||||
| 104 | }; | 138 | }; | |||||
| 105 | 139 | |||||||
| 106 | public: | 140 | public: | |||||
| 107 | /** Construct a native acceptor from an execution context. | 141 | /** Construct a native acceptor from an execution context. | |||||
| 108 | 142 | |||||||
| 109 | @param ctx The execution context that will own this acceptor. | 143 | @param ctx The execution context that will own this acceptor. | |||||
| 110 | */ | 144 | */ | |||||
| HITCBC | 111 | 4 | explicit native_tcp_acceptor(capy::execution_context& ctx) | 145 | 4 | explicit native_tcp_acceptor(capy::execution_context& ctx) | ||
| HITCBC | 112 | 4 | : tcp_acceptor(create_handle<service_type>(ctx)) | 146 | 4 | : tcp_acceptor(create_handle<service_type>(ctx)) | ||
| 113 | { | 147 | { | |||||
| HITCBC | 114 | 4 | } | 148 | 4 | } | ||
| 115 | 149 | |||||||
| 116 | /** Construct a native acceptor from an executor. | 150 | /** Construct a native acceptor from an executor. | |||||
| 117 | 151 | |||||||
| 118 | @param ex The executor whose context will own the acceptor. | 152 | @param ex The executor whose context will own the acceptor. | |||||
| 119 | */ | 153 | */ | |||||
| 120 | template<class Ex> | 154 | template<class Ex> | |||||
| 121 | requires(!std::same_as<std::remove_cvref_t<Ex>, native_tcp_acceptor>) && | 155 | requires(!std::same_as<std::remove_cvref_t<Ex>, native_tcp_acceptor>) && | |||||
| 122 | capy::Executor<Ex> | 156 | capy::Executor<Ex> | |||||
| 123 | explicit native_tcp_acceptor(Ex const& ex) | 157 | explicit native_tcp_acceptor(Ex const& ex) | |||||
| 124 | : native_tcp_acceptor(ex.context()) | 158 | : native_tcp_acceptor(ex.context()) | |||||
| 125 | { | 159 | { | |||||
| 126 | } | 160 | } | |||||
| 127 | 161 | |||||||
| 128 | /** Move construct. | 162 | /** Move construct. | |||||
| 129 | 163 | |||||||
| 130 | @param other The acceptor to move from. | 164 | @param other The acceptor to move from. | |||||
| 131 | 165 | |||||||
| 132 | @pre No awaitables returned by @p other's methods exist. | 166 | @pre No awaitables returned by @p other's methods exist. | |||||
| 133 | @pre The execution context associated with @p other must | 167 | @pre The execution context associated with @p other must | |||||
| 134 | outlive this acceptor. | 168 | outlive this acceptor. | |||||
| 135 | */ | 169 | */ | |||||
| 136 | native_tcp_acceptor(native_tcp_acceptor&&) noexcept = default; | 170 | native_tcp_acceptor(native_tcp_acceptor&&) noexcept = default; | |||||
| 137 | 171 | |||||||
| 138 | /** Move assign. | 172 | /** Move assign. | |||||
| 139 | 173 | |||||||
| 140 | @param other The acceptor to move from. | 174 | @param other The acceptor to move from. | |||||
| 141 | 175 | |||||||
| 142 | @pre No awaitables returned by either `*this` or @p other's | 176 | @pre No awaitables returned by either `*this` or @p other's | |||||
| 143 | methods exist. | 177 | methods exist. | |||||
| 144 | @pre The execution context associated with @p other must | 178 | @pre The execution context associated with @p other must | |||||
| 145 | outlive this acceptor. | 179 | outlive this acceptor. | |||||
| 146 | */ | 180 | */ | |||||
| 147 | native_tcp_acceptor& operator=(native_tcp_acceptor&&) noexcept = default; | 181 | native_tcp_acceptor& operator=(native_tcp_acceptor&&) noexcept = default; | |||||
| 148 | 182 | |||||||
| 149 | native_tcp_acceptor(native_tcp_acceptor const&) = delete; | 183 | native_tcp_acceptor(native_tcp_acceptor const&) = delete; | |||||
| 150 | native_tcp_acceptor& operator=(native_tcp_acceptor const&) = delete; | 184 | native_tcp_acceptor& operator=(native_tcp_acceptor const&) = delete; | |||||
| 151 | 185 | |||||||
| 152 | /** Asynchronously accept an incoming connection. | 186 | /** Asynchronously accept an incoming connection. | |||||
| 153 | 187 | |||||||
| 154 | Calls the backend implementation directly, bypassing virtual | 188 | Calls the backend implementation directly, bypassing virtual | |||||
| 155 | dispatch. Otherwise identical to @ref tcp_acceptor::accept. | 189 | dispatch. Otherwise identical to @ref tcp_acceptor::accept. | |||||
| 156 | 190 | |||||||
| 157 | @param peer The socket to receive the accepted connection. | 191 | @param peer The socket to receive the accepted connection. | |||||
| 158 | 192 | |||||||
| 159 | @return An awaitable yielding `io_result<>`. | 193 | @return An awaitable yielding `io_result<>`. | |||||
| 160 | 194 | |||||||
| 161 | @throws std::logic_error if the acceptor is not listening. | 195 | @throws std::logic_error if the acceptor is not listening. | |||||
| 162 | 196 | |||||||
| 163 | Both this acceptor and @p peer must outlive the returned | 197 | Both this acceptor and @p peer must outlive the returned | |||||
| 164 | awaitable. | 198 | awaitable. | |||||
| 165 | */ | 199 | */ | |||||
| HITCBC | 166 | 4 | auto accept(tcp_socket& peer) | 200 | 4 | auto accept(tcp_socket& peer) | ||
| 167 | { | 201 | { | |||||
| HITCBC | 168 | 4 | if (!is_open()) | 202 | 4 | if (!is_open()) | ||
| MISUBC | 169 | ✗ | detail::throw_logic_error("accept: acceptor not listening"); | 203 | ✗ | detail::throw_logic_error("accept: acceptor not listening"); | ||
| HITCBC | 170 | 4 | return native_accept_awaitable(*this, peer); | 204 | 4 | return native_accept_awaitable(*this, peer); | ||
| 205 | + | } | ||||||
| 206 | + | |||||||
| 207 | + | /** Asynchronously wait for the acceptor to be ready. | ||||||
| 208 | + | |||||||
| 209 | + | Calls the backend implementation directly, bypassing virtual | ||||||
| 210 | + | dispatch. Otherwise identical to @ref tcp_acceptor::wait. | ||||||
| 211 | + | |||||||
| 212 | + | @param w The wait direction (typically `wait_type::read`). | ||||||
| 213 | + | |||||||
| 214 | + | @return An awaitable yielding `io_result<>`. | ||||||
| 215 | + | */ | ||||||
| 216 | + | [[nodiscard]] auto wait(wait_type w) | ||||||
| 217 | + | { | ||||||
| 218 | + | return native_wait_awaitable(*this, w); | ||||||
| 171 | } | 219 | } | |||||
| 172 | }; | 220 | }; | |||||
| 173 | 221 | |||||||
| 174 | } // namespace boost::corosio | 222 | } // namespace boost::corosio | |||||
| 175 | 223 | |||||||
| 176 | #endif | 224 | #endif | |||||