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 | { |
William A. Kennington III | 93ae970 | 2023-12-22 16:41:17 -0800 | [diff] [blame^] | 23 | auto r = (fd.*fun)(data.subspan(total), |
| 24 | std::forward<Args>(args)...); |
| 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 | |
| 64 | template <typename Fun, typename Byte, typename... Args> |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 65 | static std::span<Byte> opAligned(const char* name, Fun&& fun, Fd& fd, |
| 66 | size_t align, std::span<Byte> data, |
| 67 | Args&&... args) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 68 | { |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 69 | std::span<Byte> ret; |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 70 | do |
| 71 | { |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 72 | auto r = (fd.*fun)(data.subspan(ret.size()), |
| 73 | std::forward<Args>(args)...); |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 74 | if (ret.size() != 0 && r.size() == 0) |
| 75 | { |
| 76 | throw exception::WouldBlock( |
William A. Kennington III | 6417c63 | 2023-07-17 02:56:52 -0700 | [diff] [blame] | 77 | std::format("{} is {}B/{}B", name, ret.size() % align, align)); |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 78 | } |
| 79 | ret = data.subspan(0, ret.size() + r.size()); |
| 80 | } while (ret.size() % align != 0); |
| 81 | return ret; |
| 82 | } |
| 83 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 84 | 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] | 85 | { |
| 86 | return opAligned("readAligned", &Fd::read, fd, align, buf); |
| 87 | } |
| 88 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 89 | std::span<std::byte> recvAligned(Fd& fd, size_t align, std::span<std::byte> buf, |
| 90 | RecvFlags flags) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 91 | { |
| 92 | return opAligned("recvAligned", &Fd::recv, fd, align, buf, flags); |
| 93 | } |
| 94 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 95 | std::span<const std::byte> writeAligned(Fd& fd, size_t align, |
| 96 | std::span<const std::byte> data) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 97 | { |
| 98 | return opAligned("writeAligned", &Fd::write, fd, align, data); |
| 99 | } |
| 100 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 101 | std::span<const std::byte> sendAligned(Fd& fd, size_t align, |
| 102 | std::span<const std::byte> data, |
| 103 | SendFlags flags) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 104 | { |
| 105 | return opAligned("sendAligned", &Fd::send, fd, align, data, flags); |
| 106 | } |
| 107 | |
| 108 | } // namespace detail |
| 109 | } // namespace fd |
| 110 | } // namespace stdplus |