William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 1 | #include <stdplus/exception.hpp> |
| 2 | #include <stdplus/fd/ops.hpp> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 3 | |
William A. Kennington III | 6417c63 | 2023-07-17 02:56:52 -0700 | [diff] [blame] | 4 | #include <format> |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 5 | #include <utility> |
| 6 | |
| 7 | namespace stdplus |
| 8 | { |
| 9 | namespace fd |
| 10 | { |
| 11 | namespace detail |
| 12 | { |
| 13 | |
| 14 | template <typename Fun, typename Byte, typename... Args> |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 15 | static void opExact(const char* name, Fun&& fun, Fd& fd, std::span<Byte> data, |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 16 | Args&&... args) |
| 17 | { |
William A. Kennington III | 93ae970 | 2023-12-22 16:41:17 -0800 | [diff] [blame] | 18 | std::size_t total = 0; |
| 19 | try |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 20 | { |
William A. Kennington III | 93ae970 | 2023-12-22 16:41:17 -0800 | [diff] [blame] | 21 | while (total < data.size()) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 22 | { |
Patrick Williams | d8e0af5 | 2024-08-16 15:21:12 -0400 | [diff] [blame^] | 23 | auto r = |
| 24 | (fd.*fun)(data.subspan(total), std::forward<Args>(args)...); |
William A. Kennington III | 93ae970 | 2023-12-22 16:41:17 -0800 | [diff] [blame] | 25 | if (r.size() == 0) |
| 26 | { |
| 27 | throw exception::WouldBlock( |
| 28 | std::format("{} missing", name, data.size())); |
| 29 | } |
| 30 | total += r.size(); |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 31 | } |
William A. Kennington III | 93ae970 | 2023-12-22 16:41:17 -0800 | [diff] [blame] | 32 | } |
| 33 | catch (const std::system_error&) |
| 34 | { |
| 35 | if (total != 0) |
| 36 | { |
| 37 | throw exception::Incomplete( |
| 38 | std::format("{} is {}B/{}B", name, total, data.size())); |
| 39 | } |
| 40 | throw; |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 44 | void readExact(Fd& fd, std::span<std::byte> data) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 45 | { |
| 46 | opExact("readExact", &Fd::read, fd, data); |
| 47 | } |
| 48 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 49 | void recvExact(Fd& fd, std::span<std::byte> data, RecvFlags flags) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 50 | { |
| 51 | opExact("recvExact", &Fd::recv, fd, data, flags); |
| 52 | } |
| 53 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 54 | void writeExact(Fd& fd, std::span<const std::byte> data) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 55 | { |
| 56 | opExact("writeExact", &Fd::write, fd, data); |
| 57 | } |
| 58 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 59 | void sendExact(Fd& fd, std::span<const std::byte> data, SendFlags flags) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 60 | { |
| 61 | opExact("sendExact", &Fd::send, fd, data, flags); |
| 62 | } |
| 63 | |
William A. Kennington III | 4edcc6e | 2023-12-19 17:24:54 -0800 | [diff] [blame] | 64 | void sendtoExact(Fd& fd, std::span<const std::byte> data, SendFlags flags, |
| 65 | std::span<const std::byte> addr) |
| 66 | { |
| 67 | auto r = fd.sendto(data, flags, addr); |
| 68 | if (r.size() == 0) |
| 69 | { |
| 70 | throw exception::WouldBlock("sendto"); |
| 71 | } |
| 72 | if (r.size() < data.size()) |
| 73 | { |
| 74 | throw exception::Incomplete("sendto"); |
| 75 | } |
| 76 | } |
| 77 | |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 78 | template <typename Fun, typename Byte, typename... Args> |
Patrick Williams | d8e0af5 | 2024-08-16 15:21:12 -0400 | [diff] [blame^] | 79 | static std::span<Byte> |
| 80 | opAligned(const char* name, Fun&& fun, Fd& fd, size_t align, |
| 81 | std::span<Byte> data, Args&&... args) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 82 | { |
William A. Kennington III | b88c457 | 2023-12-22 16:43:42 -0800 | [diff] [blame] | 83 | std::size_t total = 0; |
| 84 | try |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 85 | { |
William A. Kennington III | b88c457 | 2023-12-22 16:43:42 -0800 | [diff] [blame] | 86 | do |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 87 | { |
Patrick Williams | d8e0af5 | 2024-08-16 15:21:12 -0400 | [diff] [blame^] | 88 | auto r = |
| 89 | (fd.*fun)(data.subspan(total), std::forward<Args>(args)...); |
William A. Kennington III | b88c457 | 2023-12-22 16:43:42 -0800 | [diff] [blame] | 90 | if (total != 0 && r.size() == 0) |
| 91 | { |
| 92 | throw exception::Incomplete( |
| 93 | std::format("{} is {}B/{}B", name, total % align, align)); |
| 94 | } |
| 95 | total += r.size(); |
| 96 | } while (total % align != 0); |
| 97 | } |
| 98 | catch (const std::system_error&) |
| 99 | { |
| 100 | if (total % align) |
| 101 | { |
| 102 | throw exception::Incomplete( |
| 103 | std::format("{} is {}B/{}B", name, total % align, align)); |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 104 | } |
William A. Kennington III | b88c457 | 2023-12-22 16:43:42 -0800 | [diff] [blame] | 105 | throw; |
| 106 | } |
| 107 | return std::span<Byte>(data.data(), total); |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 110 | std::span<std::byte> readAligned(Fd& fd, size_t align, std::span<std::byte> buf) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 111 | { |
| 112 | return opAligned("readAligned", &Fd::read, fd, align, buf); |
| 113 | } |
| 114 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 115 | std::span<std::byte> recvAligned(Fd& fd, size_t align, std::span<std::byte> buf, |
| 116 | RecvFlags flags) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 117 | { |
| 118 | return opAligned("recvAligned", &Fd::recv, fd, align, buf, flags); |
| 119 | } |
| 120 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 121 | std::span<const std::byte> writeAligned(Fd& fd, size_t align, |
| 122 | std::span<const std::byte> data) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 123 | { |
| 124 | return opAligned("writeAligned", &Fd::write, fd, align, data); |
| 125 | } |
| 126 | |
Patrick Williams | d8e0af5 | 2024-08-16 15:21:12 -0400 | [diff] [blame^] | 127 | std::span<const std::byte> sendAligned( |
| 128 | Fd& fd, size_t align, std::span<const std::byte> data, SendFlags flags) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 129 | { |
| 130 | return opAligned("sendAligned", &Fd::send, fd, align, data, flags); |
| 131 | } |
| 132 | |
William A. Kennington III | d589b1b | 2023-12-24 10:39:30 -0800 | [diff] [blame] | 133 | constexpr std::size_t maxStrideB = 65536; |
| 134 | |
| 135 | void readAll(Fd& fd, function_view<std::span<std::byte>(size_t req)> resize) |
| 136 | { |
| 137 | std::size_t strideB = 256; |
| 138 | std::size_t totalB = 0; |
| 139 | auto doRead = [&]() { |
| 140 | auto buf = resize(totalB + strideB); |
| 141 | auto r = fd.read(buf.subspan(totalB)); |
| 142 | if (r.size() == 0) |
| 143 | { |
| 144 | throw exception::WouldBlock("readAll"); |
| 145 | } |
| 146 | totalB += r.size(); |
| 147 | }; |
| 148 | auto validateSize = [&]() { |
| 149 | auto buf = resize(totalB); |
| 150 | if (totalB != buf.size()) |
| 151 | { |
| 152 | throw exception::Incomplete( |
| 153 | std::format("readAll extra {}B", buf.size() - totalB)); |
| 154 | } |
| 155 | }; |
| 156 | try |
| 157 | { |
| 158 | while (strideB < maxStrideB) |
| 159 | { |
| 160 | doRead(); |
| 161 | if (totalB >= strideB) |
| 162 | { |
| 163 | strideB = strideB << 1; |
| 164 | } |
| 165 | } |
| 166 | while (true) |
| 167 | { |
| 168 | doRead(); |
| 169 | } |
| 170 | } |
| 171 | catch (const exception::Eof&) |
| 172 | { |
| 173 | validateSize(); |
| 174 | return; |
| 175 | } |
| 176 | catch (const std::system_error&) |
| 177 | { |
| 178 | validateSize(); |
| 179 | throw; |
| 180 | } |
| 181 | } |
| 182 | |
William A. Kennington III | 4829c9d | 2023-12-24 10:39:43 -0800 | [diff] [blame] | 183 | std::span<std::byte> readAllFixed(Fd& fd, size_t align, |
| 184 | std::span<std::byte> buf) |
| 185 | { |
| 186 | std::size_t totalB = 0; |
| 187 | auto validateSize = [&]() { |
| 188 | if (totalB % align != 0) |
| 189 | { |
| 190 | throw exception::Incomplete(std::format( |
| 191 | "readAllFixed partial {}B/{}B", totalB % align, align)); |
| 192 | } |
| 193 | }; |
| 194 | try |
| 195 | { |
| 196 | while (totalB < buf.size()) |
| 197 | { |
| 198 | auto r = fd.read( |
| 199 | buf.subspan(totalB, std::min(maxStrideB, buf.size() - totalB))); |
| 200 | if (r.size() == 0) |
| 201 | { |
| 202 | throw exception::WouldBlock("readAllFixed"); |
| 203 | } |
| 204 | totalB += r.size(); |
| 205 | } |
| 206 | std::byte b; |
| 207 | auto r = fd.read(std::span(&b, 1)); |
| 208 | if (r.size() == 0) |
| 209 | { |
| 210 | throw exception::WouldBlock("readAllFixed"); |
| 211 | } |
| 212 | throw std::system_error( |
| 213 | std::make_error_code(std::errc::value_too_large), |
| 214 | "readAllFixed overflow"); |
| 215 | } |
| 216 | catch (const exception::Eof& e) |
| 217 | { |
| 218 | validateSize(); |
| 219 | return buf.subspan(0, totalB); |
| 220 | } |
| 221 | catch (const std::system_error& e) |
| 222 | { |
| 223 | validateSize(); |
| 224 | throw; |
| 225 | } |
| 226 | } |
| 227 | |
William A. Kennington III | 4aec6d1 | 2024-01-08 15:28:34 -0800 | [diff] [blame] | 228 | void verifyExact(size_t expected, size_t actual) |
| 229 | { |
| 230 | if (expected != actual) |
| 231 | { |
| 232 | throw exception::WouldBlock("verifyExact"); |
| 233 | } |
| 234 | } |
| 235 | |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 236 | } // namespace detail |
| 237 | } // namespace fd |
| 238 | } // namespace stdplus |