#pragma once // M10.2 owned UDP impairment relay (ADR-0090) for tests and measurements; never shipped. // Listens on 127.0.0.1: for one client at a time (a new sender takes over after the // current one has been silent for 1 s: a reconnect) and forwards to 127.0.0.1:, applying // seeded loss, duplication, reordering, delay/jitter, a payload-size (MTU) limit and // corruption to each packet in each direction. Impairment decisions come from SplitMix64 // in packet order; wall-clock delivery times are not deterministic. Runs its own thread. #include #include #include namespace evo::network::impair { struct Impairment { double loss{}; // Probability a packet is dropped. double duplicate{}; // Probability a packet is sent twice. double reorder{}; // Probability a packet is held back by `reorder_delay`. double corrupt{}; // Probability one byte is flipped. std::chrono::milliseconds delay{}, jitter{}, reorder_delay{10}; std::size_t max_payload{65507}; // Larger UDP payloads are dropped (path MTU). }; struct RelayCounters { std::uint64_t forwarded{}, dropped{}, duplicated{}, reordered{}, corrupted{}, oversized{}; }; class UdpRelay { public: // Upstream (client -> server) and downstream (server -> client) impairments. [[nodiscard]] static std::unique_ptr start(std::uint16_t target_port, Impairment upstream, Impairment downstream, std::uint64_t seed); UdpRelay(const UdpRelay&) = delete; UdpRelay& operator=(const UdpRelay&) = delete; ~UdpRelay(); [[nodiscard]] std::uint16_t port() const noexcept; // Changes impairments while running (e.g. a total outage to exercise idle timeout). void set(Impairment upstream, Impairment downstream); [[nodiscard]] RelayCounters counters() const; struct Impl; private: explicit UdpRelay(std::unique_ptr impl); std::unique_ptr impl_; }; } // namespace evo::network::impair