开源项目 - 基于 C++ 实现沪深交易所流式二进制协议

开源项目 - 基于 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 { <<interface>> +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++ #金融科技 #开源项目 #量化交易 #高频交易


相关推荐
_wyt0015 小时前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾8 小时前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you10 小时前
constexpr函数
c++
凡人叶枫10 小时前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫10 小时前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss10 小时前
BRpc使用
c++·rpc
-森屿安年-11 小时前
63. 不同路径 II
c++·算法·动态规划
chase_my_dream11 小时前
Cartographer详细讲解
c++·人工智能·自动驾驶
森G11 小时前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
碧海蓝天202211 小时前
C++法则24:在标准 C++ 中,没有任何可移植的方式判断指针 T* pt 指向的内存位置是否已经 构造了对象,程序员必须手动跟踪哪些元素已构造。
java·开发语言·c++