itch-mold-replay 1.0
Time-accurate NASDAQ TotalView-ITCH 5.0 replay over MoldUDP64
Loading...
Searching...
No Matches
feed.h
Go to the documentation of this file.
1#pragma once
2
3#include "imr/mold/downstream/heartbeat.h"
4#include "imr/mold/packet_builder.h"
5#include "imr/mold/retransmission_buffer.h"
6#include "imr/mold/downstream/pacer.h"
7
8#include "imr/mold/types.h"
9#include "imr/util/file_descriptor.h"
10#include "imr/util/zstring_view.h"
11
12#include <netinet/in.h>
13#include <stop_token>
14#include <sys/socket.h>
15
16namespace imr::mold::downstream
17{
18 /** Replays a MoldUDP64 downstream feed over multicast.
19 *
20 * Sends heartbeats on a fixed period while running, then
21 * end of session packets for `end_of_session_duration` once the file
22 * is exhausted or `start()`'s stop_token is triggered.
23 */
24 class Feed
25 {
26 public:
27 /// @ingroup config
28 struct Config
29 {
30 /// Multicast group to send to.
32 /// Multicast port.
34 /// Sets IP_MULTICAST_TTL.
35 std::uint8_t ttl{1};
36 /// Enable/Disable IP_MULTICAST_LOOP.
37 bool loopback{false};
38 /**
39 Interface to send multicast packets from (IP_MULTICAST_IF).
40
41 Set explicitly in multi nic environment.
42 */
43 in_addr egress_interface{.s_addr = htonl(INADDR_ANY)};
44 /** Interval between heartbeat packets while the feed is running.
45 *
46 * This is also the interval of the end of session packets.
47 */
48 std::chrono::nanoseconds heartbeat_period{std::chrono::seconds(1)};
49 /// How long end of session should send packets
50 std::chrono::nanoseconds end_of_session_duration{std::chrono::seconds(30)};
51 /// Controls playback speed and pre-market message skipping.
52 Pacer<std::chrono::steady_clock>::Config pacer_cfg{};
53 };
54
55 /** Constructs the feed ready to begin downstream on configured multicast group/port
56
57 @throws std::invalid_argument if cfg.mcast_group is not a valid IPv4 address
58
59 @throws std::system_error if socket creation / configuration fails
60 */
61 explicit Feed(const Config& cfg,
62 const PacketBuilder::Config& packet_builder_cfg,
63 std::span<const char> file,
64 RetransmissionBuffer& retransmission_buffer);
65
66 /** Replays the file until EOF or `st` stopped, then send end of session packets for configured duration
67 *
68 * Blocks until finished.
69 */
70 void start(std::stop_token st);
71
72 private:
73 util::FileDescriptor socket_{[] { return socket(AF_INET, SOCK_DGRAM, 0); }};
74 sockaddr_in mcast_group_;
75 msghdr send_hdr_{};
76
77 std::span<const char> file_;
78 std::size_t file_pos_{0};
79 types::header::SequenceNumber sequence_number_{1};
80 std::atomic<types::header::SequenceNumber> sent_sequence_number_{1};
81
82 RetransmissionBuffer* retransmission_buffer_;
83
84 Pacer<std::chrono::steady_clock> pacer_;
85 PacketBuilder packet_builder_;
86 Heartbeat heartbeat_;
87
88 std::chrono::nanoseconds end_of_session_duration_;
89
90 [[nodiscard]]
91 sockaddr_in configure_socket(const Config& cfg) const;
92
93 void build_packet();
94 void send_packet() noexcept;
95
96 void end_of_session(std::stop_token st);
97 };
98}
Builds MoldUDP64 packets.
Definition packet_builder.h:24
Retransmission buffer of the last buffer_size messages.
Definition retransmission_buffer.h:15
Replays a MoldUDP64 downstream feed over multicast.
Definition feed.h:25
Feed(const Config &cfg, const PacketBuilder::Config &packet_builder_cfg, std::span< const char > file, RetransmissionBuffer &retransmission_buffer)
Constructs the feed ready to begin downstream on configured multicast group/port.
void start(std::stop_token st)
Replays the file until EOF or st stopped, then send end of session packets for configured duration.
Sends MoldUDP64 heartbeat packets at a configured interval.
Definition heartbeat.h:15
Calculates relative delay for a given message timestamp for downstream (which passes the first messag...
Definition pacer.h:47
RAII file descriptor wrapper.
Definition file_descriptor.h:19
A wrapper around std::string_view that guarantees null terminated strings for passing to C APIs.
Definition zstring_view.h:10
Definition types.h:9
Definition types.h:7
Definition file_descriptor.h:11
Definition feed.h:17
Definition packet_builder.h:30
std::uint8_t ttl
Sets IP_MULTICAST_TTL.
Definition feed.h:35
std::chrono::nanoseconds end_of_session_duration
How long end of session should send packets.
Definition feed.h:50
bool loopback
Enable/Disable IP_MULTICAST_LOOP.
Definition feed.h:37
std::chrono::nanoseconds heartbeat_period
Interval between heartbeat packets while the feed is running.
Definition feed.h:48
in_addr egress_interface
Interface to send multicast packets from (IP_MULTICAST_IF).
Definition feed.h:43
Pacer< std::chrono::steady_clock >::Config pacer_cfg
Controls playback speed and pre-market message skipping.
Definition feed.h:52
std::uint16_t port
Multicast port.
Definition feed.h:33
util::zstring_view mcast_group
Multicast group to send to.
Definition feed.h:31