Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
retransmit_ring.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cassert>
6#include <cstddef>
7#include <cstdint>
8#include <vector>
9
11
18template <size_t MessageCapacity>
20public:
21 struct Slot {
22 uint32_t seq = 0;
23 uint16_t size = 0;
26 uint16_t replay_flag_offset = 0;
27 std::array<uint8_t, MessageCapacity> data{};
28 };
29
31 RetransmitRing() = default;
32 explicit RetransmitRing(size_t capacity) : slots_(std::max<size_t>(capacity, 1)) {}
33
35 Slot &claim(uint32_t seq) noexcept {
36 // A ring that was never resized has no slot to claim; find() answers nullptr
37 // for it, and claiming into it would be a modulo by zero.
38 assert(!slots_.empty());
39 auto &slot = slots_[seq % slots_.size()];
40 slot.seq = seq;
41 return slot;
42 }
43
45 [[nodiscard]] const Slot *find(uint32_t seq) const noexcept {
46 if (seq == 0 || slots_.empty()) {
47 return nullptr;
48 }
49 const auto &slot = slots_[seq % slots_.size()];
50 return slot.seq == seq ? &slot : nullptr;
51 }
52
57 [[nodiscard]] bool holds(uint32_t from, uint32_t count) const noexcept {
58 for (uint64_t i = 0; i < count; ++i) {
59 if (!find(static_cast<uint32_t>(from + i))) {
60 return false;
61 }
62 }
63 return true;
64 }
65
67 void clear() noexcept {
68 for (auto &slot : slots_) {
69 slot.seq = 0;
70 }
71 }
72
73 void resize(size_t capacity) { slots_.resize(std::max<size_t>(capacity, 1)); }
74 void swap(RetransmitRing &other) noexcept { slots_.swap(other.slots_); }
75
76 [[nodiscard]] bool empty() const noexcept { return slots_.empty(); }
77 [[nodiscard]] size_t capacity() const noexcept { return slots_.size(); }
78
79private:
80 std::vector<Slot> slots_;
81};
82
83} // namespace slick::sim::order_gateway
bool holds(uint32_t from, uint32_t count) const noexcept
True when every message in [from, from + count) is still held.
Slot & claim(uint32_t seq) noexcept
Claims seq's slot and returns it for the message to be encoded into.
const Slot * find(uint32_t seq) const noexcept
The message sent as seq, or null when it never was or has been overwritten.
void clear() noexcept
Forgets every message, keeping the slots.
void swap(RetransmitRing &other) noexcept
RetransmitRing()=default
Empty, with no slots: holds nothing until resized.
uint16_t replay_flag_offset
The byte a replay sets on its copy of the message: iLink 3's PossRetransFlag, or ICE BOE's header fla...
std::array< uint8_t, MessageCapacity > data