Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
pcap_control_server.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <slick/queue.hpp>
6#include <slick/socket/tcp_server.h>
7// windows.h, which slick-socket's Windows server brings in after control_messages.hpp undid
8// it, defines ERROR again, turning FeedState::ERROR into FeedState::0.
9#ifdef ERROR
10#undef ERROR
11#endif
12// After slick-socket, so the logger this brings in leaves slick-socket's logging here as it was.
13#include <cli/cli_endpoint.hpp>
14#include <atomic>
15#include <cstdint>
16#include <optional>
17#include <string>
18#include <string_view>
19#include <unordered_set>
20
21namespace slick::sim::md_feed {
22
42class PcapControlServer : public slick::socket::TCPServerBase<PcapControlServer> {
43 using Server = slick::socket::TCPServerBase<PcapControlServer>;
44
45public:
47 PcapControlServer(std::string name, const slick::socket::TCPServerConfig &config, std::string cli_name);
49
51 bool start();
52 void stop();
53
55 [[nodiscard]] uint16_t listen_port() const noexcept { return Server::port(); }
56
59 static constexpr size_t MAX_MESSAGE = 512;
60
64 void publish_status(const FeedStatusMessage &status, std::string_view message);
65
67 [[nodiscard]] bool take_start() noexcept { return start_requested_.exchange(false, std::memory_order_acq_rel); }
68
70 [[nodiscard]] cli::CliEndpoint &cli() noexcept { return cli_; }
71
74 void serve_unstarted() { onPoll(); }
75
76 // slick::socket::TCPServerBase callbacks. Server thread only.
77 void onClientConnected(int client_id, const std::string &client_address);
78 void onClientDisconnected(int client_id);
79 void onClientData(int client_id, const uint8_t *data, size_t length);
80 void onPoll();
81
82private:
83 static constexpr uint32_t QUEUE_SLOTS = 1024;
84
85 struct StatusSlot {
86 uint8_t bytes[sizeof(FeedStatusMessage)];
87 uint16_t message_size;
88 char message[MAX_MESSAGE]; // only message_size of it is written
89 };
90
91 void request_start() noexcept { start_requested_.store(true, std::memory_order_release); }
93 void deliver();
94 bool serve_cli(const cli::Command &command, std::string &out);
95
96 std::string name_;
97 slick::queue<StatusSlot> outbox_{QUEUE_SLOTS}; // exchange thread -> server thread
98 std::atomic<bool> start_requested_{false}; // server thread -> exchange thread
99 cli::CliEndpoint cli_; // CLI server thread <-> server thread
100
101 // Server thread only.
102 uint64_t outbox_cursor_ = 0;
103 std::optional<StatusSlot> last_status_;
104 std::unordered_set<int> clients_;
105 ControlStreams streams_{sizeof(StartMessage)};
106};
107
108} // namespace slick::sim::md_feed
Where the CLI server's commands reach a thread that owns some state (the main thread,...
cli::CliEndpoint & cli() noexcept
The feed's CLI endpoint, served on the server thread while it runs.
void publish_status(const FeedStatusMessage &status, std::string_view message)
Sends status to every client now connected, and to each one that connects before the next.
void serve_unstarted()
After start() failed, so no server thread exists: serves the CLI from the calling thread,...
bool take_start() noexcept
True once, after a client has sent START. Exchange thread.
uint16_t listen_port() const noexcept
The bound port; with a configured port of 0, the one the OS assigned.
void onClientConnected(int client_id, const std::string &client_address)
void onClientData(int client_id, const uint8_t *data, size_t length)
bool start()
False when the port could not be bound.
static constexpr size_t MAX_MESSAGE
How much of a status's message the CLI reports.
PcapControlServer(std::string name, const slick::socket::TCPServerConfig &config, std::string cli_name)
cli_name is the CLI endpoint's (cli::pcap_feed_path).
The replay feed's state.