Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
message_parser.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common/messages.hpp>
4#include <common/order.hpp>
5#include <vector>
6#include <memory>
7#include <string>
8#include <optional>
9
11
12enum class ProtocolType {
13 FIX,
17};
18
20 std::optional<Order> order;
21 std::optional<OrderResponse> response;
22 std::string error_message;
23 bool is_complete_message = false;
24
25 bool has_order() const { return order.has_value(); }
26 bool has_response() const { return response.has_value(); }
27 bool has_error() const { return !error_message.empty(); }
28};
29
31public:
32 virtual ~MessageParser() = default;
33
34 virtual ProtocolType get_protocol_type() const = 0;
35
36 // Parse incoming message data, may be called multiple times for fragmented messages
37 virtual ParseResult parse_message(const uint8_t* data, size_t length, int client_id) = 0;
38
39 // Serialize order response back to client format
40 virtual std::vector<uint8_t> serialize_response(const OrderResponse& response) = 0;
41
42 // Check if this parser can handle the given data format
43 virtual bool can_handle(const uint8_t* data, size_t length) const = 0;
44
45 // Reset parser state (useful for stateful parsers)
46 virtual void reset() {}
47
48protected:
49 // Helper to convert protocol-specific fields to internal enums
50 static Side parse_side(const std::string& side_str);
51 static OrderType parse_order_type(const std::string& type_str);
52 static TimeInForce parse_time_in_force(const std::string& tif_str);
53
54 static std::string side_to_string(Side side);
55 static std::string order_type_to_string(OrderType type);
56 static std::string order_status_to_string(OrderStatus status);
57};
58
59// Factory for creating parsers
61public:
62 static std::unique_ptr<MessageParser> create_parser(ProtocolType type);
63
64 // Auto-detect protocol from message data
65 static std::unique_ptr<MessageParser> create_parser_from_data(const uint8_t* data, size_t length);
66
67 // Register custom parsers
68 template<typename ParserType>
69 static void register_parser() {
70 static_assert(std::is_base_of_v<MessageParser, ParserType>, "ParserType must inherit from MessageParser");
71 // Implementation would store parser creation function
72 }
73};
74
75} // namespace slick::sim::order_gateway
static std::unique_ptr< MessageParser > create_parser(ProtocolType type)
static std::unique_ptr< MessageParser > create_parser_from_data(const uint8_t *data, size_t length)
virtual ProtocolType get_protocol_type() const =0
static std::string side_to_string(Side side)
static TimeInForce parse_time_in_force(const std::string &tif_str)
static std::string order_status_to_string(OrderStatus status)
virtual bool can_handle(const uint8_t *data, size_t length) const =0
static Side parse_side(const std::string &side_str)
static OrderType parse_order_type(const std::string &type_str)
virtual std::vector< uint8_t > serialize_response(const OrderResponse &response)=0
static std::string order_type_to_string(OrderType type)
virtual ParseResult parse_message(const uint8_t *data, size_t length, int client_id)=0
std::optional< OrderResponse > response