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