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