Skip to main content

Generate protocols for C++ and JS

Project description

μ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

  1. Write your protocol in a YAML file (protocol.yml for example)
  2. Generate the code with:
    • ubuf protocol.yml --lang js > protocol.js
    • ubuf protocol.yml --lang cpp > protocol.hpp
  3. Put the io libraries in the same place as the protocol files.
  4. 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:

```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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ubuf-0.0.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

ubuf-0.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file ubuf-0.0.1.tar.gz.

File metadata

  • Download URL: ubuf-0.0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for ubuf-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c87d7de4646a334b47fc6a33aa8640cec87de66a5e2bca5cbd31ae5cd3a212b2
MD5 0fcbe76265dc4218176f84d440603b07
BLAKE2b-256 451fa95ed748387d0376ad7e3c54ada0fe9082525cec7753a5bb8c12b1ac6da2

See more details on using hashes here.

File details

Details for the file ubuf-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ubuf-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for ubuf-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2c519ee157dc3c9c126e5b281fd279312276d07682c905b5798164b20587a9a9
MD5 e5183e21696fe755bf7e656f08182f18
BLAKE2b-256 2a58a43141c42b20da347eb077df8b3010810d98a78edfe9707d5e6d2fc7efcf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page