include/boost/corosio/native/native_tcp_acceptor.hpp

90.5% Lines (57/63) 100.0% List of functions (32/32)
native_tcp_acceptor.hpp
f(x) Functions (32)
Function Calls Lines Blocks
boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::get_impl() :67 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::get_impl() :67 6x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>&, boost::corosio::wait_type) :79 1x 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) :79 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::await_ready() const :85 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const :85 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::await_resume() const :90 1x 75.0% 67.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const :90 1x 75.0% 67.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :97 1x 100.0% 82.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :97 1x 100.0% 82.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>&, boost::corosio::tcp_socket&) :114 4x 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&) :114 4x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_ready() const :121 4x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_ready() const :121 4x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_resume() const :126 4x 83.3% 78.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_resume() const :126 4x 83.3% 78.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :135 4x 100.0% 82.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :135 4x 100.0% 82.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_accept_value_awaitable::native_accept_value_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>&) :152 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::native_accept_value_awaitable(boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>&) :152 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_accept_value_awaitable::await_ready() const :158 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::await_ready() const :158 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_accept_value_awaitable::await_resume() :163 1x 71.4% 67.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::await_resume() :163 1x 71.4% 67.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_accept_value_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :173 1x 100.0% 82.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_accept_value_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :173 1x 100.0% 82.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_tcp_acceptor(boost::capy::execution_context&) :187 10x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::native_tcp_acceptor(boost::capy::execution_context&) :187 10x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::native_tcp_acceptor(boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>&&) :212 1x 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{}>&&) :212 1x 100.0% 100.0% boost::corosio::native_tcp_acceptor<boost::corosio::epoll_t{}>::accept() :260 1x 75.0% 80.0% boost::corosio::native_tcp_acceptor<boost::corosio::select_t{}>::accept() :260 1x 75.0% 80.0%
Line 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
33 #if BOOST_COROSIO_HAS_IO_URING
34 #include <boost/corosio/native/detail/io_uring/io_uring_types.hpp>
35 #endif
36 #endif // !BOOST_COROSIO_MRDOCS
37
38 namespace boost::corosio {
39
40 /** An asynchronous TCP acceptor with devirtualized accept operations.
41
42 This class template inherits from @ref tcp_acceptor and shadows
43 the `accept` operation with a version that calls the backend
44 implementation directly, allowing the compiler to inline through
45 the entire call chain.
46
47 Non-async operations (`listen`, `close`, `cancel`) remain
48 unchanged and dispatch through the compiled library.
49
50 A `native_tcp_acceptor` IS-A `tcp_acceptor` and can be passed
51 to any function expecting `tcp_acceptor&`.
52
53 @tparam Backend A backend tag value (e.g., `epoll`).
54
55 @par Thread Safety
56 Same as @ref tcp_acceptor.
57
58 @see tcp_acceptor, epoll_t, iocp_t
59 */
60 template<auto Backend>
61 class native_tcp_acceptor : public tcp_acceptor
62 {
63 using backend_type = decltype(Backend);
64 using impl_type = typename backend_type::tcp_acceptor_type;
65 using service_type = typename backend_type::tcp_acceptor_service_type;
66
67 12x impl_type& get_impl() noexcept
68 {
69 12x return *static_cast<impl_type*>(h_.get());
70 }
71
72 struct native_wait_awaitable
73 {
74 native_tcp_acceptor& acc_;
75 wait_type w_;
76 std::stop_token token_;
77 mutable std::error_code ec_;
78
79 2x native_wait_awaitable(native_tcp_acceptor& acc, wait_type w) noexcept
80 2x : acc_(acc)
81 2x , w_(w)
82 {
83 2x }
84
85 2x bool await_ready() const noexcept
86 {
87 2x return token_.stop_requested();
88 }
89
90 2x capy::io_result<> await_resume() const noexcept
91 {
92 2x if (token_.stop_requested())
93 return {make_error_code(std::errc::operation_canceled)};
94 2x return {ec_};
95 }
96
97 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
98 -> std::coroutine_handle<>
99 {
100 2x token_ = env->stop_token;
101 6x return acc_.get_impl().wait(
102 6x h, env->executor, w_, token_, &ec_);
103 }
104 };
105
106 struct native_accept_awaitable
107 {
108 native_tcp_acceptor& acc_;
109 tcp_socket& peer_;
110 std::stop_token token_;
111 mutable std::error_code ec_;
112 mutable io_object::implementation* peer_impl_ = nullptr;
113
114 8x native_accept_awaitable(
115 native_tcp_acceptor& acc, tcp_socket& peer) noexcept
116 8x : acc_(acc)
117 8x , peer_(peer)
118 {
119 8x }
120
121 8x bool await_ready() const noexcept
122 {
123 8x return token_.stop_requested();
124 }
125
126 8x capy::io_result<> await_resume() const noexcept
127 {
128 8x if (token_.stop_requested())
129 return {make_error_code(std::errc::operation_canceled)};
130 8x if (!ec_)
131 8x acc_.reset_peer_impl(peer_, peer_impl_);
132 8x return {ec_};
133 }
134
135 8x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
136 -> std::coroutine_handle<>
137 {
138 8x token_ = env->stop_token;
139 24x return acc_.get_impl().accept(
140 24x h, env->executor, token_, &ec_, &peer_impl_);
141 }
142 };
143
144 struct native_accept_value_awaitable
145 {
146 native_tcp_acceptor& acc_;
147 tcp_socket peer_;
148 std::stop_token token_;
149 mutable std::error_code ec_;
150 mutable io_object::implementation* peer_impl_ = nullptr;
151
152 2x explicit native_accept_value_awaitable(native_tcp_acceptor& acc)
153 2x : acc_(acc)
154 2x , peer_(acc.context())
155 {
156 2x }
157
158 2x bool await_ready() const noexcept
159 {
160 2x return token_.stop_requested();
161 }
162
163 2x capy::io_result<tcp_socket> await_resume() noexcept
164 {
165 2x if (token_.stop_requested())
166 return {make_error_code(std::errc::operation_canceled),
167 std::move(peer_)};
168 2x if (!ec_ && peer_impl_)
169 2x acc_.reset_peer_impl(peer_, peer_impl_);
170 2x return {ec_, std::move(peer_)};
171 }
172
173 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
174 -> std::coroutine_handle<>
175 {
176 2x token_ = env->stop_token;
177 6x return acc_.get_impl().accept(
178 6x h, env->executor, token_, &ec_, &peer_impl_);
179 }
180 };
181
182 public:
183 /** Construct a native acceptor from an execution context.
184
185 @param ctx The execution context that will own this acceptor.
186 */
187 20x explicit native_tcp_acceptor(capy::execution_context& ctx)
188 20x : tcp_acceptor(create_handle<service_type>(ctx))
189 {
190 20x }
191
192 /** Construct a native acceptor from an executor.
193
194 @param ex The executor whose context will own the acceptor.
195 */
196 template<class Ex>
197 requires(!std::same_as<std::remove_cvref_t<Ex>, native_tcp_acceptor>) &&
198 capy::Executor<Ex>
199 explicit native_tcp_acceptor(Ex const& ex)
200 : native_tcp_acceptor(ex.context())
201 {
202 }
203
204 /** Move construct.
205
206 @param other The acceptor to move from.
207
208 @pre No awaitables returned by @p other's methods exist.
209 @pre The execution context associated with @p other must
210 outlive this acceptor.
211 */
212 2x native_tcp_acceptor(native_tcp_acceptor&&) noexcept = default;
213
214 /** Move assign.
215
216 @param other The acceptor to move from.
217
218 @pre No awaitables returned by either `*this` or @p other's
219 methods exist.
220 @pre The execution context associated with @p other must
221 outlive this acceptor.
222 */
223 native_tcp_acceptor& operator=(native_tcp_acceptor&&) noexcept = default;
224
225 native_tcp_acceptor(native_tcp_acceptor const&) = delete;
226 native_tcp_acceptor& operator=(native_tcp_acceptor const&) = delete;
227
228 /** Asynchronously accept an incoming connection.
229
230 Calls the backend implementation directly, bypassing virtual
231 dispatch. Otherwise identical to @ref tcp_acceptor::accept.
232
233 @param peer The socket to receive the accepted connection.
234
235 @return An awaitable yielding `io_result<>`.
236
237 @throws std::logic_error if the acceptor is not listening.
238
239 Both this acceptor and @p peer must outlive the returned
240 awaitable.
241 */
242 8x auto accept(tcp_socket& peer)
243 {
244 8x if (!is_open())
245 detail::throw_logic_error("accept: acceptor not listening");
246 8x return native_accept_awaitable(*this, peer);
247 }
248
249 /** Asynchronously accept an incoming connection, returning the peer.
250
251 Calls the backend implementation directly, bypassing virtual
252 dispatch. Otherwise identical to @ref tcp_acceptor::accept().
253
254 @return An awaitable yielding `io_result<tcp_socket>`.
255
256 @throws std::logic_error if the acceptor is not listening.
257
258 This acceptor must outlive the returned awaitable.
259 */
260 2x auto accept()
261 {
262 2x if (!is_open())
263 detail::throw_logic_error("accept: acceptor not listening");
264 2x return native_accept_value_awaitable(*this);
265 }
266
267 /** Asynchronously wait for the acceptor to be ready.
268
269 Calls the backend implementation directly, bypassing virtual
270 dispatch. Otherwise identical to @ref tcp_acceptor::wait.
271
272 @param w The wait direction (typically `wait_type::read`).
273
274 @return An awaitable yielding `io_result<>`.
275 */
276 2x [[nodiscard]] auto wait(wait_type w)
277 {
278 2x return native_wait_awaitable(*this, w);
279 }
280 };
281
282 } // namespace boost::corosio
283
284 #endif
285