TCP 连接管理类
More...
#include <connection.hpp>
TCP 连接管理类
负责管理单个客户端连接的 I/O 操作,包括:
- 非阻塞读取(ET 模式)
- 带缓冲的非阻塞写入
- FIX 消息帧解码
- 连接生命周期管理
- 线程模型
- 每个连接绑定到一个固定的工作线程,所有操作(读、写、定时任务) 都在该线程中串行执行,避免锁竞争。
- 数据流
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 管理
◆ Connection()
| fix40::Connection::Connection |
( |
int |
fd, |
|
|
Reactor * |
reactor, |
|
|
std::shared_ptr< Session > |
session, |
|
|
ThreadPool * |
thread_pool, |
|
|
size_t |
thread_index |
|
) |
| |
构造连接对象
- Parameters
-
| fd | 已连接的 socket 文件描述符(应已设置为非阻塞) |
| reactor | Reactor 指针,用于注册/修改 I/O 事件 |
| session | 关联的 FIX 会话对象 |
| thread_pool | 线程池指针,用于派发任务 |
| thread_index | 绑定的工作线程索引 |
◆ ~Connection()
| fix40::Connection::~Connection |
( |
| ) |
|
◆ close_fd()
| void fix40::Connection::close_fd |
( |
| ) |
|
◆ dispatch()
| void fix40::Connection::dispatch |
( |
std::function< void()> |
task | ) |
|
派发任务到绑定的工作线程执行
- Parameters
-
确保任务在连接绑定的线程中串行执行,避免竞态条件。
◆ fd()
| int fix40::Connection::fd |
( |
| ) |
const |
|
inline |
获取 socket 文件描述符
- Returns
- int 文件描述符
◆ handle_read()
| void fix40::Connection::handle_read |
( |
| ) |
|
处理读事件
在 ET(边缘触发)模式下循环读取数据直到 EAGAIN, 将数据送入帧解码器,解析出完整的 FIX 消息后交给 Session 处理。
- Note
- 必须在绑定的工作线程中调用
- 错误处理
-
◆ handle_write()
| void fix40::Connection::handle_write |
( |
| ) |
|
处理写事件
在 ET 模式下循环发送写缓冲区中的数据直到 EAGAIN 或发送完毕。 发送完毕后取消写事件监听。
- Note
- 必须在绑定的工作线程中调用
◆ send()
| void fix40::Connection::send |
( |
std::string_view |
data | ) |
|
发送数据
- Parameters
-
可以从任意线程调用,内部会将发送操作派发到绑定的工作线程执行。 如果连接已关闭,调用无效。
◆ session()
| std::shared_ptr< Session > fix40::Connection::session |
( |
| ) |
const |
|
inline |
◆ shutdown()
| void fix40::Connection::shutdown |
( |
| ) |
|
关闭连接
从 Reactor 移除 fd,关闭 socket。 该方法是幂等的,多次调用安全。
◆ thread_index()
| size_t fix40::Connection::thread_index |
( |
| ) |
const |
|
inline |
获取绑定的线程索引
- Returns
- size_t 线程索引
◆ kMaxReadBufferSize
| constexpr size_t fix40::Connection::kMaxReadBufferSize = 1 * 1024 * 1024 |
|
staticconstexpr |
The documentation for this class was generated from the following files: