include/boost/corosio/native/native_tcp_socket.hpp

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