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