开源项目 - 基于 C++ 实现沪深交易所流式二进制协议
📌 简介
fin-proto-cpp
是一个专为金融领域设计的 开源 C++ 协议解析库 ,实现了 上海证券交易所(SSE) 和 深圳证券交易所(SZSE) 的低延迟流式二进制协议编解码器。 该项目具有以下特点:
- ⚡ 高性能编解码:毫秒级处理交易所二进制数据流,采用零拷贝(Zero-copy)设计
- 🪶 轻量化设计:零外部依赖的核心编解码器
- 🛠 可测试性:内置 CI/CD、单元测试
- 🔄 已支持协议:支持沪深两市最新的 Binary 协议版本
GitHub 地址:github.com/xinchentech...
⚙️ 构建过程
1. 环境依赖
本项目依赖于 fin-protoc 协议编译器(基于 Go 与 ANTLR4 实现)。 在 Linux 环境可直接下载预编译二进制并配置到 PATH
:
bash
wget https://github.com/xinchentechnote/fin-protoc/releases/download/v0.1.6/fin-protoc-v0.1.6-linux-amd64.tar.gz
tar -xvf fin-protoc-v0.1.6-linux-amd64.tar.gz
export PATH=$PWD:$PATH
2. 一键编译
bash
git clone https://github.com/xinchentechnote/fin-proto-cpp
cd fin-proto-cpp
./build.sh # 自动触发 CMake 构建
📂 目录结构
bash
├── build.sh
├── include
│ ├── bytebuf.hpp # ByteBuf 封装,类似 Netty ByteBuf
│ ├── checksum.hpp # Checksum 接口定义
│ ├── codec.hpp # 二进制协议编解码器接口定义
│ ├── sse_binary.hpp # SSE Binary 编解码器
│ └── szse_binary.hpp # SZSE Binary 编解码器
└── test
├── bytebuf_test.cpp
├── checksum_test.cpp
├── codec_test.cpp
├── sse_binary_test.cpp
└── szse_binary_test.cpp
🏗 核心接口设计
BinaryCodec 接口
cpp
struct BinaryCodec {
virtual ~BinaryCodec() = default;
virtual void encode(ByteBuf& buf) const = 0;
virtual void decode(ByteBuf& buf) = 0;
virtual std::string toString() const = 0;
virtual bool equals(const BinaryCodec& other) const = 0;
bool operator==(const BinaryCodec& other) const { return equals(other); }
bool operator!=(const BinaryCodec& other) const { return !(*this == other); }
};
Checksum 接口
cpp
class IChecksumService {
public:
virtual ~IChecksumService() = default;
virtual std::string algorithm() const = 0;
};
template <typename Input, typename Output>
class ChecksumService : public IChecksumService {
public:
using input_type = Input;
using output_type = Output;
virtual Output calc(const Input& data) const = 0;
};
🖇 UML 设计图
1. 协议解析架构
classDiagram
class ByteBuf {
+readInt()
+writeInt()
+readBytes()
+writeBytes()
}
class BinaryCodec {
<>
+encode(ByteBuf& buf)
+decode(ByteBuf& buf)
+toString()
+equals(BinaryCodec& other)
}
class SseBinary {
+encode()
+decode()
}
class SzseBinary {
+encode()
+decode()
}
BinaryCodec <|.. SseBinary
BinaryCodec <|.. SzseBinary
BinaryCodec --> ByteBuf
2. 编码流程
sequenceDiagram
participant App as 应用程序
participant Codec as BinaryCodec
participant Buf as ByteBuf
App->>Codec: encode(data)
Codec->>Buf: write fields
Buf-->>App: 返回二进制数据
📊 沪深协议定义
SSE Binary 协议
protocol
//details:https://github.com/xinchentechnote/fin-proto/blob/main/sse/binary/sse_bin_v0.57.pdsl
root packet SseBinary {
uint32 MsgType `消息类型`,
uint64 MsgSeqNum `消息序列号`,
uint32 MsgBodyLen @lengthOf(Body) `消息体长度`,
match MsgType as Body {
33 : Heartbeat,
40 : Logon,
41 : Logout,
58 : NewOrderSingle
},
uint32 Checksum @calculatedFrom("SSE_BIN") `校验和`,
}
SZSE Binary 协议
protocol
//details:https://github.com/xinchentechnote/fin-proto/blob/main/szse/binary/szse_bin_v1.29.pdsl
root packet SzseBinary {
MsgType,
BodyLength @lengthOf(Body),
match MsgType as Body {
1 : Logon,
2 : Logout,
3 : Heartbeat,
10 : TradingSessionStatus,
[100101] : NewOrder
},
int32 Checksum @calculatedFrom("SZSE_BIN"),
}
💡 应用场景
- 撮合引擎:快速解析交易所二进制数据
- 交易/风控系统:低延迟订单处理
- 协议解析教学:含完整测试用例,可作为学习样本
🎯 适合人群
量化交易开发者 | 高频交易系统工程师 | 金融科技爱好者
📌 相关标签
#c++
#金融科技
#开源项目
#量化交易
#高频交易