-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpkg.go
More file actions
78 lines (58 loc) · 1.58 KB
/
pkg.go
File metadata and controls
78 lines (58 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package thingsdb
import (
"encoding/binary"
"fmt"
"github.com/vmihailenco/msgpack/v5"
)
// pkgHeaderSize is the size of a package header.
const pkgHeaderSize = 8
// PkgInitCapacity will be used as default capacity when allocating for a package.
const PkgInitCapacity = 8192
// pkg contains of a header and data.
type pkg struct {
size uint32
pid uint16
tp uint8
data []byte
}
// newPkg returns a poiter to a new pkg.
func newPkg(b []byte) (*pkg, error) {
tp := b[6]
check := b[7]
if check != '\xff'^tp {
return nil, fmt.Errorf("invalid checkbit")
}
return &pkg{
size: binary.LittleEndian.Uint32(b),
pid: binary.LittleEndian.Uint16(b[4:]),
tp: tp,
data: nil,
}, nil
}
// setData sets package data
func (p *pkg) setData(b *[]byte, size uint32) {
p.data = (*b)[pkgHeaderSize:size]
}
// pkgPackBin returns a byte array containing a header with serialized data.
func pkgPackBin(pid uint16, tp Proto, data []byte) []byte {
datasz := len(data)
pkgdata := make([]byte, pkgHeaderSize, pkgHeaderSize+datasz)
pkgdata = append(pkgdata, data...)
// set package length.
binary.LittleEndian.PutUint32(pkgdata[0:], uint32(datasz))
// set package pid.
binary.LittleEndian.PutUint16(pkgdata[4:], pid)
// set package type and check bit.
pkgdata[6] = uint8(tp)
pkgdata[7] = '\xff' ^ uint8(tp)
return pkgdata
}
// pkgPack returns a byte array containing a header with serialized data.
func pkgPack(pid uint16, tp Proto, v interface{}) ([]byte, error) {
data, err := msgpack.Marshal(v)
if err != nil {
return nil, err
}
data = pkgPackBin(pid, tp, data)
return data, nil
}