-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg.go
More file actions
43 lines (37 loc) · 790 Bytes
/
msg.go
File metadata and controls
43 lines (37 loc) · 790 Bytes
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
package main
import (
"bytes"
"encoding/binary"
)
const magicLen = 17
const magicHead = "blockSync-ver0.01"
type Msg struct {
MagicHead [magicLen]byte
BlockIdx uint32
BlockSize uint32
FileSize uint64
DataSize uint32
Compressed bool
Zero bool
Done bool
}
func stringToFixedSizeArray(s string) [magicLen]byte {
var arr [magicLen]byte
copy(arr[:], s)
return arr
}
func pack(data *Msg) ([]byte, error) {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.LittleEndian, data); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func unpack(dataBytes []byte) (*Msg, error) {
data := &Msg{}
buf := bytes.NewReader(dataBytes)
if err := binary.Read(buf, binary.LittleEndian, data); err != nil {
return nil, err
}
return data, nil
}