|
FIX 4.0 Demo 1.0
|
FIX 会话管理器 More...
#include <session.hpp>


Public Types | |
| using | ShutdownCallback = std::function< void()> |
| 会话关闭回调类型 | |
| using | EstablishedCallback = std::function< void(std::shared_ptr< Session >)> |
| 会话建立回调类型 | |
Public Member Functions | |
| Session (const std::string &sender, const std::string &target, int hb, ShutdownCallback shutdown_cb, IStore *store=nullptr) | |
| ~Session () | |
| 析构函数 | |
| void | set_connection (std::weak_ptr< Connection > conn) |
| 关联 Connection 对象 | |
| void | set_application (Application *app) |
| 设置应用层处理器 | |
| Application * | get_application () const |
| 获取应用层处理器 | |
| SessionID | get_session_id () const |
| 获取会话标识符 | |
| void | set_established_callback (EstablishedCallback cb) |
| 设置会话建立回调 | |
| void | notify_established () |
| 通知会话已建立 | |
| void | set_target_comp_id (const std::string &target) |
| 更新对端 CompID | |
| void | send_app_message (FixMessage &msg) |
| void | start () |
| 启动会话 | |
| void | stop () |
| 停止会话 | |
| bool | is_running () const |
| 检查会话是否正在运行 | |
| void | send (FixMessage &msg) |
| 发送 FIX 消息 | |
| void | send_buffered_data () |
| 发送缓冲区中的数据 | |
| void | handle_write_ready () |
| 处理写就绪事件 | |
| void | enqueue_raw_for_send (std::string &&raw_msg) |
| 将原始消息加入发送队列 | |
| bool | is_outbound_queue_empty () const |
| 检查发送队列是否为空 | |
| void | on_message_received (const FixMessage &msg) |
| 处理收到的消息 | |
| void | on_timer_check () |
| 处理定时器检查 | |
| void | on_io_error (const std::string &reason) |
| 处理 I/O 错误 | |
| void | on_shutdown (const std::string &reason) |
| 处理连接关闭 | |
| void | initiate_logout (const std::string &reason) |
| 发起优雅登出流程 | |
| void | schedule_timer_tasks (TimingWheel *wheel) |
| 调度周期性定时任务 | |
| void | changeState (std::unique_ptr< IStateHandler > newState) |
| 切换会话状态 | |
| void | send_logout (const std::string &reason) |
| 发送 Logout 消息 | |
| void | send_heartbeat (const std::string &test_req_id="") |
| 发送 Heartbeat 消息 | |
| void | send_test_request (const std::string &id) |
| 发送 TestRequest 消息 | |
| void | perform_shutdown (const std::string &reason) |
| 执行会话关闭 | |
| void | update_last_recv_time () |
| 更新最后接收时间 | |
| void | update_last_send_time () |
| 更新最后发送时间 | |
| std::chrono::steady_clock::time_point | get_last_recv_time () const |
| 获取最后接收时间 | |
| std::chrono::steady_clock::time_point | get_last_send_time () const |
| 获取最后发送时间 | |
| int | get_heart_bt_int () const |
| 获取心跳间隔(秒) | |
| void | set_heart_bt_int (int new_hb) |
| 设置心跳间隔 | |
| int | get_min_heart_bt_int () const |
| 获取最小允许心跳间隔 | |
| int | get_max_heart_bt_int () const |
| 获取最大允许心跳间隔 | |
| int | get_send_seq_num () |
| 获取发送序列号 | |
| int | get_recv_seq_num () |
| 获取接收序列号 | |
| void | increment_send_seq_num () |
| 递增发送序列号 | |
| void | increment_recv_seq_num () |
| 递增接收序列号 | |
| void | set_recv_seq_num (int seq) |
| 设置接收序列号 | |
| void | set_send_seq_num (int seq) |
| 设置发送序列号 | |
| void | send_resend_request (int begin_seq_no, int end_seq_no) |
| 发送 ResendRequest 消息 | |
| void | send_sequence_reset_gap_fill (int seq_num, int new_seq_no) |
| 发送 SequenceReset-GapFill 消息 | |
| void | save_session_state () |
| 保存会话状态到存储 | |
| bool | restore_session_state () |
| 从存储恢复会话状态 | |
| IStore * | get_store () const |
| 获取存储接口 | |
| std::weak_ptr< Connection > | get_connection () const |
| 获取连接对象 | |
| bool | is_processing_resend () const |
| 检查是否正在处理重传请求 | |
| void | set_processing_resend (bool processing) |
| 设置重传处理状态 | |
| const std::string & | get_client_comp_id () const |
| 获取客户端标识 | |
| void | set_client_comp_id (const std::string &clientId) |
| 设置客户端标识 | |
Public Attributes | |
| const std::string | senderCompID |
| 发送方 CompID | |
| std::string | targetCompID |
| 接收方 CompID(服务端可在 Logon 后更新为真实客户端 CompID) | |
| FixCodec | codec_ |
| FIX 编解码器 | |
FIX 会话管理器
实现 FIX 协议的会话层,管理:
| using fix40::Session::EstablishedCallback = std::function<void(std::shared_ptr<Session>)> |
会话建立回调类型
在完成 Logon 握手并进入 Established 状态后触发。 常用于:服务端在此时将 Session 注册到 SessionManager。
| using fix40::Session::ShutdownCallback = std::function<void()> |
会话关闭回调类型
| fix40::Session::Session | ( | const std::string & | sender, |
| const std::string & | target, | ||
| int | hb, | ||
| ShutdownCallback | shutdown_cb, | ||
| IStore * | store = nullptr |
||
| ) |
@brief 构造会话对象 @param sender 发送方 CompID @param target 接收方 CompID
| hb | 心跳间隔(秒) |
| shutdown_cb | 会话关闭时的回调函数 |
| store | 存储接口指针(可选,用于消息持久化和断线恢复) |

| fix40::Session::~Session | ( | ) |
析构函数
取消定时任务,释放资源。

| void fix40::Session::changeState | ( | std::unique_ptr< IStateHandler > | newState | ) |
切换会话状态
| newState | 新状态对象 |

| void fix40::Session::enqueue_raw_for_send | ( | std::string && | raw_msg | ) |
将原始消息加入发送队列
| raw_msg | 原始消息字符串 |
| Application * fix40::Session::get_application | ( | ) | const |
获取应用层处理器

|
inline |
获取客户端标识
对于 Server 端 Session,返回连接客户端的真实标识。 对于 Client 端 Session,返回自身的 senderCompID。
|
inline |
获取连接对象
| int fix40::Session::get_heart_bt_int | ( | ) | const |
获取心跳间隔(秒)

| std::chrono::steady_clock::time_point fix40::Session::get_last_recv_time | ( | ) | const |
获取最后接收时间

| std::chrono::steady_clock::time_point fix40::Session::get_last_send_time | ( | ) | const |
获取最后发送时间

| int fix40::Session::get_max_heart_bt_int | ( | ) | const |
获取最大允许心跳间隔

| int fix40::Session::get_min_heart_bt_int | ( | ) | const |
获取最小允许心跳间隔

|
inline |
获取接收序列号
|
inline |
获取发送序列号
| SessionID fix40::Session::get_session_id | ( | ) | const |
|
inline |
获取存储接口

| void fix40::Session::handle_write_ready | ( | ) |
处理写就绪事件
|
inline |
递增接收序列号

|
inline |
递增发送序列号
| void fix40::Session::initiate_logout | ( | const std::string & | reason | ) |
发起优雅登出流程
| reason | 登出原因 |
| bool fix40::Session::is_outbound_queue_empty | ( | ) | const |
检查发送队列是否为空
|
inline |
检查是否正在处理重传请求
|
inline |
检查会话是否正在运行
| void fix40::Session::notify_established | ( | ) |
通知会话已建立
仅供内部状态机在完成 Logon 握手后调用,用于触发 EstablishedCallback。

| void fix40::Session::on_io_error | ( | const std::string & | reason | ) |
处理 I/O 错误
| reason | 错误原因 |

| void fix40::Session::on_message_received | ( | const FixMessage & | msg | ) |
处理收到的消息
| msg | 解码后的 FIX 消息 |

| void fix40::Session::on_shutdown | ( | const std::string & | reason | ) |
处理连接关闭
| reason | 关闭原因 |

| void fix40::Session::on_timer_check | ( | ) |
处理定时器检查
由时间轮周期性调用,用于心跳发送和超时检测。
| void fix40::Session::perform_shutdown | ( | const std::string & | reason | ) |
执行会话关闭
| reason | 关闭原因 |


| bool fix40::Session::restore_session_state | ( | ) |
从存储恢复会话状态


| void fix40::Session::save_session_state | ( | ) |
保存会话状态到存储


| void fix40::Session::schedule_timer_tasks | ( | TimingWheel * | wheel | ) |
调度周期性定时任务
| wheel | 时间轮指针 |

| void fix40::Session::send | ( | FixMessage & | msg | ) |
发送 FIX 消息
| msg | 要发送的消息(会自动设置序列号) |


| void fix40::Session::send_app_message | ( | FixMessage & | msg | ) |
@brief 发送业务消息 @param msg 要发送的业务消息
与 send() 的区别是会先调用 Application::toApp() 回调, 允许应用层在发送前拦截或修改消息。

| void fix40::Session::send_buffered_data | ( | ) |
发送缓冲区中的数据
| void fix40::Session::send_heartbeat | ( | const std::string & | test_req_id = "" | ) |
发送 Heartbeat 消息
| test_req_id | TestReqID(响应 TestRequest 时填写) |


| void fix40::Session::send_logout | ( | const std::string & | reason | ) |
发送 Logout 消息
| reason | 登出原因 |


| void fix40::Session::send_resend_request | ( | int | begin_seq_no, |
| int | end_seq_no | ||
| ) |
发送 ResendRequest 消息
| begin_seq_no | 请求重传的起始序列号 |
| end_seq_no | 请求重传的结束序列号(0 表示到最新) |


| void fix40::Session::send_sequence_reset_gap_fill | ( | int | seq_num, |
| int | new_seq_no | ||
| ) |
发送 SequenceReset-GapFill 消息
| seq_num | 消息序列号 |
| new_seq_no | 新的序列号 |

| void fix40::Session::send_test_request | ( | const std::string & | id | ) |
发送 TestRequest 消息
| id | 测试请求标识符 |


| void fix40::Session::set_application | ( | Application * | app | ) |
设置应用层处理器
| app | Application 实现类指针 |
设置后,收到业务消息时会调用 app->fromApp(), 会话建立/断开时会调用 app->onLogon()/onLogout()。
|
inline |
设置客户端标识
| clientId | 客户端的 CompID |
由 DisconnectedState::onMessageReceived 在收到 Logon 消息时调用。

| void fix40::Session::set_connection | ( | std::weak_ptr< Connection > | conn | ) |
关联 Connection 对象
| conn | Connection 的弱引用 |
| void fix40::Session::set_established_callback | ( | EstablishedCallback | cb | ) |
设置会话建立回调
| cb | 会话建立回调 |
当会话完成 Logon 握手并进入 Established 状态时调用。
| void fix40::Session::set_heart_bt_int | ( | int | new_hb | ) |
设置心跳间隔

|
inline |
设置重传处理状态
|
inline |
设置接收序列号

|
inline |
设置发送序列号
| seq | 新的发送序列号 |
用于断线恢复时从存储中恢复序列号状态。 注意:正常发送流程中不应直接调用此方法, 序列号由 send() 方法自动管理。

| void fix40::Session::set_target_comp_id | ( | const std::string & | target | ) |
更新对端 CompID
| target | 对端的真实 CompID(通常来自 Logon SenderCompID) |
服务端在 accept 新连接时往往无法立即知道客户端 CompID。 收到客户端 Logon 后,应将 targetCompID 更新为真实客户端 CompID, 以便后续发送消息时能正确设置 TargetCompID(56),并形成稳定的 SessionID。

| void fix40::Session::start | ( | ) |
启动会话
客户端:发送 Logon 消息 服务端:等待客户端 Logon
| void fix40::Session::stop | ( | ) |
停止会话

| void fix40::Session::update_last_recv_time | ( | ) |
更新最后接收时间

| void fix40::Session::update_last_send_time | ( | ) |
更新最后发送时间

| FixCodec fix40::Session::codec_ |
FIX 编解码器
| const std::string fix40::Session::senderCompID |
发送方 CompID
| std::string fix40::Session::targetCompID |
接收方 CompID(服务端可在 Logon 后更新为真实客户端 CompID)