forked from iyear/biligo-live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
73 lines (68 loc) · 1.82 KB
/
util.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
package live
import (
"bytes"
"compress/zlib"
"encoding/binary"
"github.com/andybalholm/brotli"
"io"
)
func write(l int, n int) []byte {
b := make([]byte, l)
switch l {
case 2:
binary.BigEndian.PutUint16(b, uint16(n))
case 4:
binary.BigEndian.PutUint32(b, uint32(n))
case 8:
binary.BigEndian.PutUint64(b, uint64(n))
}
return b
}
func encode(ver, op uint8, body []byte) []byte {
header := make([]byte, 0, wsPackHeaderTotalLen)
header = append(header, write(wsPackageLen, len(body)+wsPackHeaderTotalLen)...)
header = append(header, write(wsHeaderLen, wsPackHeaderTotalLen)...)
header = append(header, write(wsVerLen, int(ver))...)
header = append(header, write(wsOpLen, int(op))...)
header = append(header, write(wsSequenceLen, wsHeaderDefaultSequence)...)
return append(header, body...)
}
// decode 必须确保len(b)>16
func decode(b []byte) (ver uint16, op uint32, body []byte) {
return binary.BigEndian.Uint16(b[wsPackageLen+wsHeaderLen : wsPackageLen+wsHeaderLen+wsVerLen]),
binary.BigEndian.Uint32(b[wsPackageLen+wsHeaderLen+wsVerLen : wsPackageLen+wsHeaderLen+wsVerLen+wsOpLen]),
b[wsPackHeaderTotalLen:]
}
func zlibDe(src []byte) ([]byte, error) {
var (
r io.ReadCloser
o bytes.Buffer
err error
)
if r, err = zlib.NewReader(bytes.NewReader(src)); err != nil {
return nil, err
}
if _, err = io.Copy(&o, r); err != nil {
return nil, err
}
return o.Bytes(), nil
}
func brotliDe(src []byte) ([]byte, error) {
o := new(bytes.Buffer)
r := brotli.NewReader(bytes.NewReader(src))
if _, err := io.Copy(o, r); err != nil {
return nil, err
}
return o.Bytes(), nil
}
func brotliEn(src []byte) ([]byte, error) {
b := new(bytes.Buffer)
w := brotli.NewWriter(b)
if _, err := w.Write(src); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}