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/retransmission_buffer.h"
4#include "imr/util/file_descriptor.h"
5#include "imr/util/zstring_view.h"
6#include "imr/mold/packet_builder.h"
7
8#include <array>
9
10#include <netinet/in.h>
11#include <sys/socket.h>
12#include <sys/epoll.h>
13
14namespace imr::mold::retransmission
15{
16 /// MoldUDP64 retransmission server.
17 class Feed
18 {
19 public:
20 /// @ingroup config
21 struct Config
22 {
23 /// Address to bind the retransmission socket to
25 /// Port to bind to. Pass 0 to let the OS assign an ephemeral port.
27 };
28 /** Constructs and binds the retransmission socket.
29 *
30 * @param shutdown_fd fd polled alongside the socket; writing to this will cause `start()` to exit stopping the event loop.
31 * Must be > 0 (0 is reserved for stdin, which epoll rejects with EPERM).
32 *
33 * @throws std::invalid_argument if shutdown_fd <= 0, or if cfg.address is not valid IPv4.
34 *
35 * @throws std::system_error if network resource creation / config fails
36 */
37 explicit Feed(const Config& cfg,
38 const PacketBuilder::Config& packet_builder_cfg,
39 std::span<const char> file,
40 const RetransmissionBuffer& retransmission_buffer,
41 int shutdown_fd);
42 /** Runs the event loop, blocking until shutdown_fd becomes readable.
43 * Dispatches incoming client requests to handle_request().
44 */
45 void start();
46
47 private:
48 util::FileDescriptor socket_{[] { return socket(AF_INET, SOCK_DGRAM, 0); }};
49 util::FileDescriptor epoll_fd_{[] { return epoll_create1(0); }};
50 int shutdown_fd_;
51 /**
52 * @tparam N = 2, 1 for shutdown_fd_, 1 for request
53 */
54 std::array<epoll_event, 2> epoll_events_{};
55
56 std::array<char, types::header::length> recv_buffer_{};
57 PacketBuilder packet_builder_;
58
59 std::span<const char> file_;
60 const RetransmissionBuffer* retransmission_buffer_;
61
62 void handle_request(int client_fd);
63 struct RequestContext
64 {
65 sockaddr_in client_address;
66 types::header::SequenceNumber starting_sequence;
67 types::header::MessageCount msg_count;
68 std::size_t file_position_for_retransmission;
69 };
70 std::optional<RequestContext> parse_request(sockaddr_in&& client_addr);
71
72 void build_packet(const RequestContext& ctx);
73
74 void send_packet(const sockaddr_in& client_address);
75
76 void configure_socket(const Config& cfg);
77 };
78}
Builds MoldUDP64 packets.
Definition packet_builder.h:24
Retransmission buffer of the last buffer_size messages.
Definition retransmission_buffer.h:15
MoldUDP64 retransmission server.
Definition feed.h:18
void start()
Runs the event loop, blocking until shutdown_fd becomes readable.
Feed(const Config &cfg, const PacketBuilder::Config &packet_builder_cfg, std::span< const char > file, const RetransmissionBuffer &retransmission_buffer, int shutdown_fd)
Constructs and binds the retransmission socket.
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 feed.h:15
Definition types.h:9
constexpr std::size_t length
Definition types.h:14
Definition types.h:7
Definition file_descriptor.h:11
Definition packet_builder.h:30
util::zstring_view address
Address to bind the retransmission socket to.
Definition feed.h:24
std::uint16_t port
Port to bind to. Pass 0 to let the OS assign an ephemeral port.
Definition feed.h:26