GithubHelp home page GithubHelp logo

cobs-codec's Introduction

Cobs-Codec

Documentation Latest version Build Status unsafe forbidden

This crate provides a COBS (Consistent Overhead Byte Stuffing) codec for Tokio.

The COBS encoding is a very efficient framing method for network packets. Basically; it allows you to send messages consisting of any bytes while still being able to detect where messages start and end.

Encoded messages end with a specific (customizable) byte called a sentinel. Any occurrence of this byte within the message is avoided by a substition scheme that adds very litte overhead: O(1 + N/254) worst case.

Choosing a Sentinel Value

This crate allows users to choose their own sentinel value. There are two guiding principles when choosing a value.

Size: The encoding has the least overhead when the message contains one sentinel at least every 254 bytes. Note that this consideration is irrelevant for messages up to 254 bytes long.

Speed: Encoding/decoding is fastest for messages with as few sentinel values as possible, ideally none.

Features

  • No unsafe code (#[forbid(unsafe_code)])
  • Tested

Example

use std::io::Cursor;
use tokio_util::codec::{FramedWrite, FramedRead};
use futures::{SinkExt, StreamExt};

use cobs_codec::{Encoder, Decoder};

// Choose a message separator that does not appear too frequently in your messages.
const SENTINEL: u8 = 0x00;

// It's a good idea to limit message size to prevent running out of memory.
const MAX: usize = 32;

let encoder = Encoder::<SENTINEL, MAX>::new();
let decoder = Decoder::<SENTINEL, MAX>::new();

// Imagine this buffer being sent from the server to the client.
let mut buffer = Vec::with_capacity(128);

let mut server_cursor = Cursor::new(&mut buffer);
let mut server = FramedWrite::new(&mut server_cursor, encoder);

// Send a few messages.
assert!(server.send("hello").await.is_ok());
assert!(server.send("world").await.is_ok());

let mut client_cursor = Cursor::new(&mut buffer);
let mut client = FramedRead::new(&mut client_cursor, decoder);

// Receive the messages.
assert_eq!(convert(&client.next().await), Some(Ok(b"hello".as_slice())));
assert_eq!(convert(&client.next().await), Some(Ok(b"world".as_slice())));
assert_eq!(convert(&client.next().await), None);

Documentation

Documentation

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

cobs-codec's People

Contributors

alvra avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.