#include "internal.hpp" #include namespace edge { Reply Bank::read(std::int64_t fd, std::int64_t count) { if (!detail::fd_range(fd)) { return detail::failure("RANGE"); } if (!detail::count_range(count)) { return detail::failure("RANGE"); } auto& entry = state_->descriptors[static_cast(fd)]; if (!entry.live) { return detail::failure("CLOSED"); } const int index = static_cast(fd); const int start = entry.cursor; const auto& line = state_->pins[entry.pin]; std::string value; value.push_back(static_cast('0' + line.value)); value.push_back('\n'); const std::size_t amount = std::min(static_cast(count), value.size() - static_cast(start)); const std::string data = value.substr(static_cast(start), amount); entry.cursor += static_cast(amount); if (start == 0 && amount > 0) { for (int peer = 0; peer < 16; ++peer) { if (state_->descriptors[peer].live && state_->descriptors[peer].pin == entry.pin) { state_->acknowledge(peer); } } } if (start == 2 && count > 0) { state_->acknowledge(index); } return detail::success(static_cast(amount), data); } }