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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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