include/boost/corosio/native/native_tcp_socket.hpp

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