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

TCP 连接管理类 More...

#include <connection.hpp>

Inheritance diagram for fix40::Connection:
Inheritance graph
Collaboration diagram for fix40::Connection:
Collaboration graph

Public Member Functions

 Connection (int fd, Reactor *reactor, std::shared_ptr< Session > session, ThreadPool *thread_pool, size_t thread_index)
 构造连接对象
 
 ~Connection ()
 析构函数
 
void handle_read ()
 处理读事件
 
void handle_write ()
 处理写事件
 
void send (std::string_view data)
 发送数据
 
void dispatch (std::function< void()> task)
 派发任务到绑定的工作线程执行
 
void shutdown ()
 关闭连接
 
void close_fd ()
 关闭文件描述符
 
int fd () const
 获取 socket 文件描述符
 
size_t thread_index () const
 获取绑定的线程索引
 
std::shared_ptr< Sessionsession () const
 获取关联的 Session 对象
 

Static Public Attributes

static constexpr size_t kMaxReadBufferSize = 1 * 1024 * 1024
 读缓冲区最大大小(1 MB)
 

Detailed Description

TCP 连接管理类

负责管理单个客户端连接的 I/O 操作,包括:

  • 非阻塞读取(ET 模式)
  • 带缓冲的非阻塞写入
  • FIX 消息帧解码
  • 连接生命周期管理
线程模型
每个连接绑定到一个固定的工作线程,所有操作(读、写、定时任务) 都在该线程中串行执行,避免锁竞争。
数据流
发送: Session::send() -> Connection::send() -> do_send() -> socket
TCP 连接管理类
Definition connection.hpp:47
void send(std::string_view data)
发送数据
Definition connection.cpp:119
void handle_read()
处理读事件
Definition connection.cpp:40
FIX 消息帧解码器
Definition fix_frame_decoder.hpp:41
FIX 会话管理器
Definition session.hpp:120
void on_message_received(const FixMessage &msg)
处理收到的消息
Definition session.cpp:269
Note
该类继承 std::enable_shared_from_this,必须通过 std::shared_ptr 管理

Constructor & Destructor Documentation

◆ Connection()

fix40::Connection::Connection ( int  fd,
Reactor reactor,
std::shared_ptr< Session session,
ThreadPool thread_pool,
size_t  thread_index 
)

构造连接对象

Parameters
fd已连接的 socket 文件描述符(应已设置为非阻塞)
reactorReactor 指针,用于注册/修改 I/O 事件
session关联的 FIX 会话对象
thread_pool线程池指针,用于派发任务
thread_index绑定的工作线程索引

◆ ~Connection()

fix40::Connection::~Connection ( )

析构函数

自动关闭连接并释放资源。

Here is the call graph for this function:

Member Function Documentation

◆ close_fd()

void fix40::Connection::close_fd ( )

关闭文件描述符

内部调用 shutdown(),保持接口一致性。

Here is the caller graph for this function:

◆ dispatch()

void fix40::Connection::dispatch ( std::function< void()>  task)

派发任务到绑定的工作线程执行

Parameters
task要执行的任务

确保任务在连接绑定的线程中串行执行,避免竞态条件。

Here is the caller graph for this function:

◆ fd()

int fix40::Connection::fd ( ) const
inline

获取 socket 文件描述符

Returns
int 文件描述符

◆ handle_read()

void fix40::Connection::handle_read ( )

处理读事件

在 ET(边缘触发)模式下循环读取数据直到 EAGAIN, 将数据送入帧解码器,解析出完整的 FIX 消息后交给 Session 处理。

Note
必须在绑定的工作线程中调用
错误处理
  • 缓冲区溢出:通知 Session I/O 错误
  • 对端关闭:通知 Session 连接关闭
  • 解码错误:通知 Session I/O 错误
Here is the call graph for this function:

◆ handle_write()

void fix40::Connection::handle_write ( )

处理写事件

在 ET 模式下循环发送写缓冲区中的数据直到 EAGAIN 或发送完毕。 发送完毕后取消写事件监听。

Note
必须在绑定的工作线程中调用
Here is the call graph for this function:

◆ send()

void fix40::Connection::send ( std::string_view  data)

发送数据

Parameters
data要发送的数据

可以从任意线程调用,内部会将发送操作派发到绑定的工作线程执行。 如果连接已关闭,调用无效。

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

◆ session()

std::shared_ptr< Session > fix40::Connection::session ( ) const
inline

获取关联的 Session 对象

Returns
std::shared_ptr<Session> Session 指针

◆ shutdown()

void fix40::Connection::shutdown ( )

关闭连接

Reactor 移除 fd,关闭 socket。 该方法是幂等的,多次调用安全。

◆ thread_index()

size_t fix40::Connection::thread_index ( ) const
inline

获取绑定的线程索引

Returns
size_t 线程索引

Member Data Documentation

◆ kMaxReadBufferSize

constexpr size_t fix40::Connection::kMaxReadBufferSize = 1 * 1024 * 1024
staticconstexpr

读缓冲区最大大小(1 MB)


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