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:

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.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ubuf-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 7c88d07be5919814c1ced2fe6e3642bc97e38cdc55ced5a745b4f13b4e6bcb74
MD5 051704f53908aa76e3eac8a1a961a910
BLAKE2b-256 67dfbc398e6e12e99867d913c6fda6828f18d2e278f2930c84721128a969f17e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ubuf-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3ab16ed68ca715759c1247d32f41030da0b72858569b7037d78f151c6570cfae
MD5 4fa92131074face94ffb634b5785174c
BLAKE2b-256 cb62141a18d660d075893177d9a60f5eb560ba19b61c622d9f9dfde0cc5cb7fc

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