Line data Source code
1 : /**
2 : * @file session_manager.hpp
3 : * @brief FIX 会话管理器
4 : *
5 : * 管理所有活跃的 FIX 会话,提供会话查找和消息发送功能。
6 : * 用于将 MatchingEngine 产生的 ExecutionReport 发送到正确的客户端。
7 : */
8 :
9 : #pragma once
10 :
11 : #include <memory>
12 : #include <mutex>
13 : #include <unordered_map>
14 : #include <functional>
15 : #include "fix/session.hpp"
16 : #include "fix/application.hpp"
17 :
18 : namespace fix40 {
19 :
20 : /**
21 : * @brief SessionID 的哈希函数
22 : */
23 : struct SessionIDHash {
24 45 : std::size_t operator()(const SessionID& id) const {
25 45 : return std::hash<std::string>()(id.senderCompID) ^
26 45 : (std::hash<std::string>()(id.targetCompID) << 1);
27 : }
28 : };
29 :
30 : /**
31 : * @class SessionManager
32 : * @brief FIX 会话管理器
33 : *
34 : * 线程安全地管理所有活跃的 FIX 会话,提供:
35 : * - 会话注册/注销
36 : * - 按 SessionID 查找会话
37 : * - 向指定会话发送消息
38 : *
39 : * @par 使用场景
40 : * MatchingEngine 产生 ExecutionReport 后,通过 SessionManager
41 : * 找到对应的 Session 并发送消息。
42 : *
43 : * @par 线程安全
44 : * 所有公共方法都是线程安全的。
45 : *
46 : * @par 使用示例
47 : * @code
48 : * SessionManager manager;
49 : *
50 : * // 注册会话
51 : * manager.registerSession(session);
52 : *
53 : * // 发送消息
54 : * FixMessage report;
55 : * // ... 构建 ExecutionReport ...
56 : * manager.sendMessage(sessionID, report);
57 : *
58 : * // 注销会话
59 : * manager.unregisterSession(sessionID);
60 : * @endcode
61 : */
62 : class SessionManager {
63 : public:
64 : /**
65 : * @brief 构造会话管理器
66 : */
67 32 : SessionManager() = default;
68 :
69 : /**
70 : * @brief 析构函数
71 : */
72 32 : ~SessionManager() = default;
73 :
74 : // 禁止拷贝
75 : SessionManager(const SessionManager&) = delete;
76 : SessionManager& operator=(const SessionManager&) = delete;
77 :
78 : /**
79 : * @brief 注册会话
80 : * @param session 会话对象
81 : *
82 : * 将会话添加到管理器中。如果已存在相同 SessionID 的会话,
83 : * 会替换旧的会话。
84 : */
85 : void registerSession(std::shared_ptr<Session> session);
86 :
87 : /**
88 : * @brief 注销会话
89 : * @param sessionID 会话标识符
90 : * @return true 成功注销
91 : * @return false 会话不存在
92 : */
93 : bool unregisterSession(const SessionID& sessionID);
94 :
95 : /**
96 : * @brief 查找会话
97 : * @param sessionID 会话标识符
98 : * @return std::shared_ptr<Session> 会话对象,不存在返回 nullptr
99 : */
100 : std::shared_ptr<Session> findSession(const SessionID& sessionID) const;
101 :
102 : /**
103 : * @brief 向指定会话发送消息
104 : * @param sessionID 目标会话标识符
105 : * @param msg 要发送的 FIX 消息
106 : * @return true 发送成功
107 : * @return false 会话不存在或发送失败
108 : *
109 : * 此方法会调用 Session::send_app_message(),
110 : * 触发 Application::toApp() 回调。
111 : */
112 : bool sendMessage(const SessionID& sessionID, FixMessage& msg);
113 :
114 : /**
115 : * @brief 获取活跃会话数量
116 : */
117 : size_t getSessionCount() const;
118 :
119 : /**
120 : * @brief 检查会话是否存在
121 : * @param sessionID 会话标识符
122 : */
123 : bool hasSession(const SessionID& sessionID) const;
124 :
125 : /**
126 : * @brief 遍历所有会话
127 : * @param callback 回调函数,参数为 SessionID 和 Session
128 : */
129 : void forEachSession(std::function<void(const SessionID&, std::shared_ptr<Session>)> callback) const;
130 :
131 : private:
132 : mutable std::mutex mutex_; ///< 保护 sessions_ 的互斥锁
133 : std::unordered_map<SessionID, std::shared_ptr<Session>, SessionIDHash> sessions_;
134 : };
135 :
136 : } // namespace fix40
|