Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
cli_server.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "cli_endpoint.hpp"
6#include <slick/socket/tcp_server.h>
7#include <atomic>
8#include <chrono>
9#include <cstdint>
10#include <deque>
11#include <map>
12#include <string>
13#include <unordered_map>
14#include <utility>
15#include <vector>
16
17namespace slick::sim::cli {
18
37class CliServer : public slick::socket::TCPServerBase<CliServer> {
38 using Server = slick::socket::TCPServerBase<CliServer>;
39
40public:
41 static constexpr uint16_t DEFAULT_PORT = 17400;
42 static constexpr std::chrono::milliseconds DEFAULT_REPLY_TIMEOUT{2000};
44 static constexpr std::chrono::seconds DEFAULT_IDLE_TIMEOUT{300};
45
46 explicit CliServer(const slick::socket::TCPServerConfig &config,
47 std::chrono::milliseconds reply_timeout = DEFAULT_REPLY_TIMEOUT);
48 ~CliServer();
49
52 void add_endpoints(const std::string &group, const std::vector<CliEndpoint *> &endpoints);
53
55 void set_feeds(std::string feeds) { feeds_ = std::move(feeds); }
57 void set_version(std::string version) { version_ = std::move(version); }
58
60 bool start();
61 void stop();
62
65 void flush(std::chrono::milliseconds limit);
66
68 [[nodiscard]] uint16_t listen_port() const noexcept { return Server::port(); }
69
70 // slick::socket::TCPServerBase callbacks. Server thread only.
71 void onClientConnected(int client_id, const std::string &client_address);
72 void onClientDisconnected(int client_id);
73 void onClientData(int client_id, const uint8_t *data, size_t length);
74 void onPoll();
75
76private:
78 static constexpr size_t MAX_WAITING = 64;
79
80 struct Answer {
81 size_t endpoint;
82 bool answered = false;
84 std::string text;
85 };
86 struct Pending {
87 int client_id;
88 uint32_t request_id;
89 std::vector<Answer> answers;
90 size_t remaining;
91 std::chrono::steady_clock::time_point deadline;
92 };
93 struct Client {
94 std::deque<CliRequestMessage> waiting;
95 uint64_t ticket = 0;
96 };
97
101 void dispatch(int client_id);
102 void on_reply(size_t endpoint, uint64_t ticket, CliStatus status, std::string_view text);
104 void finish(uint64_t ticket);
105 void send_reply(int client_id, uint32_t request_id, CliStatus status, std::string_view text);
106 [[nodiscard]] std::string list_targets() const;
107
108 std::chrono::milliseconds reply_timeout_;
110 std::vector<CliEndpoint *> endpoints_;
112 std::map<std::string, std::vector<size_t>, std::less<>> targets_;
114 std::string feeds_;
115 std::string version_;
116
117 // Server thread only.
118 ControlStreams streams_{sizeof(CliRequestMessage)};
119 std::unordered_map<int, Client> clients_;
120 std::unordered_map<uint64_t, Pending> pending_;
121 uint64_t next_ticket_ = 1;
122 std::vector<uint8_t> reply_;
123 std::vector<uint64_t> expired_;
125 std::atomic<size_t> pending_count_{0};
126};
127
128} // namespace slick::sim::cli
uint16_t listen_port() const noexcept
The bound port; with a configured port of 0, the one the OS assigned.
void add_endpoints(const std::string &group, const std::vector< CliEndpoint * > &endpoints)
Routes group, and every /-prefix of each endpoint's name, to the endpoints.
bool start()
False when the port could not be bound.
void set_feeds(std::string feeds)
What LIST_FEEDS is answered with (exch::list_feeds). Before start().
static constexpr uint16_t DEFAULT_PORT
static constexpr std::chrono::milliseconds DEFAULT_REPLY_TIMEOUT
void onClientDisconnected(int client_id)
static constexpr std::chrono::seconds DEFAULT_IDLE_TIMEOUT
For TCPServerConfig::idle_timeout, so dead connections can't hold every slot.
void onClientConnected(int client_id, const std::string &client_address)
CliServer(const slick::socket::TCPServerConfig &config, std::chrono::milliseconds reply_timeout=DEFAULT_REPLY_TIMEOUT)
void flush(std::chrono::milliseconds limit)
Waits up to limit for the replies still owed to clients to go out, so the answer to a SHUTDOWN gets s...
void set_version(std::string version)
What GET_VERSION is answered with (main.cpp's SLICK_SIM_VERSION). Before start().
void onClientData(int client_id, const uint8_t *data, size_t length)
The CLI commands that switch a gateway's business rejects per ID: REJECT_LOGON, REJECT_NEW_ORDERS and...