include/boost/corosio/native/native_tcp_socket.hpp

93.2% Lines (68/73) 100.0% List of functions (46/46)
native_tcp_socket.hpp
f(x) Functions (46)
Function Calls Lines Blocks
boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::get_impl() :81 11x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::get_impl() :81 11x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::native_read_awaitable(boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>&, boost::capy::mutable_buffer) :95 2x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::native_read_awaitable(boost::corosio::native_tcp_socket<boost::corosio::select_t{}>&, boost::capy::mutable_buffer) :95 2x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_ready() const :102 2x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_ready() const :102 2x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_resume() const :107 2x 75.0% 67.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_resume() const :107 2x 75.0% 67.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :114 2x 100.0% 77.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :114 2x 100.0% 77.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_write_awaitable<boost::capy::const_buffer>::native_write_awaitable(boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>&, boost::capy::const_buffer) :132 3x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_write_awaitable<boost::capy::const_buffer>::native_write_awaitable(boost::corosio::native_tcp_socket<boost::corosio::select_t{}>&, boost::capy::const_buffer) :132 3x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_ready() const :139 3x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_ready() const :139 3x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_resume() const :144 3x 75.0% 67.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_resume() const :144 3x 75.0% 67.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :151 3x 100.0% 77.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :151 3x 100.0% 77.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>&, boost::corosio::wait_type) :167 1x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_tcp_socket<boost::corosio::select_t{}>&, boost::corosio::wait_type) :167 1x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_ready() const :173 1x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const :173 1x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_resume() const :178 1x 75.0% 67.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const :178 1x 75.0% 67.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :185 1x 100.0% 82.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :185 1x 100.0% 82.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::native_connect_awaitable(boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>&, boost::corosio::endpoint) :201 5x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_connect_awaitable::native_connect_awaitable(boost::corosio::native_tcp_socket<boost::corosio::select_t{}>&, boost::corosio::endpoint) :201 5x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_ready() const :207 5x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_ready() const :207 5x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_resume() const :212 5x 75.0% 67.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_resume() const :212 5x 75.0% 67.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :219 5x 100.0% 82.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :219 5x 100.0% 82.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_tcp_socket(boost::capy::execution_context&) :233 13x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_tcp_socket(boost::capy::execution_context&) :233 13x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::native_tcp_socket(boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>&&) :259 7x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::native_tcp_socket(boost::corosio::native_tcp_socket<boost::corosio::select_t{}>&&) :259 7x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::operator=(boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>&&) :272 1x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::operator=(boost::corosio::native_tcp_socket<boost::corosio::select_t{}>&&) :272 1x 100.0% 100.0% auto boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::read_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&) :291 2x 100.0% 100.0% auto boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::read_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&) :291 2x 100.0% 100.0% auto boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::write_some<boost::capy::const_buffer>(boost::capy::const_buffer const&) :310 3x 100.0% 100.0% auto boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::write_some<boost::capy::const_buffer>(boost::capy::const_buffer const&) :310 3x 100.0% 100.0% boost::corosio::native_tcp_socket<boost::corosio::epoll_t{}>::connect(boost::corosio::endpoint) :328 1x 75.0% 80.0% boost::corosio::native_tcp_socket<boost::corosio::select_t{}>::connect(boost::corosio::endpoint) :328 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_SOCKET_HPP
11 #define BOOST_COROSIO_NATIVE_NATIVE_TCP_SOCKET_HPP
12
13 #include <boost/corosio/tcp_socket.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 socket with devirtualized I/O operations.
41
42 This class template inherits from @ref tcp_socket and shadows
43 the async operations (`read_some`, `write_some`, `connect`) with
44 versions that call the backend implementation directly, allowing
45 the compiler to inline through the entire call chain.
46
47 Non-async operations (`open`, `close`, `cancel`, socket options)
48 remain unchanged and dispatch through the compiled library.
49
50 A `native_tcp_socket` IS-A `tcp_socket` and can be passed to
51 any function expecting `tcp_socket&` or `io_stream&`, in which
52 case virtual dispatch is used transparently.
53
54 @tparam Backend A backend tag value (e.g., `epoll`,
55 `iocp`) whose type provides the concrete implementation
56 types.
57
58 @par Thread Safety
59 Same as @ref tcp_socket.
60
61 @par Example
62 @code
63 #include <boost/corosio/native/native_tcp_socket.hpp>
64
65 native_io_context<epoll> ctx;
66 native_tcp_socket<epoll> s(ctx);
67 s.open();
68 auto [ec] = co_await s.connect(ep);
69 auto [ec2, n] = co_await s.read_some(buf);
70 @endcode
71
72 @see tcp_socket, epoll_t, iocp_t
73 */
74 template<auto Backend>
75 class native_tcp_socket : public tcp_socket
76 {
77 using backend_type = decltype(Backend);
78 using impl_type = typename backend_type::tcp_socket_type;
79 using service_type = typename backend_type::tcp_service_type;
80
81 22x impl_type& get_impl() noexcept
82 {
83 22x return *static_cast<impl_type*>(h_.get());
84 }
85
86 template<class MutableBufferSequence>
87 struct native_read_awaitable
88 {
89 native_tcp_socket& self_;
90 MutableBufferSequence buffers_;
91 std::stop_token token_;
92 mutable std::error_code ec_;
93 mutable std::size_t bytes_transferred_ = 0;
94
95 4x native_read_awaitable(
96 native_tcp_socket& self, MutableBufferSequence buffers) noexcept
97 4x : self_(self)
98 4x , buffers_(std::move(buffers))
99 {
100 4x }
101
102 4x bool await_ready() const noexcept
103 {
104 4x return token_.stop_requested();
105 }
106
107 4x capy::io_result<std::size_t> await_resume() const noexcept
108 {
109 4x if (token_.stop_requested())
110 return {make_error_code(std::errc::operation_canceled), 0};
111 4x return {ec_, bytes_transferred_};
112 }
113
114 4x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
115 -> std::coroutine_handle<>
116 {
117 4x token_ = env->stop_token;
118 12x return self_.get_impl().read_some(
119 12x h, env->executor, buffers_, token_, &ec_, &bytes_transferred_);
120 }
121 };
122
123 template<class ConstBufferSequence>
124 struct native_write_awaitable
125 {
126 native_tcp_socket& self_;
127 ConstBufferSequence buffers_;
128 std::stop_token token_;
129 mutable std::error_code ec_;
130 mutable std::size_t bytes_transferred_ = 0;
131
132 6x native_write_awaitable(
133 native_tcp_socket& self, ConstBufferSequence buffers) noexcept
134 6x : self_(self)
135 6x , buffers_(std::move(buffers))
136 {
137 6x }
138
139 6x bool await_ready() const noexcept
140 {
141 6x return token_.stop_requested();
142 }
143
144 6x capy::io_result<std::size_t> await_resume() const noexcept
145 {
146 6x if (token_.stop_requested())
147 return {make_error_code(std::errc::operation_canceled), 0};
148 6x return {ec_, bytes_transferred_};
149 }
150
151 6x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
152 -> std::coroutine_handle<>
153 {
154 6x token_ = env->stop_token;
155 18x return self_.get_impl().write_some(
156 18x h, env->executor, buffers_, token_, &ec_, &bytes_transferred_);
157 }
158 };
159
160 struct native_wait_awaitable
161 {
162 native_tcp_socket& self_;
163 wait_type w_;
164 std::stop_token token_;
165 mutable std::error_code ec_;
166
167 2x native_wait_awaitable(native_tcp_socket& self, wait_type w) noexcept
168 2x : self_(self)
169 2x , w_(w)
170 {
171 2x }
172
173 2x bool await_ready() const noexcept
174 {
175 2x return token_.stop_requested();
176 }
177
178 2x capy::io_result<> await_resume() const noexcept
179 {
180 2x if (token_.stop_requested())
181 return {make_error_code(std::errc::operation_canceled)};
182 2x return {ec_};
183 }
184
185 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
186 -> std::coroutine_handle<>
187 {
188 2x token_ = env->stop_token;
189 6x return self_.get_impl().wait(
190 6x h, env->executor, w_, token_, &ec_);
191 }
192 };
193
194 struct native_connect_awaitable
195 {
196 native_tcp_socket& self_;
197 endpoint endpoint_;
198 std::stop_token token_;
199 mutable std::error_code ec_;
200
201 10x native_connect_awaitable(native_tcp_socket& self, endpoint ep) noexcept
202 10x : self_(self)
203 10x , endpoint_(ep)
204 {
205 10x }
206
207 10x bool await_ready() const noexcept
208 {
209 10x return token_.stop_requested();
210 }
211
212 10x capy::io_result<> await_resume() const noexcept
213 {
214 10x if (token_.stop_requested())
215 return {make_error_code(std::errc::operation_canceled)};
216 10x return {ec_};
217 }
218
219 10x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
220 -> std::coroutine_handle<>
221 {
222 10x token_ = env->stop_token;
223 30x return self_.get_impl().connect(
224 30x h, env->executor, endpoint_, token_, &ec_);
225 }
226 };
227
228 public:
229 /** Construct a native socket from an execution context.
230
231 @param ctx The execution context that will own this socket.
232 */
233 26x explicit native_tcp_socket(capy::execution_context& ctx)
234 26x : io_object(create_handle<service_type>(ctx))
235 {
236 26x }
237
238 /** Construct a native socket from an executor.
239
240 @param ex The executor whose context will own the socket.
241 */
242 template<class Ex>
243 requires(!std::same_as<std::remove_cvref_t<Ex>, native_tcp_socket>) &&
244 capy::Executor<Ex>
245 explicit native_tcp_socket(Ex const& ex) : native_tcp_socket(ex.context())
246 {
247 }
248
249 /** Move construct.
250
251 @param other The socket to move from.
252
253 @pre No awaitables returned by @p other's methods exist.
254 @pre @p other is not referenced as a peer in any outstanding
255 accept awaitable.
256 @pre The execution context associated with @p other must
257 outlive this socket.
258 */
259 14x native_tcp_socket(native_tcp_socket&&) noexcept = default;
260
261 /** Move assign.
262
263 @param other The socket to move from.
264
265 @pre No awaitables returned by either `*this` or @p other's
266 methods exist.
267 @pre Neither `*this` nor @p other is referenced as a peer in
268 any outstanding accept awaitable.
269 @pre The execution context associated with @p other must
270 outlive this socket.
271 */
272 2x native_tcp_socket& operator=(native_tcp_socket&&) noexcept = default;
273
274 native_tcp_socket(native_tcp_socket const&) = delete;
275 native_tcp_socket& operator=(native_tcp_socket const&) = delete;
276
277 /** Asynchronously read data from the socket.
278
279 Calls the backend implementation directly, bypassing virtual
280 dispatch. Otherwise identical to @ref io_stream::read_some.
281
282 @param buffers The buffer sequence to read into.
283
284 @return An awaitable yielding `(error_code, std::size_t)`.
285
286 This socket must outlive the returned awaitable. The memory
287 referenced by @p buffers must remain valid until the operation
288 completes.
289 */
290 template<capy::MutableBufferSequence MB>
291 4x auto read_some(MB const& buffers)
292 {
293 4x return native_read_awaitable<MB>(*this, buffers);
294 }
295
296 /** Asynchronously write data to the socket.
297
298 Calls the backend implementation directly, bypassing virtual
299 dispatch. Otherwise identical to @ref io_stream::write_some.
300
301 @param buffers The buffer sequence to write from.
302
303 @return An awaitable yielding `(error_code, std::size_t)`.
304
305 This socket must outlive the returned awaitable. The memory
306 referenced by @p buffers must remain valid until the operation
307 completes.
308 */
309 template<capy::ConstBufferSequence CB>
310 6x auto write_some(CB const& buffers)
311 {
312 6x return native_write_awaitable<CB>(*this, buffers);
313 }
314
315 /** Asynchronously connect to a remote endpoint.
316
317 Calls the backend implementation directly, bypassing virtual
318 dispatch. Otherwise identical to @ref tcp_socket::connect.
319
320 @param ep The remote endpoint to connect to.
321
322 @return An awaitable yielding `io_result<>`.
323
324 @throws std::logic_error if the socket is not open.
325
326 This socket must outlive the returned awaitable.
327 */
328 10x auto connect(endpoint ep)
329 {
330 10x if (!is_open())
331 detail::throw_logic_error("connect: socket not open");
332 10x return native_connect_awaitable(*this, ep);
333 }
334
335 /** Asynchronously wait for the socket to be ready.
336
337 Calls the backend implementation directly, bypassing virtual
338 dispatch. Otherwise identical to @ref tcp_socket::wait.
339
340 @param w The wait direction (read, write, or error).
341
342 @return An awaitable yielding `io_result<>`.
343 */
344 2x [[nodiscard]] auto wait(wait_type w)
345 {
346 2x return native_wait_awaitable(*this, w);
347 }
348 };
349
350 } // namespace boost::corosio
351
352 #endif
353