https://github.com/mic90/go-binproto
Binary protocol using COBS + CRC16
https://github.com/mic90/go-binproto
binary-protocol cobs go golang
Last synced: about 1 month ago
JSON representation
Binary protocol using COBS + CRC16
- Host: GitHub
- URL: https://github.com/mic90/go-binproto
- Owner: mic90
- License: gpl-3.0
- Created: 2018-02-10T13:01:24.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-03T16:41:14.000Z (over 7 years ago)
- Last Synced: 2024-06-20T15:21:08.726Z (over 1 year ago)
- Topics: binary-protocol, cobs, go, golang
- Language: Go
- Homepage:
- Size: 63.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-binproto
[](https://travis-ci.org/mic90/go-binproto)
[](https://goreportcard.com/report/github.com/mic90/go-binproto)
[](https://gocover.io/github.com/mic90/go-binproto)
[](https://godoc.org/github.com/mic90/go-binproto)
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!.
```bash
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 ##
```golang
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()
```