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