include/boost/corosio/native/native_tcp_acceptor.hpp

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