itch-mold-replay 1.0
Time-accurate NASDAQ TotalView-ITCH 5.0 replay over MoldUDP64
Loading...
Searching...
No Matches
packet_builder.h
Go to the documentation of this file.
1#pragma once
2
3#include "imr/mold/types.h"
4#include <array>
5#include <cstddef>
6#include <span>
7#include <string_view>
8#include <sys/uio.h>
9#include <vector>
10
11namespace imr::mold
12{
13 /** Builds MoldUDP64 packets.
14 *
15 * Usage:
16 *
17 * 1. Call reset() to write the sequence number to the header.
18 *
19 * 2. Add messages with try_add() until all are added, or it returns false (packet full per the configured MTU).
20 *
21 * 3. Call finalize() to write the message count to the header and get the completed packet as a span of iovecs
22 */
24 {
25 public:
26 // todo change this to something like totalview_5_0_min_message_size and update tests but just leaving now to make tests compile temporarily
27 static constexpr auto min_message_size{14};
28 /// @ingroup config
29 struct Config
30 {
31 /// MoldUDP64 Session, Must be 10 characters.
32 std::string_view session;
33 /// MTU for packet, default is ethernet MTU (1500 bytes) - IP/UDP headers (28 bytes).
34 std::size_t MTU{1472};
35 /** Smallest possible message size (including 2-byte length prefix); defaults to 14 (TotalView-ITCH 5.0).
36 *
37 * Configurable in case a future TotalView version introduces smaller messages.
38 *
39 * Used to size the iovecs vector for the worst case packet (`MTU` / `min_message_size`),
40 * i.e. a packet filled entirely with minimum-size messages.
41 *
42 @see https://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/nqtvitchspecification.pdf
43 */
45 };
46
47 /**
48 * @throws std::invalid_argument if cfg.session is not 10 characters
49 * @throws std::invalid_argument if cfg.MTU less than the size of a MoldUDP64 header (20 bytes)
50 */
51 explicit PacketBuilder(const Config& cfg);
52 /** Adds a message to the messge block.
53 *
54 * @returns false without adding message if wouldn't fit under configured MTU.
55 */
56 [[nodiscard]]
57 bool try_add(std::span<const char> message) noexcept;
58 /// Writes the message count to the header and returns header + message block as iovecs.
59 [[nodiscard]]
60 std::span<iovec> finalize() noexcept;
61 /// Writes sequence number for new packet to header and clears the message block.
62 void reset(types::header::SequenceNumber seq = 1) noexcept;
63
64 /// Number of messages added since last `reset()`.
65 [[nodiscard]]
66 types::header::MessageCount message_count() const noexcept;
67 /// Returns the configured MoldUDP64 session from header
68 [[nodiscard]]
69 std::string_view session() const noexcept;
70
71 private:
72 std::size_t MTU_;
73 std::size_t bytes_remaining_;
74
75 std::array<char, types::header::length> header_buffer_{};
76 // [0] = header (*iov_base = header_buffer_), [1..N] = messages
77 // N = 1 (header) + (MTU_ / min_message_size_)
78 std::vector<iovec> iovecs_;
79
80 std::size_t min_message_size_;
81 };
82}
Builds MoldUDP64 packets.
Definition packet_builder.h:24
PacketBuilder(const Config &cfg)
void reset(types::header::SequenceNumber seq=1) noexcept
Writes sequence number for new packet to header and clears the message block.
std::span< iovec > finalize() noexcept
Writes the message count to the header and returns header + message block as iovecs.
static constexpr auto min_message_size
Definition packet_builder.h:27
bool try_add(std::span< const char > message) noexcept
Adds a message to the messge block.
types::header::MessageCount message_count() const noexcept
Number of messages added since last reset().
std::string_view session() const noexcept
Returns the configured MoldUDP64 session from header.
Definition types.h:9
constexpr std::size_t length
Definition types.h:14
Definition types.h:7
Definition packet_builder.h:30
std::string_view session
MoldUDP64 Session, Must be 10 characters.
Definition packet_builder.h:32
std::size_t min_message_size
Smallest possible message size (including 2-byte length prefix); defaults to 14 (TotalView-ITCH 5....
Definition packet_builder.h:44
std::size_t MTU
MTU for packet, default is ethernet MTU (1500 bytes) - IP/UDP headers (28 bytes).
Definition packet_builder.h:34