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 | { |
| 18 | while (data.size() > 0) |
| 19 | { |
| 20 | auto ret = (fd.*fun)(data, std::forward<Args>(args)...); |
| 21 | if (ret.size() == 0) |
| 22 | { |
| 23 | throw exception::WouldBlock( |
William A. Kennington III | 6417c63 | 2023-07-17 02:56:52 -0700 | [diff] [blame] | 24 | std::format("{} missing {}B", name, data.size())); |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 25 | } |
| 26 | data = data.subspan(ret.size()); |
| 27 | } |
| 28 | } |
| 29 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 30 | void readExact(Fd& fd, std::span<std::byte> data) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 31 | { |
| 32 | opExact("readExact", &Fd::read, fd, data); |
| 33 | } |
| 34 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 35 | 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] | 36 | { |
| 37 | opExact("recvExact", &Fd::recv, fd, data, flags); |
| 38 | } |
| 39 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 40 | void writeExact(Fd& fd, std::span<const std::byte> data) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 41 | { |
| 42 | opExact("writeExact", &Fd::write, fd, data); |
| 43 | } |
| 44 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 45 | 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] | 46 | { |
| 47 | opExact("sendExact", &Fd::send, fd, data, flags); |
| 48 | } |
| 49 | |
| 50 | template <typename Fun, typename Byte, typename... Args> |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 51 | static std::span<Byte> opAligned(const char* name, Fun&& fun, Fd& fd, |
| 52 | size_t align, std::span<Byte> data, |
| 53 | Args&&... args) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 54 | { |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 55 | std::span<Byte> ret; |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 56 | do |
| 57 | { |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 58 | auto r = (fd.*fun)(data.subspan(ret.size()), |
| 59 | std::forward<Args>(args)...); |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 60 | if (ret.size() != 0 && r.size() == 0) |
| 61 | { |
| 62 | throw exception::WouldBlock( |
William A. Kennington III | 6417c63 | 2023-07-17 02:56:52 -0700 | [diff] [blame] | 63 | std::format("{} is {}B/{}B", name, ret.size() % align, align)); |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 64 | } |
| 65 | ret = data.subspan(0, ret.size() + r.size()); |
| 66 | } while (ret.size() % align != 0); |
| 67 | return ret; |
| 68 | } |
| 69 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | return opAligned("readAligned", &Fd::read, fd, align, buf); |
| 73 | } |
| 74 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 75 | std::span<std::byte> recvAligned(Fd& fd, size_t align, std::span<std::byte> buf, |
| 76 | RecvFlags flags) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 77 | { |
| 78 | return opAligned("recvAligned", &Fd::recv, fd, align, buf, flags); |
| 79 | } |
| 80 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 81 | std::span<const std::byte> writeAligned(Fd& fd, size_t align, |
| 82 | std::span<const std::byte> data) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 83 | { |
| 84 | return opAligned("writeAligned", &Fd::write, fd, align, data); |
| 85 | } |
| 86 | |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 87 | std::span<const std::byte> sendAligned(Fd& fd, size_t align, |
| 88 | std::span<const std::byte> data, |
| 89 | SendFlags flags) |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 90 | { |
| 91 | return opAligned("sendAligned", &Fd::send, fd, align, data, flags); |
| 92 | } |
| 93 | |
| 94 | } // namespace detail |
| 95 | } // namespace fd |
| 96 | } // namespace stdplus |