FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
fix_messages.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include "fix/fix_codec.hpp"
15#include "fix/fix_tags.hpp"
16#include "base/config.hpp"
17
18namespace fix40 {
19
33inline FixMessage create_logon_message(const std::string& sender,
34 const std::string& target,
35 int seq_num = 1,
36 int heart_bt = Config::instance().get_int("fix_session", "default_heartbeat_interval", 30),
37 bool reset_seq_num = false) {
38 FixMessage logon;
39 logon.set(tags::MsgType, "A");
40 logon.set(tags::EncryptMethod, "0");
41 logon.set(tags::HeartBtInt, heart_bt);
42 logon.set(tags::SenderCompID, sender);
43 logon.set(tags::TargetCompID, target);
44 logon.set(tags::MsgSeqNum, seq_num);
45 if (reset_seq_num) {
46 logon.set(tags::ResetSeqNumFlag, "Y");
47 }
48 return logon;
49}
50
66inline FixMessage create_heartbeat_message(const std::string& sender,
67 const std::string& target,
68 int seq_num,
69 const std::string& test_req_id = "") {
70 FixMessage hb;
71 hb.set(tags::MsgType, "0");
72 hb.set(tags::SenderCompID, sender);
73 hb.set(tags::TargetCompID, target);
74 hb.set(tags::MsgSeqNum, seq_num);
75
76 if (!test_req_id.empty()) {
77 hb.set(tags::TestReqID, test_req_id);
78 }
79
80 return hb;
81}
82
97inline FixMessage create_test_request_message(const std::string& sender,
98 const std::string& target,
99 int seq_num,
100 const std::string& test_req_id) {
101 FixMessage tr;
102 tr.set(tags::MsgType, "1");
103 tr.set(tags::SenderCompID, sender);
104 tr.set(tags::TargetCompID, target);
105 tr.set(tags::MsgSeqNum, seq_num);
106 tr.set(tags::TestReqID, test_req_id);
107 return tr;
108}
109
124inline FixMessage create_logout_message(const std::string& sender,
125 const std::string& target,
126 int seq_num,
127 const std::string& text = "") {
128 FixMessage lo;
129 lo.set(tags::MsgType, "5");
130 lo.set(tags::SenderCompID, sender);
131 lo.set(tags::TargetCompID, target);
132 lo.set(tags::MsgSeqNum, seq_num);
133 if (!text.empty()) {
134 lo.set(tags::Text, text);
135 }
136 return lo;
137}
138
155inline FixMessage create_resend_request_message(const std::string& sender,
156 const std::string& target,
157 int seq_num,
158 int begin_seq_no,
159 int end_seq_no) {
160 FixMessage rr;
161 rr.set(tags::MsgType, "2");
162 rr.set(tags::SenderCompID, sender);
163 rr.set(tags::TargetCompID, target);
164 rr.set(tags::MsgSeqNum, seq_num);
165 rr.set(tags::BeginSeqNo, begin_seq_no);
166 rr.set(tags::EndSeqNo, end_seq_no);
167 return rr;
168}
169
187inline FixMessage create_sequence_reset_message(const std::string& sender,
188 const std::string& target,
189 int seq_num,
190 int new_seq_no,
191 bool gap_fill = true) {
192 FixMessage sr;
193 sr.set(tags::MsgType, "4");
194 sr.set(tags::SenderCompID, sender);
195 sr.set(tags::TargetCompID, target);
196 sr.set(tags::MsgSeqNum, seq_num);
197 sr.set(tags::NewSeqNo, new_seq_no);
198 sr.set(tags::GapFillFlag, gap_fill ? "Y" : "N");
199 return sr;
200}
201
209inline bool is_admin_message(const std::string& msg_type) {
210 return msg_type == "0" || // Heartbeat
211 msg_type == "1" || // TestRequest
212 msg_type == "2" || // ResendRequest
213 msg_type == "4" || // SequenceReset
214 msg_type == "5" || // Logout
215 msg_type == "A"; // Logon
216}
217
218} // namespace fix40
static Config & instance()
获取 Config 单例实例
Definition config.cpp:13
FIX 消息的面向对象封装
Definition fix_codec.hpp:46
void set(int tag, const std::string &value)
设置字符串类型字段
Definition fix_codec.hpp:53
INI 格式配置文件解析器
FIX 消息编解码器
FIX 4.0 协议标签定义
constexpr int GapFillFlag
GapFill 标志 (Gap Fill Flag),用于 SequenceReset Y = Gap Fill 模式,N = Reset 模式
Definition fix_tags.hpp:167
constexpr int BeginSeqNo
起始序列号 (Begin Sequence Number),用于 ResendRequest
Definition fix_tags.hpp:157
constexpr int MsgSeqNum
消息序列号
Definition fix_tags.hpp:42
constexpr int SenderCompID
发送方标识符
Definition fix_tags.hpp:36
constexpr int TestReqID
测试请求标识符
Definition fix_tags.hpp:62
constexpr int ResetSeqNumFlag
重置序列号标志 (Reset Sequence Number Flag) Y = 双方都将会话序列号重置为 1(通常在一方丢失持久化状态时使用)
Definition fix_tags.hpp:59
constexpr int TargetCompID
接收方标识符
Definition fix_tags.hpp:39
constexpr int HeartBtInt
心跳间隔(秒)
Definition fix_tags.hpp:55
constexpr int Text
文本消息(用于 Logout 原因等)
Definition fix_tags.hpp:65
constexpr int NewSeqNo
新序列号 (New Sequence Number),用于 SequenceReset
Definition fix_tags.hpp:163
constexpr int MsgType
消息类型(如 "A"=Logon, "0"=Heartbeat, "5"=Logout)
Definition fix_tags.hpp:33
constexpr int EncryptMethod
加密方法(0=无加密)
Definition fix_tags.hpp:52
constexpr int EndSeqNo
结束序列号 (End Sequence Number),用于 ResendRequest
Definition fix_tags.hpp:160
Definition matching_engine.hpp:23
bool is_admin_message(const std::string &msg_type)
判断消息类型是否为管理消息
Definition fix_messages.hpp:209
FixMessage create_logon_message(const std::string &sender, const std::string &target, int seq_num=1, int heart_bt=Config::instance().get_int("fix_session", "default_heartbeat_interval", 30), bool reset_seq_num=false)
创建 Logon 消息
Definition fix_messages.hpp:33
FixMessage create_test_request_message(const std::string &sender, const std::string &target, int seq_num, const std::string &test_req_id)
创建 TestRequest 消息
Definition fix_messages.hpp:97
FixMessage create_heartbeat_message(const std::string &sender, const std::string &target, int seq_num, const std::string &test_req_id="")
创建 Heartbeat 消息
Definition fix_messages.hpp:66
FixMessage create_logout_message(const std::string &sender, const std::string &target, int seq_num, const std::string &text="")
创建 Logout 消息
Definition fix_messages.hpp:124
FixMessage create_resend_request_message(const std::string &sender, const std::string &target, int seq_num, int begin_seq_no, int end_seq_no)
创建 ResendRequest 消息
Definition fix_messages.hpp:155
FixMessage create_sequence_reset_message(const std::string &sender, const std::string &target, int seq_num, int new_seq_no, bool gap_fill=true)
创建 SequenceReset 消息
Definition fix_messages.hpp:187