itch-mold-replay 1.0
Time-accurate NASDAQ TotalView-ITCH 5.0 replay over MoldUDP64
Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1#pragma once
2
3#include "imr/mold/packet_builder.h"
4#include "imr/util/memory_mapped_file.h"
5#include "imr/mold/retransmission_buffer.h"
6#include "imr/mold/retransmission/feed_pool.h"
7#include "imr/mold/downstream/feed.h"
8
9#include <thread>
10#include <memory>
11#include <expected>
12#include <algorithm>
13
14/**
15 *
16 * @code{.cpp}
17 * imr::Server::Config cfg{...};
18 *
19ndi * // construct directly with try/catch
20 *
21 * try
22 * {
23 * Server server(cfg);
24 * server.start();
25 * server.wait_for_downstream();
26 * }
27 * catch (const std::exception& ex)
28 * {
29 * std::println("{}", ex.what());
30 * }
31 *
32 * // or use helper that catches and returns std::expected
33 *
34 * const std::expected res{imr::make_server(cfg)};
35 * if (!res.has_value())
36 * {
37 * std::println(stderr, "{}", res.error());
38 * }
39 *
40 * std::unique_ptr<imr::Server> server{*res};
41 *
42 * server->start();
43 * server->wait_for_downstream();
44 * @endcode
45 *
46 */
47namespace imr
48{
49
50 /// A MoldUDP64 server that replays NASDAQ TotalView-ITCH 5.0 files.
51 class Server
52 {
53 public:
54 /**
55 * @defgroup config Configuration
56 * @brief Configuration structs for each component of the server.
57 */
58
59 /** Aggregate for configuration of all server components.
60 *
61 * @ingroup config
62 */
63 struct Config
64 {
68 /**
69 Capacity of the retransmission ring buffer in messages.
70
71 Power-of-two sizes use bitmasking for index lookup (faster); non-power-of-two sizes fall back to division.
72 */
73 std::size_t retransmission_buffer_size{1 << 22U};
75 /**
76 Number of retransmission feed worker threads.
77
78 Defaults to either 1 or one less than the hardware thread count for multicore systems to leave one thread for the downstream feed
79 */
80 std::size_t num_retransmission_feeds{std::max(std::thread::hardware_concurrency() - 1, 1U)};
81 };
82
83 /**
84 Constructs server ready to start
85
86 @throws std::invalid_arugment if configuration passed is invalid
87 @throws std::system_error if system calls to setup server fail
88 */
89 explicit Server(const Config& cfg);
90
91 /**
92 Starts downstream replay and retransmission feeds.
93
94 Downstreams and retransmission feeds run in background threads until downstream reaches end of file and finishes sending
95 downstream packets
96 */
97 void start();
98
99 /// Blocks caller until downstream replay finishes.
101
102 /// Stop all feeds early (before downstream reaches end of file).
103 void stop();
104
106
107 Server(const Server&) = delete;
108 Server(Server&&) = delete;
109
110 Server& operator=(const Server&) = delete;
111 Server& operator=(Server&&) = delete;
112
113 private:
114 util::MemoryMappedFile mapped_itch_file_;
115 mold::RetransmissionBuffer retransmission_buffer_;
116 mold::downstream::Feed downstream_feed_;
117 std::jthread downstream_thread_;
118 mold::retransmission::FeedPool retransmission_feeds_;
119
120 void join_downstream();
121 };
122
123 /**
124 Allows you to construct server without try/catch block to handle configuration errors.
125
126 At the moment just returns a string describing the error, so you can print and see what went wrong.
127 */
129
130}
A MoldUDP64 server that replays NASDAQ TotalView-ITCH 5.0 files.
Definition server.h:52
Server(const Config &cfg)
Constructs server ready to start.
void start()
Starts downstream replay and retransmission feeds.
Server & operator=(Server &&)=delete
Server & operator=(const Server &)=delete
void wait_for_downstream()
Blocks caller until downstream replay finishes.
Server(Server &&)=delete
Server(const Server &)=delete
void stop()
Stop all feeds early (before downstream reaches end of file).
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
Owns N (num_feeds) retransmission feeds.
Definition feed_pool.h:16
MoldUDP64 retransmission server.
Definition feed.h:18
RAII mmap file.
Definition memory_mapped_file.h:15
Definition feed.h:15
Definition file_descriptor.h:11
Definition feed.h:17
std::expected< std::unique_ptr< Server >, std::string > make_server(const Server::Config &cfg) noexcept
Allows you to construct server without try/catch block to handle configuration errors.
Aggregate for configuration of all server components.
Definition server.h:64
mold::PacketBuilder::Config packet_builder_cfg
Definition server.h:66
std::size_t retransmission_buffer_size
Capacity of the retransmission ring buffer in messages.
Definition server.h:73
util::MemoryMappedFile::Config mapped_itch_file_cfg
Definition server.h:65
std::size_t num_retransmission_feeds
Number of retransmission feed worker threads.
Definition server.h:80
mold::downstream::Feed::Config downstream_feed_config
Definition server.h:67
mold::retransmission::Feed::Config retransmission_feed_config
Definition server.h:74
Definition packet_builder.h:30
Definition memory_mapped_file.h:19