|
Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
|
The transport half of a live multicast feed: one receiver thread that polls every joined group ("line"), handing the packets to the exchange thread through one lock-free queue. More...
#include <md_feed/multicast_packet_feed.hpp>
Public Member Functions | |
| MulticastPacketFeed (std::string name, const nlohmann::json &config) | |
| virtual | ~MulticastPacketFeed () |
| MulticastPacketFeed (const MulticastPacketFeed &)=delete | |
| MulticastPacketFeed & | operator= (const MulticastPacketFeed &)=delete |
| bool | has_group (const char *key) const |
| True when the config names a group for key. | |
| bool | add_line (const char *key, uint8_t line) |
| Joins the group under key, if the config names one, as line. | |
| void | start () |
| void | stop () |
| bool | deliver (uint8_t line, const uint8_t *data, size_t size) |
| Queues a packet as if line's receiver had received it. | |
| void | mute (uint8_t line, bool muted) noexcept |
| Drops line's packets on arrival while muted. Any thread. | |
| void | set_joined (uint8_t line, bool joined, uint64_t now_ns) |
| Exchange thread, from poll(). | |
| template<typename OnPacket> | |
| uint32_t | drain (OnPacket &&on_packet) |
| Exchange thread. | |
| const std::string & | name () const noexcept |
| uint64_t | loss_count () const noexcept |
| Queue bytes a receiver overwrote before this thread drained them, each lost packet counting its size plus the line byte. | |
| uint64_t | receive_error_count () const noexcept |
| Reads a socket reported as failed, across every line. | |
| uint64_t | oversized_count () const noexcept |
| Packets refused for being larger than the queue. Only the first is logged. | |
Static Public Attributes | |
| static constexpr uint8_t | MAX_LINES = 32 |
| static constexpr uint64_t | MEMBERSHIP_RETRY_NS = 100'000'000 |
| How long a failed join or leave waits before set_joined() tries it again. | |
| static constexpr size_t | MAX_DATAGRAM = 1u << 16 |
| The receiver thread's read buffer: above any UDP payload a venue sends. | |
| static constexpr std::chrono::microseconds | RECEIVER_IDLE_SLEEP {50} |
Protected Member Functions | |
| virtual int | change_membership (uint8_t line, bool join) |
| Joins or leaves line's group. | |
Static Protected Attributes | |
| static constexpr int | MEMBERSHIP_PENDING = -1 |
| What change_membership() returns while the receiver thread has not yet answered. | |
Friends | |
| struct | MulticastPacketFeedTestAccess |
The transport half of a live multicast feed: one receiver thread that polls every joined group ("line"), handing the packets to the exchange thread through one lock-free queue.
Nothing here takes a lock. Only the exchange thread touches the book, as it drains the queue into the venue's protocol handler.
A line is a small integer below MAX_LINES that the venue chooses (its enum). The venue can leave a line's group while it has no use for it, such as a snapshot loop while the book is live, and rejoin it on a gap (set_joined).
Header-only, so the md_feed target stays free of slick-socket: a venue that includes this links slick::socket itself.
Config keys, read from the feed's config entry: queue_size (bytes, default 16 MiB), max_packets_per_poll (256), interface and receive_buffer_size for every receiver. Each line's group is { "address", "port" } under its key.
receive_buffer_size (bytes, default 64 MiB) is each socket's SO_RCVBUF: what the kernel holds while the receiver thread is descheduled. The kernel drops a burst beyond it, so size it for the stall, not for one packet. The read buffer is separate and fixed (MAX_DATAGRAM). The kernel caps it at net.core.rmem_max (Linux), so raise that too. receiver_cpu (default -1) pins the receiver thread to that core. It busy-polls its sockets, so a pinned thread keeps the core at 100%; an unpinned one sleeps RECEIVER_IDLE_SLEEP between empty sweeps instead. A pin that cannot be made (no such core, outside the process's cpuset, an unsupported platform) is logged and leaves the thread unpinned, so it sleeps too rather than spin on a core it does not own. A receiver_cpu that is not a number throws from the constructor, like queue_size.
The queue is fixed and lossy, like every slick::queue: should the exchange thread fall far enough behind, a receiver overwrites packets nobody has drained, and loss_count() counts their bytes. The venue's own sequence numbers are what turn that into a gap it can recover from. A packet is also held back from the queue until the receiver that reserved its slot has copied it in, so one stalled copy delays the packets behind it - never reorders them.
Definition at line 80 of file multicast_packet_feed.hpp.
|
inline |
Definition at line 89 of file multicast_packet_feed.hpp.
References name().
Referenced by MulticastPacketFeed(), and operator=().
|
inlinevirtual |
Definition at line 104 of file multicast_packet_feed.hpp.
References stop().
|
delete |
References MulticastPacketFeed().
|
inline |
Joins the group under key, if the config names one, as line.
Call before start(). Returns whether a receiver was added.
Definition at line 114 of file multicast_packet_feed.hpp.
References MAX_LINES.
|
inlineprotectedvirtual |
Joins or leaves line's group.
Returns 0 once the socket is as asked, an error, or MEMBERSHIP_PENDING: the change is under way and the same call is to be made again. Virtual so a test can make it fail; only called when the membership has to change, or a change handed to the receiver thread is still in flight.
A socket is only ever touched by the receiver thread, so the change is handed to it, and each call looks for its answer without waiting, so the exchange thread cannot be held up by a receiver that is descheduled. A change that was handed over and is no longer wanted is withdrawn if the thread has not taken it yet, so it is never made behind the caller's back; one the thread has begun is waited out across calls, then followed by the one now wanted. EIO is a join the thread could not make; slick-socket logs the socket error behind it.
Definition at line 308 of file multicast_packet_feed.hpp.
References MEMBERSHIP_PENDING.
Referenced by set_joined().
|
inline |
Queues a packet as if line's receiver had received it.
Any thread. False when the packet is dropped: the line is muted, or the packet cannot fit the queue.
The second case has to be refused here. slick::queue::reserve() throws above the queue's size, and this runs on a receiver thread, where an escaping exception is std::terminate - a queue_size smaller than one datagram would otherwise take the process down.
Definition at line 178 of file multicast_packet_feed.hpp.
References MAX_LINES.
|
inline |
Exchange thread.
Hands at most max_packets_per_poll packets to on_packet(line, data, size), pointing into the queue's memory. The cap keeps a busy feed from starving client requests. Returns how many it handed over.
Definition at line 261 of file multicast_packet_feed.hpp.
|
inlinenodiscard |
True when the config names a group for key.
Definition at line 110 of file multicast_packet_feed.hpp.
|
inlinenodiscardnoexcept |
Queue bytes a receiver overwrote before this thread drained them, each lost packet counting its size plus the line byte.
Non-zero means packets were lost, and the venue's sequence numbers are the only thing that can repair the gap.
Definition at line 281 of file multicast_packet_feed.hpp.
|
inlinenoexcept |
Drops line's packets on arrival while muted. Any thread.
Definition at line 202 of file multicast_packet_feed.hpp.
References MAX_LINES.
Referenced by set_joined().
|
inlinenodiscardnoexcept |
Definition at line 276 of file multicast_packet_feed.hpp.
Referenced by MulticastPacketFeed(), and MulticastPacketFeedTestAccess.
|
delete |
References MulticastPacketFeed().
|
inlinenodiscardnoexcept |
Packets refused for being larger than the queue. Only the first is logged.
Definition at line 290 of file multicast_packet_feed.hpp.
|
inlinenodiscardnoexcept |
Reads a socket reported as failed, across every line.
Logged at most once a second per line, so a non-zero count that grows is a socket that is broken, not a quiet feed.
Definition at line 285 of file multicast_packet_feed.hpp.
|
inline |
Exchange thread, from poll().
Leaves or rejoins line's group, so the network stops sending what nobody reads. A line not wanted is muted at once, which also drops what is already in the socket buffer. The membership counts as changed only once the socket call succeeds: a failed one is logged once and tried again every MEMBERSHIP_RETRY_NS until it does, so recovery never waits on a group that was not rejoined. The receiver thread makes the change itself, as its sockets are its own: this hands it over and returns, then finds the answer on a later call (see change_membership), so the exchange thread never waits for it. Two compares when nothing is pending, so it is cheap to call every poll. Before start(), or for a line with no group, only the mute changes.
Definition at line 222 of file multicast_packet_feed.hpp.
References change_membership(), MAX_LINES, MEMBERSHIP_PENDING, MEMBERSHIP_RETRY_NS, and mute().
|
inline |
Definition at line 134 of file multicast_packet_feed.hpp.
|
inline |
Definition at line 160 of file multicast_packet_feed.hpp.
Referenced by ~MulticastPacketFeed().
|
friend |
Definition at line 350 of file multicast_packet_feed.hpp.
References MulticastPacketFeedTestAccess, and name().
Referenced by MulticastPacketFeedTestAccess.
|
staticconstexpr |
The receiver thread's read buffer: above any UDP payload a venue sends.
Definition at line 86 of file multicast_packet_feed.hpp.
|
staticconstexpr |
Definition at line 82 of file multicast_packet_feed.hpp.
Referenced by add_line(), deliver(), mute(), and set_joined().
|
staticconstexprprotected |
What change_membership() returns while the receiver thread has not yet answered.
Definition at line 294 of file multicast_packet_feed.hpp.
Referenced by change_membership(), and set_joined().
|
staticconstexpr |
How long a failed join or leave waits before set_joined() tries it again.
Definition at line 84 of file multicast_packet_feed.hpp.
Referenced by set_joined().
|
staticconstexpr |
Definition at line 87 of file multicast_packet_feed.hpp.