-
Notifications
You must be signed in to change notification settings - Fork 0
/
headinfo.go
53 lines (45 loc) · 1.2 KB
/
headinfo.go
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
package hconx
import (
"bytes"
"encoding/binary"
)
var HeadFlag uint32
func init() {
HeadFlag = 0x20180618
}
// SetHeadFlag set head flag on global mode
func SetHeadFlag(flag uint32) {
HeadFlag = flag
}
// HeadInfo trans data head info, 20 size
type HeadInfo struct {
Flag uint32 //head flag used to base check
Id uint16 //head id
DataType uint16 //data type
DataId int32 //data func id
DataLen uint64 //data len
}
// GetBytes get bytes
func (h *HeadInfo) GetBytes() []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, h.Flag)
binary.Write(buf, binary.LittleEndian, h.Id)
binary.Write(buf, binary.LittleEndian, h.DataType)
binary.Write(buf, binary.LittleEndian, h.DataId)
binary.Write(buf, binary.LittleEndian, h.DataLen)
return buf.Bytes()
}
// FromBytes convert from bytes
func (h *HeadInfo) FromBytes(b []byte) {
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &h.Flag)
binary.Read(buf, binary.LittleEndian, &h.Id)
binary.Read(buf, binary.LittleEndian, &h.DataType)
binary.Read(buf, binary.LittleEndian, &h.DataId)
binary.Read(buf, binary.LittleEndian, &h.DataLen)
}
func DefaultHead() *HeadInfo {
return &HeadInfo{
Flag: HeadFlag,
}
}