GithubHelp home page GithubHelp logo

go-binproto's Introduction

go-binproto

Build Status go report card coverage godocs

This package provides simple binary protocol implementation in Golang.

The protocol is intended to be used on low-memory devices, like MIPS processors, so it's written in such a manner to maintan low memory-usage and minimize memory reallocation between operations.

How it works

Protocol uses COBS encoding with Fletcher CRC16 checksum. Source message is first concatenated with its checksum and then encoded using COBS.

Thanks to the used encoding method, the resulting data contains only one '0' sign - at the frame end, so it's easy to check where each frame is ending.

Memory usage

Library contains internal memory buffer to which the encoded/decoded messages are written, so each call to encode or decode will overwrite last data.

Internal memory buffer will grow only if its required to store a message bigger than its current length.

To obtain a copy of the last result use the Copy method. !This method will allocate new memory for the result data on each call!.

BenchmarkCache_Encode-4             	100000000	        14.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkCache_Decode-4             	100000000	        15.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkBinProto_Encode-4          	20000000	        73.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkBinProto_Decode-4          	20000000	        69.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkCobsEncode-4               	100000000	        21.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkCobsDecode-4               	100000000	        17.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkFletcher16-4               	100000000	        21.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkWriteReadShouldSucceed-4   	5000000	                313  ns/op	       0 B/op	       0 allocs/op

Thread safety

Currently this library is not thread-safe, but it should be fairly easy to implement using for example mutexes.

Usage

proto := NewBinProto()
src := []byte{1, 1, 1, 0, 0, 1, 5, 12, 44}
encoded, _ := proto.Encode(src)
// to save data for later use
encodedCopy := proto.Copy()
...
decoded, _ := proto.Decode(encodedCopy)
// to save data for later use
decodedCopy := proto.Copy()

go-binproto's People

Contributors

mic90 avatar

Watchers

 avatar  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.