-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
89 lines (80 loc) · 2.01 KB
/
decode.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"github.com/pkg/errors"
//"github.com/google/brotli/go/cbrotli"
"io"
)
type IDecode interface {
Decode([]byte) ([][]byte, error)
}
type DecodeV0 struct {
IDecode
}
// Decode v0没有压缩
func (d *DecodeV0) Decode(input []byte) (result [][]byte, err error) {
return [][]byte{input}, nil
}
type DecodeV2 struct {
IDecode
}
// Decode v2zip压缩
func (d *DecodeV2) Decode(input []byte) (result [][]byte, err error) {
result = [][]byte{}
b := bytes.NewReader(input)
r, err := zlib.NewReader(b)
if err != nil {
err = errors.Wrapf(err, "[DecodeV2 | Decode] unzip err")
return
}
var bodyBuf bytes.Buffer
_, err = io.Copy(&bodyBuf, r)
if err != nil {
err = errors.Wrapf(err, "[DecodeV2 | Decode] io.Copy err")
return
}
bodyLen := int32(bodyBuf.Len())
var offset int32
// |int32(一个cmd的长度+4位)|cmd内容|int32(一个cmd的长度+4位)|cmd内容|int32(一个cmd的长度+4位)|cmd内容|....
for offset < bodyLen {
cmdSize := int32(binary.BigEndian.Uint32(bodyBuf.Bytes()[offset : offset+CmdSize]))
// 协议长度大于body长度
if offset+cmdSize > bodyLen {
err = fmt.Errorf("[DecodeV2 | Decode] offset:%d + cmdSize:%d > bodyLen:%d", offset, cmdSize, bodyLen)
return
}
cmd := bodyBuf.Bytes()[offset+CmdSize : offset+cmdSize]
result = append(result, cmd)
offset += cmdSize
}
return
}
type DecodeManager struct {
m map[int64]IDecode
}
func NewDecodeManager() (manager *DecodeManager) {
return &DecodeManager{
m: map[int64]IDecode{
ProtoVersion0: &DecodeV0{},
ProtoVersion2: &DecodeV2{},
},
}
}
// Decode 解码数据方法
func (m *DecodeManager) Decode(version int64, input []byte) (result [][]byte, err error) {
d, exist := m.m[version]
if !exist {
err = errors.Wrapf(err, "[DecodeManager | Decode] version not found")
return
}
//解码数据函数
result, err = d.Decode(input)
if err != nil {
err = errors.Wrapf(err, "[DecodeManager | Decode] Decode err")
return
}
return
}