FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
fix40::Session Class Reference

FIX 会话管理器 More...

#include <session.hpp>

Inheritance diagram for fix40::Session:
Inheritance graph
Collaboration diagram for fix40::Session:
Collaboration graph

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)
 设置应用层处理器
 
Applicationget_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 ()
 从存储恢复会话状态
 
IStoreget_store () const
 获取存储接口
 
std::weak_ptr< Connectionget_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 编解码器
 

Detailed Description

FIX 会话管理器

实现 FIX 协议的会话层,管理:

  • 会话状态机(Disconnected -> LogonSent -> Established -> LogoutSent)
  • 消息序列号
  • 心跳检测和 TestRequest
  • 优雅登出流程
状态转换图
┌──────────────┐
│ Disconnected │
└──────┬───────┘
start() [客户端发送 Logon]
┌──────────────┐
│ LogonSent │
└──────┬───────┘
│ 收到 Logon 确认
┌──────────────┐
│ Established │
└──────┬───────┘
│ 发起 Logout
┌──────────────┐
│ LogoutSent │
└──────┬───────┘
│ 收到 Logout 确认
┌──────────────┐
│ Disconnected │
└──────────────┘
void start()
启动会话
Definition session.cpp:235
Note
该类继承 std::enable_shared_from_this,必须通过 std::shared_ptr 管理

Member Typedef Documentation

◆ EstablishedCallback

using fix40::Session::EstablishedCallback = std::function<void(std::shared_ptr<Session>)>

会话建立回调类型

在完成 Logon 握手并进入 Established 状态后触发。 常用于:服务端在此时将 Session 注册到 SessionManager。

Note
回调可能在连接绑定的工作线程中执行,需保证线程安全。

◆ ShutdownCallback

using fix40::Session::ShutdownCallback = std::function<void()>

会话关闭回调类型

Constructor & Destructor Documentation

◆ Session()

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
Parameters
hb心跳间隔(秒)
shutdown_cb会话关闭时的回调函数
store存储接口指针(可选,用于消息持久化和断线恢复)
Here is the call graph for this function:

◆ ~Session()

fix40::Session::~Session ( )

析构函数

取消定时任务,释放资源。

Here is the call graph for this function:

Member Function Documentation

◆ changeState()

void fix40::Session::changeState ( std::unique_ptr< IStateHandler newState)

切换会话状态

Parameters
newState新状态对象
Here is the caller graph for this function:

◆ enqueue_raw_for_send()

void fix40::Session::enqueue_raw_for_send ( std::string &&  raw_msg)

将原始消息加入发送队列

Parameters
raw_msg原始消息字符串

◆ get_application()

Application * fix40::Session::get_application ( ) const

获取应用层处理器

Returns
Application* 当前关联的 Application 指针,可能为 nullptr
Here is the caller graph for this function:

◆ get_client_comp_id()

const std::string & fix40::Session::get_client_comp_id ( ) const
inline

获取客户端标识

Returns
客户端的 CompID(从 Logon 消息中提取)

对于 Server 端 Session,返回连接客户端的真实标识。 对于 Client 端 Session,返回自身的 senderCompID。

◆ get_connection()

std::weak_ptr< Connection > fix40::Session::get_connection ( ) const
inline

获取连接对象

Returns
std::weak_ptr<Connection> 连接的弱引用

◆ get_heart_bt_int()

int fix40::Session::get_heart_bt_int ( ) const

获取心跳间隔(秒)

Here is the caller graph for this function:

◆ get_last_recv_time()

std::chrono::steady_clock::time_point fix40::Session::get_last_recv_time ( ) const

获取最后接收时间

Here is the caller graph for this function:

◆ get_last_send_time()

std::chrono::steady_clock::time_point fix40::Session::get_last_send_time ( ) const

获取最后发送时间

Here is the caller graph for this function:

◆ get_max_heart_bt_int()

int fix40::Session::get_max_heart_bt_int ( ) const

获取最大允许心跳间隔

Here is the caller graph for this function:

◆ get_min_heart_bt_int()

int fix40::Session::get_min_heart_bt_int ( ) const

获取最小允许心跳间隔

Here is the caller graph for this function:

◆ get_recv_seq_num()

int fix40::Session::get_recv_seq_num ( )
inline

获取接收序列号

◆ get_send_seq_num()

int fix40::Session::get_send_seq_num ( )
inline

获取发送序列号

◆ get_session_id()

SessionID fix40::Session::get_session_id ( ) const

获取会话标识符

Returns
SessionID 包含 senderCompID 和 targetCompID
Here is the caller graph for this function:

◆ get_store()

IStore * fix40::Session::get_store ( ) const
inline

获取存储接口

Returns
IStore* 存储接口指针,可能为 nullptr
Here is the caller graph for this function:

◆ handle_write_ready()

void fix40::Session::handle_write_ready ( )

处理写就绪事件

◆ increment_recv_seq_num()

void fix40::Session::increment_recv_seq_num ( )
inline

递增接收序列号

Here is the caller graph for this function:

◆ increment_send_seq_num()

void fix40::Session::increment_send_seq_num ( )
inline

递增发送序列号

◆ initiate_logout()

void fix40::Session::initiate_logout ( const std::string &  reason)

发起优雅登出流程

Parameters
reason登出原因

◆ is_outbound_queue_empty()

bool fix40::Session::is_outbound_queue_empty ( ) const

检查发送队列是否为空

Returns
true 队列为空
false 队列非空

◆ is_processing_resend()

bool fix40::Session::is_processing_resend ( ) const
inline

检查是否正在处理重传请求

◆ is_running()

bool fix40::Session::is_running ( ) const
inline

检查会话是否正在运行

Returns
true 正在运行
false 已停止

◆ notify_established()

void fix40::Session::notify_established ( )

通知会话已建立

仅供内部状态机在完成 Logon 握手后调用,用于触发 EstablishedCallback。

Note
该方法是幂等的:多次调用只会触发一次回调。
Here is the caller graph for this function:

◆ on_io_error()

void fix40::Session::on_io_error ( const std::string &  reason)

处理 I/O 错误

Parameters
reason错误原因
Here is the call graph for this function:

◆ on_message_received()

void fix40::Session::on_message_received ( const FixMessage msg)

处理收到的消息

Parameters
msg解码后的 FIX 消息
Here is the call graph for this function:

◆ on_shutdown()

void fix40::Session::on_shutdown ( const std::string &  reason)

处理连接关闭

Parameters
reason关闭原因
Here is the call graph for this function:

◆ on_timer_check()

void fix40::Session::on_timer_check ( )

处理定时器检查

由时间轮周期性调用,用于心跳发送和超时检测。

◆ perform_shutdown()

void fix40::Session::perform_shutdown ( const std::string &  reason)

执行会话关闭

Parameters
reason关闭原因
Here is the call graph for this function:
Here is the caller graph for this function:

◆ restore_session_state()

bool fix40::Session::restore_session_state ( )

从存储恢复会话状态

Returns
true 如果成功恢复,false 如果没有保存的状态
Here is the call graph for this function:
Here is the caller graph for this function:

◆ save_session_state()

void fix40::Session::save_session_state ( )

保存会话状态到存储

Here is the call graph for this function:
Here is the caller graph for this function:

◆ schedule_timer_tasks()

void fix40::Session::schedule_timer_tasks ( TimingWheel wheel)

调度周期性定时任务

Parameters
wheel时间轮指针
Here is the call graph for this function:

◆ send()

void fix40::Session::send ( FixMessage msg)

发送 FIX 消息

Parameters
msg要发送的消息(会自动设置序列号)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_app_message()

void fix40::Session::send_app_message ( FixMessage msg)
@brief 发送业务消息
@param msg 要发送的业务消息

send() 的区别是会先调用 Application::toApp() 回调, 允许应用层在发送前拦截或修改消息。

Here is the call graph for this function:

◆ send_buffered_data()

void fix40::Session::send_buffered_data ( )

发送缓冲区中的数据

◆ send_heartbeat()

void fix40::Session::send_heartbeat ( const std::string &  test_req_id = "")

发送 Heartbeat 消息

Parameters
test_req_idTestReqID(响应 TestRequest 时填写)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_logout()

void fix40::Session::send_logout ( const std::string &  reason)

发送 Logout 消息

Parameters
reason登出原因
Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_resend_request()

void fix40::Session::send_resend_request ( int  begin_seq_no,
int  end_seq_no 
)

发送 ResendRequest 消息

Parameters
begin_seq_no请求重传的起始序列号
end_seq_no请求重传的结束序列号(0 表示到最新)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_sequence_reset_gap_fill()

void fix40::Session::send_sequence_reset_gap_fill ( int  seq_num,
int  new_seq_no 
)

发送 SequenceReset-GapFill 消息

Parameters
seq_num消息序列号
new_seq_no新的序列号
Here is the call graph for this function:

◆ send_test_request()

void fix40::Session::send_test_request ( const std::string &  id)

发送 TestRequest 消息

Parameters
id测试请求标识符
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_application()

void fix40::Session::set_application ( Application app)

设置应用层处理器

Parameters
appApplication 实现类指针

设置后,收到业务消息时会调用 app->fromApp(), 会话建立/断开时会调用 app->onLogon()/onLogout()。

Note
Application 的生命周期必须比 Session
可传入 nullptr 取消关联

◆ set_client_comp_id()

void fix40::Session::set_client_comp_id ( const std::string &  clientId)
inline

设置客户端标识

Parameters
clientId客户端的 CompID

DisconnectedState::onMessageReceived 在收到 Logon 消息时调用。

Here is the caller graph for this function:

◆ set_connection()

void fix40::Session::set_connection ( std::weak_ptr< Connection conn)

关联 Connection 对象

Parameters
connConnection 的弱引用

◆ set_established_callback()

void fix40::Session::set_established_callback ( EstablishedCallback  cb)

设置会话建立回调

Parameters
cb会话建立回调

当会话完成 Logon 握手并进入 Established 状态时调用。

Note
回调只会触发一次(幂等)。

◆ set_heart_bt_int()

void fix40::Session::set_heart_bt_int ( int  new_hb)

设置心跳间隔

Here is the caller graph for this function:

◆ set_processing_resend()

void fix40::Session::set_processing_resend ( bool  processing)
inline

设置重传处理状态

◆ set_recv_seq_num()

void fix40::Session::set_recv_seq_num ( int  seq)
inline

设置接收序列号

Here is the caller graph for this function:

◆ set_send_seq_num()

void fix40::Session::set_send_seq_num ( int  seq)
inline

设置发送序列号

Parameters
seq新的发送序列号

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

Here is the caller graph for this function:

◆ set_target_comp_id()

void fix40::Session::set_target_comp_id ( const std::string &  target)

更新对端 CompID

Parameters
target对端的真实 CompID(通常来自 Logon SenderCompID)

服务端在 accept 新连接时往往无法立即知道客户端 CompID。 收到客户端 Logon 后,应将 targetCompID 更新为真实客户端 CompID, 以便后续发送消息时能正确设置 TargetCompID(56),并形成稳定的 SessionID。

Here is the caller graph for this function:

◆ start()

void fix40::Session::start ( )

启动会话

客户端:发送 Logon 消息 服务端:等待客户端 Logon

◆ stop()

void fix40::Session::stop ( )

停止会话

Here is the caller graph for this function:

◆ update_last_recv_time()

void fix40::Session::update_last_recv_time ( )

更新最后接收时间

Here is the caller graph for this function:

◆ update_last_send_time()

void fix40::Session::update_last_send_time ( )

更新最后发送时间

Here is the caller graph for this function:

Member Data Documentation

◆ codec_

FixCodec fix40::Session::codec_

FIX 编解码器

◆ senderCompID

const std::string fix40::Session::senderCompID

发送方 CompID

◆ targetCompID

std::string fix40::Session::targetCompID

接收方 CompID(服务端可在 Logon 后更新为真实客户端 CompID)


The documentation for this class was generated from the following files: