This repository contains old version of messgen, "v0", that is discontinued. New version (v1), that is better adapted for usage for high frequency trading (while still can be used on embedded systems) moved to github.com/Alber-Blanc/messgen.
Lightweight and fast message serialization library. Generates message classes/structs from yml scheme.
Features:
- Embedded-friendly
- Fixed size arrays
- Dynamic size arrays
- Nested messages
- Messages metadata
- Supported languages: C++, Go, JavaScript
- python 3.X
On Linux:
sudo apt install python3
On Windows 10:
- Download https://bootstrap.pypa.io/get-pip.py
- Execute
python3 get_pip.py - Execute
pip3 install pyyaml
Each protocol should be placed in directory base_dir/vendor/protocol.
base_dir is base directory for message definitions (is allowed to specify multiple base directories).
vendor is protocol vendor, it is used as namespace in generated messages allowing to avoid conflict between protocols from different vendors if used in one application.
protocol is protocol name, each protocol has protocol ID, that allows to use multiple protocols on single connection, e.g. bootloader and application protocols.
The protocol directory holds a _protocol.yaml describing the protocol itself:
proto_id: 1
generate_protocol_version: true # optional, defaults to true
check_field_order: false # optional, defaults to false
retired_messages: # optional, defaults to none
- {id: 3, name: old_request}generate_protocol_version controls the version stamp - an md5 over the protocol's own message set,
truncated to six hex digits - that C++, JSON and Markdown outputs carry (PROTO_VERSION and
ProtoInfo::VERSION, version.json, the Version line). Set it to false where the peers agree on
their wire format by other means, such as a hand-written constant or a copied message-id registry:
the hash covers field names, so a pure rename moves the stamp even though no byte on the wire moves,
and a stamp nobody checks reads as a protocol event to everyone who greps for it. Stating the default
explicitly does not move the stamp - the key is excluded from the hash.
The generated code does not keep the yaml field order: it puts plain scalars first, then nested
messages, then dynamic fields (strings and T[]), each group sorted by alignment descending, and
serializes them in that order. A peer that reads the yaml in declaration order, as messgen v1 does,
sees a different layout wherever the two orders differ. check_field_order: true makes generation
fail for every message of the protocol whose yaml order differs from the generated one, and the error
lists the order to declare. The key is excluded from the hash too.
retired_messages lists the messages deleted from the protocol, by the id and the name that each had.
A peer on an older build still reads such an id as the deleted message, so generation fails for a
message yaml that takes one. The Markdown leaves these ids out of "Next available message IDs" and
lists them on a line of their own. C++ emits them as RETIRED_MESSAGES, an array of
messgen::RetiredMessage (empty in a protocol without the key), for a receiver that names what such
a peer sent. The key is excluded from the hash: deleting the message's yaml already moved the stamp.
Message generator usage:
python3 generate.py -b <base_dir> -m <vendor>/<protocol> -l <lang> -o <out_dir> [-D variable=value]
For some languages it's necessary to specify some variables using -D option.
Generated messages placed in out_dir directory.
Example for Go messages generation:
python3 generate.py -b ./base_dir -m my_vendor/my_protocol -l go -o out/go -D messgen_go_module=example.com/path/to/messgen
Variable messgen_go_module must point to messgen Go module (port/go/messgen), to add necessary imports in generated messages.
Example for C++ messages generation:
python3 generate.py -b ./base_dir -m my_vendor/my_protocol -l cpp -o out/cpp
Variable metadata_json=true can be passed to generate metadata in JSON format, rather than legacy.
Example for JS messages generation:
python3 generate.py -b ./base_dir -m my_vendor/my_protocol -l json -o out/json
This command will generate json messages.
The types of these messages for TS can be generated as follows:
python3 generate.py -b ./base_dir -m my_vendor/my_protocol -l ts -o out/ts
if it is necessary to generate typed arrays for TS, it is necessary to pass the flag -D typed_arrays=true:
python3 generate.py -b ./base_dir -m my_vendor/my_protocol -l ts -o out/ts -D typed_arrays=true
Example for protocol documentation generation:
python3 generate.py -b ./base_dir -m my_vendor/my_protocol -l md -o out/md
To get the version stamp without generating anything, pass --print-version in place of -l:
python3 generate.py -b ./base_dir -m my_vendor/my_protocol --print-version
It prints the six hex digits the generators would emit, one line per module in -m order, and
writes no files. A module with generate_protocol_version: false has no version, so naming one
fails the call before anything is printed.