FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
connection.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <memory>
12#include <string>
13#include <string_view>
14#include <functional>
15#include <atomic>
17
18namespace fix40 {
19
20// 前置声明
21class Session;
22class Reactor;
23class ThreadPool;
24
47class Connection : public std::enable_shared_from_this<Connection> {
48public:
50 static constexpr size_t kMaxReadBufferSize = 1 * 1024 * 1024;
51
60 Connection(int fd, Reactor* reactor, std::shared_ptr<Session> session,
61 ThreadPool* thread_pool, size_t thread_index);
62
69
83 void handle_read();
84
93 void handle_write();
94
102 void send(std::string_view data);
103
110 void dispatch(std::function<void()> task);
111
118 void shutdown();
119
125 void close_fd();
126
131 int fd() const { return fd_; }
132
137 size_t thread_index() const { return thread_index_; }
138
143 std::shared_ptr<Session> session() const { return session_; }
144
145private:
153 void do_send(const std::string& data);
154
155 const int fd_;
156 Reactor* reactor_;
157 std::shared_ptr<Session> session_;
158 ThreadPool* thread_pool_;
159 const size_t thread_index_;
160 std::atomic<bool> is_closed_{false};
161
162 FixFrameDecoder frame_decoder_;
163 std::string write_buffer_;
164 // 注意:移除了 write_mutex_,因为同一连接的所有操作都在同一线程
165};
166
167} // namespace fix40
TCP 连接管理类
Definition connection.hpp:47
static constexpr size_t kMaxReadBufferSize
读缓冲区最大大小(1 MB)
Definition connection.hpp:50
void dispatch(std::function< void()> task)
派发任务到绑定的工作线程执行
Definition connection.cpp:170
std::shared_ptr< Session > session() const
获取关联的 Session 对象
Definition connection.hpp:143
void send(std::string_view data)
发送数据
Definition connection.cpp:119
size_t thread_index() const
获取绑定的线程索引
Definition connection.hpp:137
void handle_read()
处理读事件
Definition connection.cpp:40
void handle_write()
处理写事件
Definition connection.cpp:91
void close_fd()
关闭文件描述符
Definition connection.cpp:195
void shutdown()
关闭连接
Definition connection.cpp:176
~Connection()
析构函数
Definition connection.cpp:35
int fd() const
获取 socket 文件描述符
Definition connection.hpp:131
基于 Reactor 模式的事件循环
Definition reactor.hpp:81
支持"连接绑定线程"的线程池
Definition thread_pool.hpp:48
FIX 消息帧解码器
Definition matching_engine.hpp:23