LCOV - code coverage report
Current view: top level - src/client - client_app.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 1 1
Test Date: 2025-12-19 03:13:09 Functions: 50.0 % 2 1

            Line data    Source code
       1              : /**
       2              :  * @file client_app.hpp
       3              :  * @brief 客户端 FIX Application 实现
       4              :  *
       5              :  * 处理 FIX 消息的发送和接收,更新客户端状态。
       6              :  */
       7              : 
       8              : #pragma once
       9              : 
      10              : #include "fix/application.hpp"
      11              : #include "fix/fix_codec.hpp"
      12              : #include "fix/session.hpp"
      13              : #include "client_state.hpp"
      14              : #include <memory>
      15              : #include <atomic>
      16              : #include <string>
      17              : #include <cstdint>
      18              : 
      19              : namespace fix40::client {
      20              : 
      21              : /**
      22              :  * @class ClientApp
      23              :  * @brief 客户端 FIX Application
      24              :  *
      25              :  * 实现 Application 接口,处理:
      26              :  * - 登录/登出
      27              :  * - 执行报告 (8)
      28              :  * - 资金查询响应 (U2)
      29              :  * - 持仓查询响应 (U4)
      30              :  * - 账户推送 (U5)
      31              :  * - 持仓推送 (U6)
      32              :  * - 合约搜索响应 (U8)
      33              :  * - 订单历史查询响应 (U10)
      34              :  */
      35              : class ClientApp : public Application {
      36              : public:
      37              :     /**
      38              :      * @brief 构造函数
      39              :      * @param state 客户端状态管理器
      40              :      * @param userId 用户ID(作为 SenderCompID)
      41              :      */
      42              :     ClientApp(std::shared_ptr<ClientState> state, const std::string& userId);
      43              : 
      44            2 :     ~ClientApp() override = default;
      45              : 
      46              :     // =========================================================================
      47              :     // Application 接口实现
      48              :     // =========================================================================
      49              : 
      50              :     void onLogon(const SessionID& sessionID) override;
      51              :     void onLogout(const SessionID& sessionID) override;
      52              :     void fromApp(const FixMessage& msg, const SessionID& sessionID) override;
      53              :     void toApp(FixMessage& msg, const SessionID& sessionID) override;
      54              : 
      55              :     // =========================================================================
      56              :     // 业务操作
      57              :     // =========================================================================
      58              : 
      59              :     /**
      60              :      * @brief 设置 Session
      61              :      */
      62              :     void setSession(std::shared_ptr<Session> session);
      63              : 
      64              :     /**
      65              :      * @brief 发送新订单
      66              :      * @param symbol 合约代码
      67              :      * @param side "1"=买, "2"=卖
      68              :      * @param qty 数量
      69              :      * @param price 价格(限价单)
      70              :      * @param ordType "1"=市价, "2"=限价
      71              :      * @return 客户端订单ID
      72              :      */
      73              :     std::string sendNewOrder(const std::string& symbol, const std::string& side,
      74              :                              int64_t qty, double price, const std::string& ordType = "2");
      75              : 
      76              :     /**
      77              :      * @brief 发送撤单请求
      78              :      * @param origClOrdID 原订单ID
      79              :      * @param symbol 合约代码
      80              :      * @param side 买卖方向
      81              :      */
      82              :     void sendCancelOrder(const std::string& origClOrdID, 
      83              :                          const std::string& symbol, const std::string& side);
      84              : 
      85              :     /**
      86              :      * @brief 查询资金
      87              :      */
      88              :     void queryBalance();
      89              : 
      90              :     /**
      91              :      * @brief 查询持仓
      92              :      */
      93              :     void queryPositions();
      94              : 
      95              :     /**
      96              :      * @brief 查询订单历史(服务端持久化)
      97              :      *
      98              :      * 发送 U9 请求,服务端返回 U10 响应(Text 字段为序列化订单列表)。
      99              :      */
     100              :     void queryOrderHistory();
     101              : 
     102              :     /**
     103              :      * @brief 搜索合约
     104              :      * @param pattern 搜索前缀
     105              :      * @param maxResults 最大返回数量
     106              :      */
     107              :     void searchInstruments(const std::string& pattern, int maxResults = 10);
     108              : 
     109              :     /**
     110              :      * @brief 获取用户ID
     111              :      */
     112              :     const std::string& getUserId() const { return userId_; }
     113              : 
     114              : private:
     115              :     // 消息处理函数
     116              :     void handleExecutionReport(const FixMessage& msg);
     117              :     void handleBalanceResponse(const FixMessage& msg);
     118              :     void handlePositionResponse(const FixMessage& msg);
     119              :     void handleAccountUpdate(const FixMessage& msg);
     120              :     void handlePositionUpdate(const FixMessage& msg);
     121              :     void handleInstrumentSearchResponse(const FixMessage& msg);
     122              :     void handleOrderHistoryResponse(const FixMessage& msg);
     123              : 
     124              :     // 生成客户端订单ID
     125              :     std::string generateClOrdID();
     126              : 
     127              :     std::shared_ptr<ClientState> state_;
     128              :     std::weak_ptr<Session> session_;
     129              :     std::string userId_;
     130              :     int64_t clOrdIdPrefixMs_ = 0; ///< 本次运行的 ClOrdID 前缀时间戳(毫秒),用于跨重启去重
     131              :     std::atomic<uint64_t> orderIdCounter_{1};
     132              :     std::atomic<uint64_t> requestIdCounter_{1};
     133              : };
     134              : 
     135              : } // namespace fix40::client
        

Generated by: LCOV version 2.0-1