|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 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_DETAIL_POSIX_OP_HPP |
| 11 | +#define BOOST_COROSIO_DETAIL_POSIX_OP_HPP |
| 12 | + |
| 13 | +#include <boost/corosio/detail/config.hpp> |
| 14 | +#include <boost/corosio/io_object.hpp> |
| 15 | +#include <boost/capy/ex/any_dispatcher.hpp> |
| 16 | +#include <boost/capy/concept/affine_awaitable.hpp> |
| 17 | +#include <boost/capy/ex/any_coro.hpp> |
| 18 | +#include <boost/capy/error.hpp> |
| 19 | +#include <boost/capy/ex/execution_context.hpp> |
| 20 | +#include <boost/system/error_code.hpp> |
| 21 | + |
| 22 | +#include <unistd.h> |
| 23 | +#include <errno.h> |
| 24 | + |
| 25 | +#include <atomic> |
| 26 | +#include <cstddef> |
| 27 | +#include <cstdint> |
| 28 | +#include <optional> |
| 29 | +#include <stop_token> |
| 30 | + |
| 31 | +#include <netinet/in.h> |
| 32 | +#include <sys/socket.h> |
| 33 | +#include <sys/uio.h> |
| 34 | + |
| 35 | +namespace boost { |
| 36 | +namespace corosio { |
| 37 | +namespace detail { |
| 38 | + |
| 39 | +/** Base class for POSIX async operations. |
| 40 | +
|
| 41 | + This class is analogous to overlapped_op on Windows. |
| 42 | + It stores the coroutine handle, dispatcher, and result |
| 43 | + pointers needed to complete an async operation. |
| 44 | +*/ |
| 45 | +struct posix_op : capy::execution_context::handler |
| 46 | +{ |
| 47 | + struct canceller |
| 48 | + { |
| 49 | + posix_op* op; |
| 50 | + void operator()() const noexcept { op->request_cancel(); } |
| 51 | + }; |
| 52 | + |
| 53 | + capy::any_coro h; |
| 54 | + capy::any_dispatcher d; |
| 55 | + system::error_code* ec_out = nullptr; |
| 56 | + std::size_t* bytes_out = nullptr; |
| 57 | + |
| 58 | + int fd = -1; // Socket file descriptor |
| 59 | + std::uint32_t events = 0; // Requested epoll events (EPOLLIN/EPOLLOUT) |
| 60 | + int error = 0; // errno on completion |
| 61 | + std::size_t bytes_transferred = 0; |
| 62 | + |
| 63 | + std::atomic<bool> cancelled{false}; |
| 64 | + std::optional<std::stop_callback<canceller>> stop_cb; |
| 65 | + |
| 66 | + posix_op() |
| 67 | + { |
| 68 | + data_ = this; |
| 69 | + } |
| 70 | + |
| 71 | + void reset() noexcept |
| 72 | + { |
| 73 | + fd = -1; |
| 74 | + events = 0; |
| 75 | + error = 0; |
| 76 | + bytes_transferred = 0; |
| 77 | + cancelled.store(false, std::memory_order_relaxed); |
| 78 | + } |
| 79 | + |
| 80 | + void operator()() override |
| 81 | + { |
| 82 | + stop_cb.reset(); |
| 83 | + |
| 84 | + if (ec_out) |
| 85 | + { |
| 86 | + if (cancelled.load(std::memory_order_acquire)) |
| 87 | + *ec_out = make_error_code(system::errc::operation_canceled); |
| 88 | + else if (error != 0) |
| 89 | + *ec_out = system::error_code(error, system::system_category()); |
| 90 | + else if (is_read_operation() && bytes_transferred == 0) |
| 91 | + { |
| 92 | + // EOF: 0 bytes transferred with no error indicates end of stream |
| 93 | + *ec_out = make_error_code(capy::error::eof); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (bytes_out) |
| 98 | + *bytes_out = bytes_transferred; |
| 99 | + |
| 100 | + d(h).resume(); |
| 101 | + } |
| 102 | + |
| 103 | + // Returns true if this is a read operation (for EOF detection) |
| 104 | + virtual bool is_read_operation() const noexcept { return false; } |
| 105 | + |
| 106 | + void destroy() override |
| 107 | + { |
| 108 | + stop_cb.reset(); |
| 109 | + } |
| 110 | + |
| 111 | + void request_cancel() noexcept |
| 112 | + { |
| 113 | + cancelled.store(true, std::memory_order_release); |
| 114 | + } |
| 115 | + |
| 116 | + void start(std::stop_token token) |
| 117 | + { |
| 118 | + cancelled.store(false, std::memory_order_release); |
| 119 | + stop_cb.reset(); |
| 120 | + |
| 121 | + if (token.stop_possible()) |
| 122 | + stop_cb.emplace(token, canceller{this}); |
| 123 | + } |
| 124 | + |
| 125 | + void complete(int err, std::size_t bytes) noexcept |
| 126 | + { |
| 127 | + error = err; |
| 128 | + bytes_transferred = bytes; |
| 129 | + } |
| 130 | + |
| 131 | + /** Called when epoll signals the fd is ready. |
| 132 | + Derived classes override this to perform the actual I/O. |
| 133 | + Sets error and bytes_transferred appropriately. |
| 134 | + */ |
| 135 | + virtual void perform_io() noexcept {} |
| 136 | +}; |
| 137 | + |
| 138 | +inline posix_op* |
| 139 | +get_posix_op(capy::execution_context::handler* h) noexcept |
| 140 | +{ |
| 141 | + return static_cast<posix_op*>(h->data()); |
| 142 | +} |
| 143 | + |
| 144 | +//------------------------------------------------------------------------------ |
| 145 | + |
| 146 | +/** Connect operation state. */ |
| 147 | +struct posix_connect_op : posix_op |
| 148 | +{ |
| 149 | + void perform_io() noexcept override |
| 150 | + { |
| 151 | + // For connect, check SO_ERROR to see if connection succeeded |
| 152 | + int err = 0; |
| 153 | + socklen_t len = sizeof(err); |
| 154 | + if (::getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) |
| 155 | + err = errno; |
| 156 | + complete(err, 0); |
| 157 | + } |
| 158 | +}; |
| 159 | + |
| 160 | +//------------------------------------------------------------------------------ |
| 161 | + |
| 162 | +/** Read operation state with buffer descriptors. */ |
| 163 | +struct posix_read_op : posix_op |
| 164 | +{ |
| 165 | + static constexpr std::size_t max_buffers = 16; |
| 166 | + iovec iovecs[max_buffers]; |
| 167 | + int iovec_count = 0; |
| 168 | + |
| 169 | + bool is_read_operation() const noexcept override { return true; } |
| 170 | + |
| 171 | + void reset() noexcept |
| 172 | + { |
| 173 | + posix_op::reset(); |
| 174 | + iovec_count = 0; |
| 175 | + } |
| 176 | + |
| 177 | + void perform_io() noexcept override |
| 178 | + { |
| 179 | + ssize_t n = ::readv(fd, iovecs, iovec_count); |
| 180 | + if (n >= 0) |
| 181 | + complete(0, static_cast<std::size_t>(n)); |
| 182 | + else |
| 183 | + complete(errno, 0); |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +//------------------------------------------------------------------------------ |
| 188 | + |
| 189 | +/** Write operation state with buffer descriptors. */ |
| 190 | +struct posix_write_op : posix_op |
| 191 | +{ |
| 192 | + static constexpr std::size_t max_buffers = 16; |
| 193 | + iovec iovecs[max_buffers]; |
| 194 | + int iovec_count = 0; |
| 195 | + |
| 196 | + void reset() noexcept |
| 197 | + { |
| 198 | + posix_op::reset(); |
| 199 | + iovec_count = 0; |
| 200 | + } |
| 201 | + |
| 202 | + void perform_io() noexcept override |
| 203 | + { |
| 204 | + ssize_t n = ::writev(fd, iovecs, iovec_count); |
| 205 | + if (n >= 0) |
| 206 | + complete(0, static_cast<std::size_t>(n)); |
| 207 | + else |
| 208 | + complete(errno, 0); |
| 209 | + } |
| 210 | +}; |
| 211 | + |
| 212 | +//------------------------------------------------------------------------------ |
| 213 | + |
| 214 | +/** Accept operation state. */ |
| 215 | +struct posix_accept_op : posix_op |
| 216 | +{ |
| 217 | + int accepted_fd = -1; |
| 218 | + io_object::io_object_impl* peer_impl = nullptr; |
| 219 | + io_object::io_object_impl** impl_out = nullptr; |
| 220 | + |
| 221 | + // Function to create peer impl - set by posix_sockets |
| 222 | + using create_peer_fn = io_object::io_object_impl* (*)(void*, int); |
| 223 | + create_peer_fn create_peer = nullptr; |
| 224 | + void* service_ptr = nullptr; |
| 225 | + |
| 226 | + void reset() noexcept |
| 227 | + { |
| 228 | + posix_op::reset(); |
| 229 | + accepted_fd = -1; |
| 230 | + peer_impl = nullptr; |
| 231 | + impl_out = nullptr; |
| 232 | + // Don't reset create_peer and service_ptr - they're set once |
| 233 | + } |
| 234 | + |
| 235 | + void perform_io() noexcept override |
| 236 | + { |
| 237 | + sockaddr_in addr{}; |
| 238 | + socklen_t addrlen = sizeof(addr); |
| 239 | + int new_fd = ::accept4(fd, reinterpret_cast<sockaddr*>(&addr), |
| 240 | + &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); |
| 241 | + |
| 242 | + if (new_fd >= 0) |
| 243 | + { |
| 244 | + accepted_fd = new_fd; |
| 245 | + if (create_peer && service_ptr) |
| 246 | + peer_impl = create_peer(service_ptr, new_fd); |
| 247 | + complete(0, 0); |
| 248 | + } |
| 249 | + else |
| 250 | + { |
| 251 | + complete(errno, 0); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + void operator()() override |
| 256 | + { |
| 257 | + stop_cb.reset(); |
| 258 | + |
| 259 | + bool success = (error == 0 && !cancelled.load(std::memory_order_acquire)); |
| 260 | + |
| 261 | + if (ec_out) |
| 262 | + { |
| 263 | + if (cancelled.load(std::memory_order_acquire)) |
| 264 | + *ec_out = make_error_code(system::errc::operation_canceled); |
| 265 | + else if (error != 0) |
| 266 | + *ec_out = system::error_code(error, system::system_category()); |
| 267 | + } |
| 268 | + |
| 269 | + if (success && accepted_fd >= 0 && peer_impl) |
| 270 | + { |
| 271 | + // Pass impl to awaitable for assignment to peer socket |
| 272 | + if (impl_out) |
| 273 | + *impl_out = peer_impl; |
| 274 | + peer_impl = nullptr; |
| 275 | + } |
| 276 | + else |
| 277 | + { |
| 278 | + // Cleanup on failure |
| 279 | + if (accepted_fd >= 0) |
| 280 | + { |
| 281 | + ::close(accepted_fd); |
| 282 | + accepted_fd = -1; |
| 283 | + } |
| 284 | + |
| 285 | + if (peer_impl) |
| 286 | + { |
| 287 | + peer_impl->release(); |
| 288 | + peer_impl = nullptr; |
| 289 | + } |
| 290 | + |
| 291 | + if (impl_out) |
| 292 | + *impl_out = nullptr; |
| 293 | + } |
| 294 | + |
| 295 | + d(h).resume(); |
| 296 | + } |
| 297 | +}; |
| 298 | + |
| 299 | +} // namespace detail |
| 300 | +} // namespace corosio |
| 301 | +} // namespace boost |
| 302 | + |
| 303 | +#endif |
0 commit comments