-
Notifications
You must be signed in to change notification settings - Fork 13
/
frame.go
75 lines (64 loc) · 1.62 KB
/
frame.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package utils
import (
"bytes"
"encoding/binary"
"errors"
"io"
)
const frameCode uint32 = 0x656f7363 // frameCode = "eosc"
var (
ErrorInvalidFrame = errors.New("invalid frame")
)
func ReadFrame(r io.Reader) ([]byte, error) {
heater := make([]byte, 4)
_, err := io.ReadFull(r, heater)
if err != nil {
return nil, err
}
code := binary.BigEndian.Uint32(heater)
if code != frameCode {
return nil, ErrorInvalidFrame
}
_, err = io.ReadFull(r, heater)
if err != nil {
return nil, err
}
size := binary.BigEndian.Uint32(heater)
buf := make([]byte, size)
_, err = io.ReadFull(r, buf)
if err != nil {
return nil, err
}
return buf, nil
}
func WriteFrame(w io.Writer, data []byte) error {
size := len(data)
err := binary.Write(w, binary.BigEndian, frameCode)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, uint32(size))
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
func DecodeFrame(data []byte) ([]byte, error) {
return ReadFrame(bytes.NewBuffer(data))
}
func EncodeFrame(data []byte) []byte {
size := len(data)
buf := make([]byte, size+8)
binary.BigEndian.PutUint32(buf[0:4], frameCode)
binary.BigEndian.PutUint32(buf[4:8], uint32(size))
copy(buf[8:], data)
return buf
}