#include "clock.hpp" #include #include namespace shared_clock { namespace { using Image = std::array; void store(Image& image, std::size_t offset, std::uint64_t value, unsigned width) { for (unsigned index = 0; index < width; ++index) { image[offset + index] = static_cast(value & 255); value >>= 8; } } std::uint64_t fetch(const Image& image, std::size_t offset, unsigned width) { std::uint64_t value = 0; for (unsigned index = 0; index < width; ++index) { value |= static_cast(image[offset + index]) << (8 * index); } return value; } std::uint64_t checksum(const Image& image) { std::uint64_t value = 14695981039346656037ULL; for (std::size_t index = 0; index < 56; ++index) { value ^= image[index]; value *= 1099511628211ULL; } return value; } std::int64_t signed_word(std::uint64_t value) { if (value <= static_cast(std::numeric_limits::max())) { return static_cast(value); } return -1 - static_cast(~value); } void header(Image& image, const Snapshot& snapshot) { image[0] = 'C'; image[1] = 'K'; image[2] = 'S'; image[3] = '1'; store(image, 4, 1, 2); store(image, 6, 64, 2); store(image, 8, snapshot.sequence, 8); } void payload(Image& image, const Snapshot& snapshot) { store(image, 16, snapshot.local, 8); store(image, 24, snapshot.remote, 8); store(image, 32, static_cast(snapshot.offset), 8); store(image, 40, snapshot.delay, 8); store(image, 48, snapshot.id, 4); store(image, 52, 0, 4); } } Page::Page() { reset(); } void Page::reset() { image_.fill(0); } Status Page::load(const std::vector& data) { if (data.size() != image_.size()) { return Status::Invalid; } std::copy(data.begin(), data.end(), image_.begin()); return Status::Ok; } std::vector Page::bytes() const { return {image_.begin(), image_.end()}; } Status Page::publish(const Snapshot& snapshot, unsigned failpoint) { if (failpoint > 3) { return Status::Invalid; } if (validate_snapshot(snapshot) != Status::Ok) { return Status::Range; } header(image_, snapshot); if (failpoint == 1) { return Status::PublishFailed; } payload(image_, snapshot); if (failpoint == 2) { return Status::PublishFailed; } store(image_, 56, checksum(image_), 8); if (failpoint == 3) { return Status::PublishFailed; } return Status::Ok; } Status Page::read(Snapshot& out) const { if (std::all_of(image_.begin(), image_.end(), [](std::uint8_t byte) { return byte == 0; })) { return Status::Empty; } if (image_[0] != 'C' || image_[1] != 'K' || image_[2] != 'S' || image_[3] != '1' || fetch(image_, 4, 2) != 1 || fetch(image_, 6, 2) != 64 || fetch(image_, 52, 4) != 0 || fetch(image_, 56, 8) != checksum(image_)) { return Status::Empty; } Snapshot decoded; decoded.sequence = fetch(image_, 8, 8); decoded.local = fetch(image_, 16, 8); decoded.remote = fetch(image_, 24, 8); decoded.offset = signed_word(fetch(image_, 32, 8)); decoded.delay = fetch(image_, 40, 8); decoded.id = static_cast(fetch(image_, 48, 4)); if (validate_snapshot(decoded) != Status::Ok) { return Status::Empty; } out = decoded; return Status::Ok; } Status project(const Page& page, Tick now, Tick max_age, Projection& out) { Snapshot snapshot; const Status status = page.read(snapshot); if (status != Status::Ok) { return status; } if (now > LIMIT || max_age > LIMIT) { return Status::Range; } if (now < snapshot.local || now - snapshot.local > max_age) { return Status::Stale; } const Tick elapsed = now - snapshot.local; if (elapsed > LIMIT - snapshot.remote) { return Status::Range; } Projection result; result.sequence = snapshot.sequence; result.remote_ticks = snapshot.remote + elapsed; ticks_to_microseconds(result.remote_ticks, result.microseconds); out = result; return Status::Ok; } }