# SBE：消息头以及解码器如何选定消息

> 了解每条 SBE 消息开头的固定消息头如何让交易解码器选定正确的消息排布、处理模式版本并跳过未知的尾部字节。

页面：https://structure.fi/guides/zh/sbe-message-header/

简单二进制编码（SBE）是 FIX 交易社区制定的定偏移二进制格式，交易所和交易台用它传送低延迟行情和报单（[SBE：一条消息如何排布](https://structure.fi/guides/zh/sbe-message-layout/)）。一路实时行情流混合了多种消息类型——成交、最优报价、订单簿快照和状态更新——线路上没有字段名。

因此每条 SBE 消息都以一个小的定长**消息头**开始，让解码器立即知道适用哪个模式和消息排布、发送方使用哪个模式版本，以及定长消息体在哪里结束。本页介绍消息头的四个字段、解码器遵循的确切分派顺序、`blockLength` 为什么要在线路上传送，以及 SBE 消息头与外层[分帧](https://structure.fi/guides/zh/binary-message/#framing)有何不同。

## 四个字段

模式把消息头声明为一个[复合类型](https://structure.fi/guides/zh/sbe-schema-file/#composites)，名为 `messageHeader`，除非 `headerType` 另行指定。在自带的 Binance Spot Streams 示例中，`messageHeader` 为 8 字节——四个 `uint16` 字段：

- `blockLength`：根块的字节数（不含消息头、[重复组](https://structure.fi/guides/zh/variable-sized-data/#sbe-count-prefixed-groups-and-length-prefixed-data)和[变长数据](https://structure.fi/guides/zh/sbe-variable-length-data/)）。
- `templateId`：与 `<message id="…">` 对应的消息 id（`TradesStreamEvent` 为 `10000`）。
- `schemaId`：与 `<messageSchema id="…">` 对应的模式 id（`binance_stream_1_0_sbe.xml` 为 `1`，`binance_spot_3_6_sbe.xml` 为 `3`）。
- `version`：发送方使用的模式版本（`binance_stream_1_0_sbe.xml` 为 `0`，`binance_spot_3_6_sbe.xml` 为 `6`）。

[在 Market Data Visualizer 中打开 Binance 流的消息头](https://structure.fi/tools/market-data-visualizer/app/?lang=zh#open?sample=binance-stream&node=t%2FmessageHeader&place=detail&guide=sbe-message-header&section=block-5)

## 解码器如何分派

1. 按模式的[字节序](https://structure.fi/guides/zh/binary-message/#endianness)，从 SBE 消息的第 0 字节读取消息头。
2. 匹配 `schemaId`，选定协议模式。
3. 匹配 `templateId`，选定消息排布。如果 `templateId` 未知且消息可能包含组或数据，仅凭 `blockLength` 无法跳过它——需要外层[分帧](https://structure.fi/guides/zh/binary-message/#framing)提供的长度。
4. 按模式中的偏移读取根块字段，偏移从紧接消息头之后的字节算起。
5. 从根块起点前进 `blockLength` 字节，开始读取组和变长数据。

## 为什么 blockLength 要在线路上传送

根块的大小在模式中已经给出——`TradesStreamEvent` 为 18 字节——但在线路上发送 `blockLength` 让模式演进得以成立。当较新的发送方在根块末尾追加字段时，较旧的解码器按收到的 `blockLength` 前进，正好落在第一个组上，而不会误读新字节。当较旧的发送方缺少后续版本新增的字段时，`version` 告诉较新的解码器哪些 `sinceVersion` 字段不存在（[模式版本管理](https://structure.fi/guides/zh/schema-versioning/)）。

Binance Spot Streams SBE schema 中 TradesStreamEvent 的布局，按 Market Data Visualizer 的展示方式:

| 偏移 | 大小 | 名称 | 类型 | 出现方式 | 起始版本 |
| --- | --- | --- | --- | --- | --- |
| 0 | 18 | TradesStreamEvent | block |  | 0 |
| 0 | 8 |  eventTime | utcTimestampUs | required | 0 |
| 8 | 8 |  transactTime | utcTimestampUs | required | 0 |
| 16 | 1 |  priceExponent | exponent8 | required | 0 |
| 17 | 1 |  qtyExponent | exponent8 | required | 0 |
| 18 | 6 |  trades | group groupSizeEncoding |  | 0 |
| 18 | 2 |   .blockLength | uint16 |  |  |
| 20 | 4 |   .numInGroup | uint32 |  |  |
| 24 | 25 |   [0] | entry |  |  |
| 24 | 8 |    .id | tradeId | required | 0 |
| 32 | 8 |    .price | mantissa64 | required | 0 |
| 40 | 8 |    .qty | mantissa64 | required | 0 |
| 48 | 1 |    .isBuyerMaker | enum boolEnum | required | 0 |
| — | 0 |    .isBestMatch | const enum boolEnum | constant | 0 |
| 49 | 1 |  symbol | data varString8 |  | 0 |

## 变体与分帧

- **消息头的排布因模式而异。** 由于消息头复合类型由模式声明，交易所可以加宽字段或增加字段。SBE 标准建议同时包含 `numGroups` 和 `numVarDataFields`，以便较旧的解码器跳过未知的组和数据字段。请查看每个交易所的消息头复合类型，而不要假定它是 8 字节。
- **消息头不是帧。** 它从不给出消息总长度。在 TCP 流或抓包文件中，由外层帧提供总长度：要么是 FIX **简单开放分帧头（SOFH）**（标准 SOFH 是 4 字节大端序的总长度（含 SOFH 本身）加 2 字节大端序的编码类型，但 [CME iLink 3](https://structure.fi/guides/zh/cme-ilink3/#a-message-from-the-first-byte) 和 [B3 Binary UMDF](https://structure.fi/guides/zh/b3-umdf/#a-packet-from-the-first-byte) 等交易所使用 4 字节小端序的变体：2 字节长度加 2 字节编码类型），要么是交易所专用的前缀，例如 [CME MDP 3.0 的 `MsgSize`](https://structure.fi/guides/zh/cme-mdp3/#a-packet-from-the-first-byte)。参见[分帧](https://structure.fi/guides/zh/binary-message/#framing)。在 Market Data Visualizer 中，布局从消息头之后、根块的第 0 字节开始，直到你在面板栏的 **分帧** 选择框中选择一种帧；此后帧和消息头作为图层画在根块之前。

## 解码时要检查的事项

- 在硬编码 8 字节偏移之前，先在交易所的模式中核实消息头的字段类型和总大小。
- 解码前同时检查 `schemaId` 和 `templateId`；一条连接可以复用多个模式。
- 切勿用 `blockLength` 跳过无法识别的 `templateId`；应依赖分帧的长度前缀。

关于消息头之后的消息体，参见[SBE：一条消息如何排布](https://structure.fi/guides/zh/sbe-message-layout/)；关于 `headerType` 和模式 id，参见[阅读 XML 消息模式](https://structure.fi/guides/zh/sbe-schema-file/)。

## 来源

- https://www.fixtrading.org/what-is-the-fix-protocol/
- https://cmegroupclientsite.atlassian.net/wiki/spaces/EPICSANDBOX/pages/457638617/MDP+3.0+-+SBE+Technical+Headers
- https://www.fixtrading.org/standards/sbe/
- https://github.com/FIXTradingCommunity/fix-simple-binary-encoding/blob/master/v1-0-STANDARD/doc/03MessageStructure.md
- https://github.com/FIXTradingCommunity/fix-simple-binary-encoding/blob/master/v1-0-STANDARD/doc/05SchemaExtensionMechanism.md
- https://github.com/real-logic/simple-binary-encoding/wiki/FIX-SBE-XML-Primer
- https://github.com/real-logic/simple-binary-encoding/wiki/Message-Versioning

由 Structure 于 2026-11-26 审阅。 最后修改于 2026-11-25。
