Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
ws_md_publisher.cpp
Go to the documentation of this file.
1#include <slick/logger.hpp>
2#include "ws_md_publisher.hpp"
3
4using namespace slick::sim;
5using namespace slick::sim::md_publisher;
6
8 Venue venue,
9 slick::queue<Request>& request_queue,
10 slick::queue<uint8_t>& market_data_queue,
11 uint32_t port
12)
13 : MarketDataPublisher(venue, request_queue, market_data_queue)
14 , port_(port)
15{
16}
17
19 running_.store(true, std::memory_order_release);
20 // Start WebSocket server thread (handles everything in event loop)
21 ws_thread_ = std::thread([this]() {
22 auto app = uWS::App();
23 setup_routes(app);
24 app.listen(port_, [this](auto *listen_socket) {
25 listen_socket_ = listen_socket;
26 if (listen_socket) {
27 LOG_INFO("{} Publisher started on port {}", to_string(venue_), port_);
28
29 // Get the event loop
30 loop_ = uWS::Loop::get();
31
32 // Create heartbeat timer in the event loop
33 // Use defer() to create timer with proper lambda capture
34 loop_->defer([this]() {
35 // The third argument is ext_size, not a flag: it is the number
36 // of bytes us_timer_ext() hands back. Passing 0 and then
37 // writing a pointer there overruns the timer allocation.
38 heartbeat_timer_ = us_create_timer((struct us_loop_t*)loop_, 0,
40
41 // Store 'this' pointer in timer extension data
42 *((WebsocketMarketDataPublisher**)us_timer_ext(heartbeat_timer_)) = this;
43
44 // Set timer callback that retrieves 'this' from extension data
45 us_timer_set(heartbeat_timer_, [](struct us_timer_t *timer) {
46 // Dereference the pointer stored in extension data
47 auto* self = *((WebsocketMarketDataPublisher**)us_timer_ext(timer));
48 self->check_heartbeats();
50 });
51
52 LOG_INFO("{} Publisher Event loop configured (heartbeat: {}ms)", to_string(venue_), ping_interval_ms_);
53
54 // Start continuous response processing
56
57 } else {
58 LOG_ERROR("{} Publisher Failed to listen on port {}", to_string(venue_), port_);
59 }
60 })
61 .run();
62 });
63}
64
66 running_.store(false, std::memory_order_release);
67
68 // Close heartbeat timer
69 if (heartbeat_timer_) {
70 us_timer_close(heartbeat_timer_);
71 heartbeat_timer_ = nullptr;
72 }
73
74 if (listen_socket_ && loop_) {
75 // Closing the listen socket must happen on the loop's own thread;
76 // doing it cross-thread races with the loop's poll handles.
77 loop_->defer([this]() {
78 if (listen_socket_) {
79 us_listen_socket_close(0, listen_socket_);
80 listen_socket_ = nullptr;
81 }
82 });
83 }
84
85 if (ws_thread_.joinable()) {
86 ws_thread_.join();
87 }
88}
89
91 // Bound each drain so socket writes and heartbeats get time on the loop.
92 // Each batch costs one defer; raising the limit from 100 to 1000 reduces
93 // scheduling overhead during a flood. Snapshot frames end a batch early
94 // because they cost much more to encode than incremental updates.
95 constexpr int max_batch = 1000;
96 int processed = 0;
97
98 while (processed < max_batch) {
99 // Try to read from the queue (non-blocking)
100 auto [data, size] = md_queue_.read(data_cursor_);
101
102 if (data == nullptr) {
103 // No more data available
104 break;
105 }
106
107 auto *update = reinterpret_cast<MarketDataUpdate*>(data);
109
110 if (update->type == MDUpdateType::SUB_RESPONSE ||
111 update->type == MDUpdateType::BOOK_SNAPSHOT) {
112 break; // A whole book went out - yield rather than start another
113 }
114
115 ++processed;
116 }
117
118 // Reschedule even when the queue is empty. This defer is the only way
119 // publish_processing() runs again: md_queue_ has no wakeup mechanism and
120 // the exchange thread never touches this loop. Skipping it would stop the
121 // publisher permanently once it catches up.
122 // schedule_publish_processing() checks running_.
124}
125
127 if (!running_.load(std::memory_order_acquire) || !loop_) {
128 return;
129 }
130
131 loop_->defer([this]() {
133 });
134}
MarketDataPublisher(Venue venue, slick::queue< Request > &request_queue, slick::queue< uint8_t > &md_queue)
virtual void publish_market_data_update(MarketDataUpdate *update)=0
WebsocketMarketDataPublisher(Venue venue, slick::queue< Request > &request_queue, slick::queue< uint8_t > &market_data_queue, uint32_t port)
constexpr const char * to_string(FeedState state) noexcept