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