μBuf
Introduction
Have you ever needed to create your own network protocol for several languages? Rewriting every change in multiple languages is tedious and introduces bugs!
When I tried to use protobuf, the resulting code was daunting and I observed that the message size was far from optimal.
This is why I created a simplistic protocol generator: μBuf.
It takes a simple YAML file as input and produces compatible serialization and deserialization code for several languages.
Currently, C++ and Javascript are supported as target languages.
Usage
- Write your protocol in a YAML file (
protocol.ymlfor example) - Generate the code with:
ubuf protocol.yml --lang js > protocol.jsubuf protocol.yml --lang cpp > protocol.hpp
- Put the
iolibraries in the same place as the protocol files. - Profit!
Example
This is the config file that defines my protocol for a simple game:
types:
coord_t: double
Pos:
x: coord_t
y: coord_t
UserId: string
messages:
ClientRegisterMsg:
name: string
ServerRegisterMsg:
ClientUpdateMsg:
pos: Pos
ServerUpdateMsg:
user_id: UserId
name: string
pos: Pos
ServerDeleteMsg:
user_id: UserId
As you can see, it is divided in two sections, types and messages. The first define data types that can be used in messages.
Write
From JS:
import * as Proto from "./protocol.js";
const msg = new Proto.ClientUpdateMsg({ x: 4.2, y: 6.9 });
const buf: Uint8Array = msg.serialize();
From C++:
#include "protocol.hpp"
ClientUpdateMsg msg{4.2, 6.9};
string buf = serialize(msg);
Read
From JS:
import * as Proto from "./protocol.js";
const buf = new Proto.InBuffer(data);
const type = buf.readChar();
if (type == Proto.ServerUpdateMsg.id) {
const msg = buf.readServerUpdateMsg();
// msg is a standard JS object that can be intuitively accessed:
// msg.user_id, msg.name, msg.pos.x, msg.pos.y
} else if (type == Proto.ServerDeleteMsg.id) {
const msg = buf.readServerDeleteMsg();
// do something
} else {
console.log("unknown message type", type);
}
From C++:
#include "protocol.hpp"
std::string_view msg;
InBuffer buffer(msg);
if(type == ClientRegisterMsg::id_) {
ClientRegisterMsg msg;
buffer >> msg;
std::string name = msg.name;
// do other things
}
Supported primitive types
- char
- string
- double
Metadata
Release files for ubuf 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ubuf-0.0.2.tar.gz | 5.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ubuf-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.6 kB
Release files / ubuf-0.0.2.tar.gz
| Download URL | ubuf-0.0.2.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7c88d07be5919814c1ced2fe6e3642bc97e38cdc55ced5a745b4f13b4e6bcb74
|
|
BLAKE2b-256 checksum How to use checksums |
67dfbc398e6e12e99867d913c6fda6828f18d2e278f2930c84721128a969f17e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
|
Release files / ubuf-0.0.2-py3-none-any.whl
| Download URL | ubuf-0.0.2-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3ab16ed68ca715759c1247d32f41030da0b72858569b7037d78f151c6570cfae
|
|
BLAKE2b-256 checksum How to use checksums |
cb62141a18d660d075893177d9a60f5eb560ba19b61c622d9f9dfde0cc5cb7fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
|
Security for ubuf 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.