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