include/boost/corosio/native/native_tcp_acceptor.hpp

90.0% Lines (45/50) 100.0% List of functions (36/36) 41.7% Branches (20/48)
native_tcp_acceptor.hpp
f(x) Functions (36)
Function Calls Lines Branches Blocks
boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::~native_tcp_acceptor() :57 24x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::~native_tcp_acceptor() :57 24x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::get_impl() :63 7x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::get_impl() :63 7x 0.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&&) :68 2x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::~native_wait_awaitable() :68 4x 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&&) :68 2x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::~native_wait_awaitable() :68 4x 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) :75 2x 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) :75 2x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::await_ready() const :81 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const :81 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_wait_awaitable::await_resume() const :86 1x 80.0% 33.3% 57.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const :86 1x 100.0% 57.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*) :93 1x 80.0% 50.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*) :93 1x 100.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&&) :102 12x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::~native_accept_awaitable() :102 24x 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&&) :102 12x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::~native_accept_awaitable() :102 24x 0.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&) :110 12x 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&) :110 12x 0.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::await_ready() const :117 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_ready() const :117 6x 0.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_accept_awaitable::await_resume() const :122 6x 85.7% 37.5% 66.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_resume() const :122 6x 0.0% 0.0% 66.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*) :131 6x 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*) :131 6x 0.0% 0.0% 50.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_tcp_acceptor(boost::capy::execution_context&) :145 22x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_tcp_acceptor(boost::capy::execution_context&) :145 22x 0.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::native_tcp_acceptor(boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>&&) :170 2x 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{}>&&) :170 2x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::accept(boost::corosio::tcp_socket&) :200 6x 75.0% 50.0% 66.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::accept(boost::corosio::tcp_socket&) :200 6x 0.0% 0.0% 66.0% boost::corosio::native_tcp_acceptor<boost::corosio::kqueue_t{}>::wait(boost::corosio::wait_type) :216 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::wait(boost::corosio::wait_type) :216 1x 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 #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 14x impl_type& get_impl() noexcept
64 {
65 14x return *static_cast<impl_type*>(h_.get());
66 }
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 6x native_wait_awaitable(native_tcp_acceptor& acc, wait_type w) noexcept
76 2x : acc_(acc)
77 2x , w_(w)
78 2x {
79 4x }
80
81 2x bool await_ready() const noexcept
82 {
83 2x return token_.stop_requested();
84 }
85
86 2x capy::io_result<> await_resume() const noexcept
87 {
88
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
2x if (token_.stop_requested())
89 return {make_error_code(std::errc::operation_canceled)};
90
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2x return {ec_};
91 2x }
92
93 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
94 -> std::coroutine_handle<>
95 {
96 2x token_ = env->stop_token;
97
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
4x return acc_.get_impl().wait(
98 2x h, env->executor, w_, token_, &ec_);
99 }
100 };
101
102 struct native_accept_awaitable
103 {
104 native_tcp_acceptor& acc_;
105 tcp_socket& peer_;
106 std::stop_token token_;
107 mutable std::error_code ec_;
108 12x mutable io_object::implementation* peer_impl_ = nullptr;
109
110 36x native_accept_awaitable(
111 native_tcp_acceptor& acc, tcp_socket& peer) noexcept
112 12x : acc_(acc)
113 12x , peer_(peer)
114 12x {
115 24x }
116
117 12x bool await_ready() const noexcept
118 {
119 12x return token_.stop_requested();
120 }
121
122 12x capy::io_result<> await_resume() const noexcept
123 {
124
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
12x if (token_.stop_requested())
125 return {make_error_code(std::errc::operation_canceled)};
126
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
12x if (!ec_)
127 12x acc_.reset_peer_impl(peer_, peer_impl_);
128
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12x return {ec_};
129 12x }
130
131 12x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
132 -> std::coroutine_handle<>
133 {
134 12x token_ = env->stop_token;
135
4/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
24x return acc_.get_impl().accept(
136 12x h, env->executor, token_, &ec_, &peer_impl_);
137 }
138 };
139
140 public:
141 /** Construct a native acceptor from an execution context.
142
143 @param ctx The execution context that will own this acceptor.
144 */
145 44x explicit native_tcp_acceptor(capy::execution_context& ctx)
146 22x : tcp_acceptor(create_handle<service_type>(ctx))
147 44x {
148 44x }
149
150 /** Construct a native acceptor from an executor.
151
152 @param ex The executor whose context will own the acceptor.
153 */
154 template<class Ex>
155 requires(!std::same_as<std::remove_cvref_t<Ex>, native_tcp_acceptor>) &&
156 capy::Executor<Ex>
157 explicit native_tcp_acceptor(Ex const& ex)
158 : native_tcp_acceptor(ex.context())
159 {
160 }
161
162 /** Move construct.
163
164 @param other The acceptor to move from.
165
166 @pre No awaitables returned by @p other's methods exist.
167 @pre The execution context associated with @p other must
168 outlive this acceptor.
169 */
170 4x native_tcp_acceptor(native_tcp_acceptor&&) noexcept = default;
171
172 /** Move assign.
173
174 @param other The acceptor to move from.
175
176 @pre No awaitables returned by either `*this` or @p other's
177 methods exist.
178 @pre The execution context associated with @p other must
179 outlive this acceptor.
180 */
181 native_tcp_acceptor& operator=(native_tcp_acceptor&&) noexcept = default;
182
183 native_tcp_acceptor(native_tcp_acceptor const&) = delete;
184 native_tcp_acceptor& operator=(native_tcp_acceptor const&) = delete;
185
186 /** Asynchronously accept an incoming connection.
187
188 Calls the backend implementation directly, bypassing virtual
189 dispatch. Otherwise identical to @ref tcp_acceptor::accept.
190
191 @param peer The socket to receive the accepted connection.
192
193 @return An awaitable yielding `io_result<>`.
194
195 @throws std::logic_error if the acceptor is not listening.
196
197 Both this acceptor and @p peer must outlive the returned
198 awaitable.
199 */
200 12x auto accept(tcp_socket& peer)
201 {
202
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12x if (!is_open())
203 detail::throw_logic_error("accept: acceptor not listening");
204 12x 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 2x [[nodiscard]] auto wait(wait_type w)
217 {
218 2x return native_wait_awaitable(*this, w);
219 }
220 };
221
222 } // namespace boost::corosio
223
224 #endif
225