include/boost/corosio/native/native_local_stream_acceptor.hpp

100.0% Lines (51 / 51) 100.0% Functions (26 / 26)
native_local_stream_acceptor.hpp
f(x) Functions (26)
Function Calls Lines Blocks
boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::get_impl() :74 5x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::get_impl() :74 5x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>&, boost::corosio::wait_type) :84 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>&, boost::corosio::wait_type) :84 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_wait_awaitable::dispatch(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref) const :92 2x 100.0% 80.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_wait_awaitable::dispatch(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref) const :92 2x 100.0% 80.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>&, boost::corosio::local_stream_socket&) :105 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::native_accept_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>&, boost::corosio::local_stream_socket&) :105 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::await_resume() const :113 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::await_resume() const :113 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_accept_awaitable::dispatch(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref) const :121 2x 100.0% 80.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_accept_awaitable::dispatch(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref) const :121 2x 100.0% 80.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::native_move_accept_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>&) :134 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::native_move_accept_awaitable(boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>&) :134 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::await_resume() const :141 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::await_resume() const :141 3x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_move_accept_awaitable::dispatch(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref) const :154 1x 100.0% 80.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_move_accept_awaitable::dispatch(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref) const :154 1x 100.0% 80.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_local_stream_acceptor(boost::capy::execution_context&) :166 9x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_local_stream_acceptor(boost::capy::execution_context&) :166 9x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::native_local_stream_acceptor(boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>&&) :186 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::native_local_stream_acceptor(boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>&&) :186 1x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::accept(boost::corosio::local_stream_socket&) :213 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::accept(boost::corosio::local_stream_socket&) :213 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::epoll_t{}>::accept() :237 4x 100.0% 100.0% boost::corosio::native_local_stream_acceptor<boost::corosio::select_t{}>::accept() :237 4x 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_LOCAL_STREAM_ACCEPTOR_HPP
11 #define BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_ACCEPTOR_HPP
12
13 #include <boost/corosio/local_stream_acceptor.hpp>
14 #include <boost/corosio/native/native_local_stream_socket.hpp>
15 #include <boost/corosio/backend.hpp>
16 #include <boost/corosio/detail/op_base.hpp>
17
18 #ifndef BOOST_COROSIO_MRDOCS
19 #if BOOST_COROSIO_HAS_EPOLL
20 #include <boost/corosio/native/detail/epoll/epoll_types.hpp>
21 #endif
22
23 #if BOOST_COROSIO_HAS_SELECT
24 #include <boost/corosio/native/detail/select/select_types.hpp>
25 #endif
26
27 #if BOOST_COROSIO_HAS_KQUEUE
28 #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp>
29 #endif
30
31 #if BOOST_COROSIO_HAS_URING
32 #include <boost/corosio/native/detail/uring/uring_types.hpp>
33 #endif
34
35 #if BOOST_COROSIO_HAS_IOCP
36 #include <boost/corosio/native/detail/iocp/win_local_stream_acceptor_service.hpp>
37 #endif
38 #endif // !BOOST_COROSIO_MRDOCS
39
40 namespace boost::corosio {
41
42 /** Accepts Unix domain stream connections, calling the backend directly.
43
44 This class template inherits from @ref local_stream_acceptor. It
45 shadows both `accept` overloads (the peer-reference form and the
46 move-return form) with versions that call the backend implementation
47 directly. The compiler can then inline through the entire call
48 chain. The move-return form yields a
49 @ref native_local_stream_socket so subsequent I/O on the peer
50 is also devirtualized.
51
52 Non-async operations (`listen`, `close`, `cancel`) remain
53 unchanged and dispatch through the compiled library.
54
55 A `native_local_stream_acceptor` IS-A `local_stream_acceptor`
56 and can be passed to any function expecting
57 `local_stream_acceptor&`.
58
59 @tparam Backend A backend tag value (e.g., `epoll`).
60
61 @par Thread Safety
62 Same as @ref local_stream_acceptor.
63
64 @see local_stream_acceptor, epoll_t, iocp_t
65 */
66 template<auto Backend>
67 class native_local_stream_acceptor : public local_stream_acceptor
68 {
69 using backend_type = decltype(Backend);
70 using impl_type = typename backend_type::local_stream_acceptor_type;
71 using service_type =
72 typename backend_type::local_stream_acceptor_service_type;
73
74 10x impl_type& get_impl() noexcept
75 {
76 10x return *static_cast<impl_type*>(h_.get());
77 }
78
79 struct native_wait_awaitable : detail::void_op_base<native_wait_awaitable>
80 {
81 native_local_stream_acceptor& acc_;
82 wait_type w_;
83
84 6x native_wait_awaitable(
85 native_local_stream_acceptor& acc, wait_type w) noexcept
86 6x : acc_(acc)
87 6x , w_(w)
88 {
89 6x }
90
91 std::coroutine_handle<>
92 4x dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const
93 {
94 4x return acc_.get_impl().wait(h, ex, w_, this->token_, &this->ec_);
95 }
96 };
97
98 struct native_accept_awaitable
99 : detail::void_op_base<native_accept_awaitable>
100 {
101 native_local_stream_acceptor& acc_;
102 local_stream_socket& peer_;
103 mutable io_object::implementation* peer_impl_ = nullptr;
104
105 8x native_accept_awaitable(
106 native_local_stream_acceptor& acc,
107 local_stream_socket& peer) noexcept
108 8x : acc_(acc)
109 8x , peer_(peer)
110 {
111 8x }
112
113 8x [[nodiscard]] capy::io_result<> await_resume() const noexcept
114 {
115 8x if (!this->ec_)
116 4x acc_.reset_peer_impl(peer_, peer_impl_);
117 8x return {this->ec_};
118 }
119
120 std::coroutine_handle<>
121 4x dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const
122 {
123 12x return acc_.get_impl().accept(
124 12x h, ex, this->token_, &this->ec_, &peer_impl_);
125 }
126 };
127
128 struct native_move_accept_awaitable
129 : detail::void_op_base<native_move_accept_awaitable>
130 {
131 native_local_stream_acceptor& acc_;
132 mutable io_object::implementation* peer_impl_ = nullptr;
133
134 6x explicit native_move_accept_awaitable(
135 native_local_stream_acceptor& acc) noexcept
136 6x : acc_(acc)
137 {
138 6x }
139
140 [[nodiscard]] capy::io_result<native_local_stream_socket<Backend>>
141 6x await_resume() const noexcept
142 {
143 6x if (this->ec_ || !peer_impl_)
144 return {
145 4x this->ec_,
146 4x native_local_stream_socket<Backend>(acc_.context())};
147
148 2x native_local_stream_socket<Backend> peer(acc_.context());
149 2x acc_.reset_peer_impl(peer, peer_impl_);
150 2x return {this->ec_, std::move(peer)};
151 2x }
152
153 std::coroutine_handle<>
154 2x dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const
155 {
156 6x return acc_.get_impl().accept(
157 6x h, ex, this->token_, &this->ec_, &peer_impl_);
158 }
159 };
160
161 public:
162 /** Construct a native acceptor from an execution context.
163
164 @param ctx The execution context that owns this acceptor.
165 */
166 18x explicit native_local_stream_acceptor(capy::execution_context& ctx)
167 18x : local_stream_acceptor(create_handle<service_type>(ctx), ctx)
168 {
169 18x }
170
171 /** Construct a native acceptor from an executor.
172
173 @param ex The executor whose context owns the acceptor.
174 */
175 template<class Ex>
176 requires(!std::same_as<
177 std::remove_cvref_t<Ex>,
178 native_local_stream_acceptor>) &&
179 capy::Executor<Ex>
180 explicit native_local_stream_acceptor(Ex const& ex)
181 : native_local_stream_acceptor(ex.context())
182 {
183 }
184
185 /// Move construct.
186 2x native_local_stream_acceptor(native_local_stream_acceptor&&) noexcept =
187 default;
188
189 /// Move assign.
190 native_local_stream_acceptor&
191 operator=(native_local_stream_acceptor&&) noexcept = default;
192
193 /// Copy construction is disabled; the handle is uniquely owned.
194 native_local_stream_acceptor(native_local_stream_acceptor const&) = delete;
195 /// Copy assignment is disabled; the handle is uniquely owned.
196 native_local_stream_acceptor&
197 operator=(native_local_stream_acceptor const&) = delete;
198
199 /** Asynchronously accept an incoming connection.
200
201 Calls the backend implementation directly, bypassing virtual
202 dispatch. Otherwise identical to @ref local_stream_acceptor::accept.
203
204 @param peer The socket to receive the accepted connection.
205
206 @return An awaitable yielding `io_result<>`.
207
208 A closed acceptor reports `errc::bad_file_descriptor`.
209
210 Both this acceptor and @p peer must outlive the returned
211 awaitable.
212 */
213 8x [[nodiscard]] auto accept(local_stream_socket& peer)
214 {
215 8x native_accept_awaitable aw(*this, peer);
216 8x if (!is_open())
217 2x aw.ec_ = make_error_code(std::errc::bad_file_descriptor);
218 8x return aw;
219 }
220
221 /** Asynchronously accept an incoming connection, returning the peer.
222
223 Calls the backend implementation directly, bypassing virtual
224 dispatch. The accepted peer is returned as a
225 @ref native_local_stream_socket so that subsequent I/O on it
226 is also devirtualized.
227
228 @return An awaitable yielding
229 `io_result<native_local_stream_socket<Backend>>`.
230
231 A closed acceptor reports `errc::bad_file_descriptor`.
232
233 @throws std::logic_error If the acceptor is moved-from.
234
235 This acceptor must outlive the returned awaitable.
236 */
237 8x [[nodiscard]] auto accept()
238 {
239 // The awaitable builds the peer from context(), which a
240 // moved-from acceptor no longer has.
241 8x if (!h_)
242 2x detail::throw_logic_error("accept: acceptor moved-from");
243 6x native_move_accept_awaitable aw(*this);
244 6x if (!is_open())
245 2x aw.ec_ = make_error_code(std::errc::bad_file_descriptor);
246 6x return aw;
247 }
248
249 /** Asynchronously wait for the acceptor to be ready.
250
251 Calls the backend implementation directly, bypassing virtual
252 dispatch. Otherwise identical to @ref local_stream_acceptor::wait.
253
254 @param w The wait direction (typically `wait_type::read`).
255
256 @return An awaitable yielding `io_result<>`.
257 */
258 6x [[nodiscard]] auto wait(wait_type w)
259 {
260 6x return native_wait_awaitable(*this, w);
261 }
262 };
263
264 } // namespace boost::corosio
265
266 #endif // BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_ACCEPTOR_HPP
267