写点什么

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

  • 2025-08-12
    广东
  • 本文字数:2013 字

    阅读完需:约 7 分钟

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

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

📌 简介

fin-proto-cpp 是一个专为金融领域设计的 开源 C++ 协议解析库,实现了 上海证券交易所(SSE)深圳证券交易所(SZSE) 的低延迟流式二进制协议编解码器。该项目具有以下特点:


  • 高性能编解码:微秒级处理交易所二进制数据流,采用零拷贝(Zero-copy)设计

  • 🪶 轻量化设计:零外部依赖的核心编解码器

  • 🛠 可测试性:内置 CI/CD、单元测试

  • 🔄 已支持协议:支持沪深两市最新的 Binary 协议版本


GitHub 地址:https://github.com/xinchentechnote/fin-proto-cpp



⚙️ 构建过程

1. 环境依赖

本项目依赖于 fin-protoc 协议编译器(基于 Go 与 ANTLR4 实现)。在 Linux 环境可直接下载预编译二进制并配置到 PATH


wget https://github.com/xinchentechnote/fin-protoc/releases/download/v0.1.6/fin-protoc-v0.1.6-linux-amd64.tar.gztar -xvf fin-protoc-v0.1.6-linux-amd64.tar.gzexport PATH=$PWD:$PATH
复制代码

2. 一键编译

git clone https://github.com/xinchentechnote/fin-proto-cppcd fin-proto-cpp./build.sh  # 自动触发 CMake 构建
复制代码



📂 目录结构

├── 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 接口

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 接口

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 协议

//details:https://github.com/xinchentechnote/fin-proto/blob/main/sse/binary/sse_bin_v0.57.pdslroot 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 协议

//details:https://github.com/xinchentechnote/fin-proto/blob/main/szse/binary/szse_bin_v1.29.pdslroot 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++ #金融科技 #开源项目 #量化交易 #高频交易




发布于: 2025-08-12阅读数: 8
用户头像

Talk is cheap, show me the code. 2019-04-17 加入

勿在浮沙筑高台,沉淀、记录个人的技术笔记与总结。现某金融科技公司开发人员,主要负责网络编程,涉及相关网关组件架构设计与重构,低时延性能优化等。

评论

发布
暂无评论
开源项目 - 基于 C++ 实现沪深交易所流式二进制协议_歆晨技术笔记_InfoQ写作社区