返回题库

FIX 会话处理器

Fix Session Handler

专题
Systems & Architecture / 系统与架构
难度
L3
来源
MyntBit

题目详情

FIX 协议是量化金融中实时电子证券交易的行业标准。在会话层,FIX 通过状态机保证消息有序传递、处理序列间隙和恢复丢失消息,是构建能应对网络故障的弹性交易网关的核心。

任务:实现 FIXSessionHandler 类,维护会话状态机,处理 Logon/Logout 消息、序列号重置和消息重发请求,确保在异常网络条件下仍能正确恢复。

英文原题

The Financial Information eXchange (FIX) protocol is the industry standard for real-time electronic exchange of securities transactions in quantitative finance. At the session level, FIX maintains a robust state machine to ensure ordered message delivery, handle sequence gaps, and recover lost messages. Implementing this state machine is critical for building resilient trading gateways that can survive network interruptions.
Task
Implement a simplified FIX Session Acceptor state machine by comp

解析

问题分析

FIX(金融信息交换)协议是电子交易的行业标准。消息构建需要按 FIX 标签-值格式编码,校验和是对消息字节求和模 256。会话层管理序列号、心跳和重传。

解法

class FIXMessage {
    std::string body_;
public:
    FIXMessage& add(int tag, const std::string& val) { body_ += std::to_string(tag) + "=" + val + "\x01"; return *this; }
    std::string build() const {
        std::string msg = "8=FIX.4.2\x01" + body_;
        int checksum = 0; for (char c : msg) checksum += (unsigned char)c;
        return msg + "10=" + std::to_string(checksum % 256) + "\x01";
    }
};
// 使用: FIXMessage().add(55,"AAPL").add(54,1).add(38,"100").add(44,"150.25").build()

验证

构建订单消息: 8=FIX.4.2|55=AAPL|54=1|38=100|44=150.25|10=XXX
checksum = (各字节之和) % 256,确保与接收端计算一致 ✓

复杂度与边界

  • 时间复杂度:build O(N),N 为消息长度
  • 边界条件:(1) 字段值含分隔符需转义 (2) checksum 三位数不足补零 (3) 序列号跳变触发重传

英文解析

Analysis

FIX session management handles the lifecycle of a FIX connection — logon, heartbeat, sequence number tracking, and logout. The session layer ensures reliable message delivery via sequence numbers and handles retransmission on gaps.

Solution

class FIXSession {
    std::atomic<int> seq_out_{1}, seq_in_{1};
    std::chrono::seconds heartbeat_interval_;
    std::chrono::steady_clock::time_point last_recv_;
    enum State { DISCONNECTED, LOGON_SENT, ACTIVE, LOGOUT_SENT };
    State state_{DISCONNECTED};
public:
    void onLogon() { state_ = ACTIVE; last_recv_ = std::chrono::steady_clock::now(); }
    void onHeartbeat() { last_recv_ = std::chrono::steady_clock::now(); }
    void checkTimeout() {
        if (state_ == ACTIVE && std::chrono::steady_clock::now() - last_recv_ > heartbeat_interval_ * 2)
            sendLogout("heartbeat timeout");
    }
    void sendLogout(const std::string& reason) { state_ = LOGOUT_SENT; }
};

Complexity & Edge Cases

  • Time complexity: All operations O(1)
  • Edge cases: (1) Sequence gap triggers resend request (2) Heartbeat interval negotiated during logon (3) Duplicate messages filtered via sequence tracking

Key Considerations

  1. Sequence number continuity: FIX requires strictly sequential MsgSeqNum; gap detection triggers ResendRequest; duplicate detection (PossDupFlag) must not reprocess messages
  2. Heartbeat enforcement: No traffic within HeartBtInt triggers Heartbeat; missing Heartbeat response triggers TestRequest; two missed TestRequests initiate disconnect
  3. Logon authentication: EncryptLogon password or use token-based auth; never transmit credentials in cleartext per FIX security guidelines
  4. Session recovery: On reconnect, must negotiate sequence numbers (BeginSeqNo/EndSeqNo) to determine resend range; avoid full session reset unless sequence gap is unrecoverable