include/boost/corosio/native/native_local_stream_socket.hpp

94.4% Lines (68/72) 100.0% List of functions (44/44)
native_local_stream_socket.hpp
f(x) Functions (44)
Function Calls Lines Blocks
boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::get_impl() :81 10x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::get_impl() :81 10x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::native_read_awaitable(boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>&, boost::capy::mutable_buffer) :95 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::native_read_awaitable(boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>&, boost::capy::mutable_buffer) :95 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_ready() const :103 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_ready() const :103 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_resume() const :108 2x 75.0% 67.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_resume() const :108 2x 75.0% 67.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :115 2x 100.0% 77.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_read_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :115 2x 100.0% 77.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_write_awaitable<boost::capy::const_buffer>::native_write_awaitable(boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>&, boost::capy::const_buffer) :133 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_write_awaitable<boost::capy::const_buffer>::native_write_awaitable(boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>&, boost::capy::const_buffer) :133 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_ready() const :141 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_ready() const :141 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_resume() const :146 2x 75.0% 67.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_resume() const :146 2x 75.0% 67.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :153 2x 100.0% 77.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_write_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :153 2x 100.0% 77.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>&, boost::corosio::wait_type) :169 1x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>&, boost::corosio::wait_type) :169 1x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_ready() const :176 1x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const :176 1x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_resume() const :181 1x 75.0% 67.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const :181 1x 75.0% 67.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :188 1x 100.0% 82.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :188 1x 100.0% 82.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::native_connect_awaitable(boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>&, boost::corosio::local_endpoint) :204 5x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_connect_awaitable::native_connect_awaitable(boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>&, boost::corosio::local_endpoint) :204 5x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_ready() const :212 5x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_ready() const :212 5x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_resume() const :217 5x 75.0% 67.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_resume() const :217 5x 75.0% 67.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :224 5x 100.0% 82.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :224 5x 100.0% 82.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_local_stream_socket(boost::capy::execution_context&) :238 14x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_local_stream_socket(boost::capy::execution_context&) :238 14x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::native_local_stream_socket(boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>&&) :258 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::native_local_stream_socket(boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>&&) :258 2x 100.0% 100.0% auto boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::read_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&) :278 2x 100.0% 100.0% auto boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::read_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&) :278 2x 100.0% 100.0% auto boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::write_some<boost::capy::const_buffer>(boost::capy::const_buffer const&) :293 2x 100.0% 100.0% auto boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::write_some<boost::capy::const_buffer>(boost::capy::const_buffer const&) :293 2x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::epoll_t{}>::connect(boost::corosio::local_endpoint) :312 5x 100.0% 100.0% boost::corosio::native_local_stream_socket<boost::corosio::select_t{}>::connect(boost::corosio::local_endpoint) :312 5x 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_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 20x impl_type& get_impl() noexcept
82 {
83 20x 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 4x native_read_awaitable(
96 native_local_stream_socket& self,
97 MutableBufferSequence buffers) noexcept
98 4x : self_(self)
99 4x , buffers_(std::move(buffers))
100 {
101 4x }
102
103 4x bool await_ready() const noexcept
104 {
105 4x return token_.stop_requested();
106 }
107
108 4x capy::io_result<std::size_t> await_resume() const noexcept
109 {
110 4x if (token_.stop_requested())
111 return {make_error_code(std::errc::operation_canceled), 0};
112 4x return {ec_, bytes_transferred_};
113 }
114
115 4x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
116 -> std::coroutine_handle<>
117 {
118 4x token_ = env->stop_token;
119 12x return self_.get_impl().read_some(
120 12x 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 4x native_write_awaitable(
134 native_local_stream_socket& self,
135 ConstBufferSequence buffers) noexcept
136 4x : self_(self)
137 4x , buffers_(std::move(buffers))
138 {
139 4x }
140
141 4x bool await_ready() const noexcept
142 {
143 4x return token_.stop_requested();
144 }
145
146 4x capy::io_result<std::size_t> await_resume() const noexcept
147 {
148 4x if (token_.stop_requested())
149 return {make_error_code(std::errc::operation_canceled), 0};
150 4x return {ec_, bytes_transferred_};
151 }
152
153 4x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
154 -> std::coroutine_handle<>
155 {
156 4x token_ = env->stop_token;
157 12x return self_.get_impl().write_some(
158 12x 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 2x native_wait_awaitable(
170 native_local_stream_socket& self, wait_type w) noexcept
171 2x : self_(self)
172 2x , w_(w)
173 {
174 2x }
175
176 2x bool await_ready() const noexcept
177 {
178 2x return token_.stop_requested();
179 }
180
181 2x capy::io_result<> await_resume() const noexcept
182 {
183 2x if (token_.stop_requested())
184 return {make_error_code(std::errc::operation_canceled)};
185 2x return {ec_};
186 }
187
188 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
189 -> std::coroutine_handle<>
190 {
191 2x token_ = env->stop_token;
192 6x return self_.get_impl().wait(
193 6x 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 10x native_connect_awaitable(
205 native_local_stream_socket& self,
206 corosio::local_endpoint ep) noexcept
207 10x : self_(self)
208 10x , endpoint_(ep)
209 {
210 10x }
211
212 10x bool await_ready() const noexcept
213 {
214 10x return token_.stop_requested();
215 }
216
217 10x capy::io_result<> await_resume() const noexcept
218 {
219 10x if (token_.stop_requested())
220 return {make_error_code(std::errc::operation_canceled)};
221 10x return {ec_};
222 }
223
224 10x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
225 -> std::coroutine_handle<>
226 {
227 10x token_ = env->stop_token;
228 30x return self_.get_impl().connect(
229 30x 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 28x explicit native_local_stream_socket(capy::execution_context& ctx)
239 28x : io_object(create_handle<service_type>(ctx))
240 {
241 28x }
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 4x 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 4x auto read_some(MB const& buffers)
279 {
280 4x 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 4x auto write_some(CB const& buffers)
294 {
295 4x 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 10x auto connect(corosio::local_endpoint ep)
313 {
314 10x if (!is_open())
315 10x open();
316 10x 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 2x [[nodiscard]] auto wait(wait_type w)
329 {
330 2x return native_wait_awaitable(*this, w);
331 }
332 };
333
334 } // namespace boost::corosio
335
336 #endif // BOOST_COROSIO_NATIVE_NATIVE_LOCAL_STREAM_SOCKET_HPP
337