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


相关推荐
xo198820116 分钟前
鸿蒙Des 加密解密 C++版本
c++·华为·harmonyos
啊阿狸不会拉杆1 小时前
《算法导论》第 21 章-用于不相交集合的数据结构
数据结构·c++·算法·随机森林
愿天堂没有C++1 小时前
C++——高性能组件
开发语言·c++·windows
CodeCraft Studio2 小时前
3D文档控件Aspose.3D实用教程:在 C# 中将 3MF 文件转换为 STL
c++·3d·c#
点灯的棉羊3 小时前
从C学C++(10)-string/vector/map的简单使用
c语言·c++
草莓熊Lotso3 小时前
《吃透 C++ 类和对象(上):封装、实例化与 this 指针详解》
开发语言·c++·经验分享·笔记·其他
·前路漫漫亦灿灿3 小时前
C++11-下
开发语言·c++
啊阿狸不会拉杆4 小时前
《算法导论》第 18 章 - B 树
数据结构·c++·b树·算法·排序算法
( ̄▽ ̄).4 小时前
C++联合体的定义
前端·c++·算法