Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
pcap_feed.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "md_feed.hpp"
5#include "replay_gate.hpp"
6
8#include <common/types.hpp>
9#include <nlohmann/json.hpp>
11
12#include <cstdint>
13#include <memory>
14#include <span>
15#include <string>
16#include <string_view>
17#include <utility>
18#include <vector>
19
20namespace slick::sim::md_feed {
21
34 std::string root;
35 uint64_t begin_ns = 0;
36 uint64_t end_ns = 0;
37 double speed = 1.0;
38 uint16_t control_port = 0;
39 static constexpr int DEFAULT_CONTROL_MAX_CONNECTIONS = 100;
41
43 static PcapFeedConfig from_json(const nlohmann::json &config, const std::string &feed);
44};
45
48[[nodiscard]] uint64_t parse_utc_time_ns(const std::string &text);
49
53[[nodiscard]] std::vector<PcapLine> parse_pcap_lines(const nlohmann::json &config,
54 std::span<const std::pair<const char *, uint8_t>> keys);
55
80class PcapFeedBase : public MDFeed {
81public:
83 PcapFeedBase(std::string name, Venue venue, const PcapFeedConfig &config, std::vector<PcapLine> lines,
84 std::shared_ptr<ReplayGate> gate);
85 ~PcapFeedBase() override;
86
87 void start() override;
88 void stop() override;
89
92 void poll(uint64_t now_ns);
93
94 [[nodiscard]] FeedState state() const noexcept { return state_; }
95 [[nodiscard]] const std::string &name() const noexcept { return name_; }
96 [[nodiscard]] FeedKind feedKind() const noexcept override { return FeedKind::PCAP; }
97 [[nodiscard]] std::string feedName() const override { return name_; }
99 [[nodiscard]] uint16_t control_port() const noexcept { return control_.listen_port(); }
102 cli::CliEndpoint *cliEndpoint() noexcept override { return &control_.cli(); }
103
106 [[nodiscard]] uint64_t event_time_ns() const noexcept { return event_time_ns_; }
107
108protected:
111 virtual void on_packet(uint8_t line, const uint8_t *data, size_t size, uint64_t now_ns) = 0;
113 virtual void on_timer(uint64_t now_ns) = 0;
115 [[nodiscard]] virtual bool synchronized() const = 0;
118 [[nodiscard]] virtual std::string describe() const { return {}; }
119
120private:
121 static constexpr uint32_t MAX_PACKETS_PER_POLL = 4096;
122 static constexpr uint64_t HEARTBEAT_NS = 1'000'000'000;
123
124 void serve_control();
125 void enter(FeedState next, uint64_t now_ns, std::string message = {});
126 void publish_status(uint64_t now_ns);
127 void feed(uint64_t now_ns);
128
129 std::string name_;
130 Venue venue_;
131 PcapFeedConfig config_;
132 std::shared_ptr<ReplayGate> gate_;
133 PcapReplaySource source_;
134 PcapControlServer control_;
135 ReplayPacer pacer_;
136
137 bool start_requested_ = false;
140 bool control_down_ = false;
141 FeedState state_ = FeedState::IDLE;
142 std::string message_;
143 uint64_t current_capture_ns_ = 0;
144 uint64_t event_time_ns_ = 0;
145 uint64_t next_heartbeat_ns_ = 0;
146};
147
148} // namespace slick::sim::md_feed
Where the CLI server's commands reach a thread that owns some state (the main thread,...
FeedKind feedKind() const noexcept override
Definition pcap_feed.hpp:96
const std::string & name() const noexcept
Definition pcap_feed.hpp:95
uint64_t event_time_ns() const noexcept
Wall time to stamp on the events of the packet being handled.
virtual void on_timer(uint64_t now_ns)=0
The handler's timers (gap timeouts), while it is being fed.
FeedState state() const noexcept
Definition pcap_feed.hpp:94
void poll(uint64_t now_ns)
Exchange thread: serves control requests, advances the state machine and feeds the handler what is du...
uint16_t control_port() const noexcept
The listening port of the control channel (0 before start()).
Definition pcap_feed.hpp:99
cli::CliEndpoint * cliEndpoint() noexcept override
pcap/<venue>/<feed>: replay and status, served on the control channel's thread (by poll() if that nev...
virtual std::string describe() const
What the handler has done so far, for the log at READY and at the end: packets, gaps,...
std::string feedName() const override
Its name, different from the venue's other feeds'. Empty for a feed without one.
Definition pcap_feed.hpp:97
virtual void on_packet(uint8_t line, const uint8_t *data, size_t size, uint64_t now_ns)=0
Hands one captured packet to the venue's protocol handler.
PcapFeedBase(std::string name, Venue venue, const PcapFeedConfig &config, std::vector< PcapLine > lines, std::shared_ptr< ReplayGate > gate)
gate is shared with the market-data publisher, which holds market data back until it opens.
uint64_t parse_utc_time_ns(const std::string &text)
Parses a UTC "YYYY-MM-DDTHH:MM:SS[.fraction][Z]" into nanoseconds since the epoch.
Definition pcap_feed.cpp:39
std::vector< PcapLine > parse_pcap_lines(const nlohmann::json &config, std::span< const std::pair< const char *, uint8_t > > keys)
The multicast groups of a pcap feed's config: for each {key, line} that the config names,...
FeedKind
Where a feed's market data comes from.
Definition md_feed.hpp:13
@ PCAP
Recorded captures.
Definition md_feed.hpp:15
FeedState
Where a pcap replay feed is. The values are on the wire: append, never renumber.
What a pcap feed's config entry says, beyond its venue's multicast groups.
Definition pcap_feed.hpp:33
static PcapFeedConfig from_json(const nlohmann::json &config, const std::string &feed)
Throws std::runtime_error naming feed when a key is missing or invalid.
Definition pcap_feed.cpp:84
static constexpr int DEFAULT_CONTROL_MAX_CONNECTIONS
Definition pcap_feed.hpp:39