-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatapack.go
76 lines (60 loc) · 2.2 KB
/
datapack.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
package tnet
import (
"bytes"
"encoding/binary"
"errors"
"github.com/HOU-SZ/tigerkin/tiface"
"github.com/HOU-SZ/tigerkin/utils"
)
// 封包拆包类,暂时不需要成员
type DataPack struct{}
// 封包拆包实例的初始化方法
func NewDataPack() *DataPack {
return &DataPack{}
}
// 获取包头长度方法
func (dp *DataPack) GetHeadLen() uint32 {
//DataLen uint32(4字节) + ID uint32(4字节)
return 8
}
// 封包方法(压缩数据)
func (dp *DataPack) Pack(msg tiface.IMessage) ([]byte, error) {
// 创建一个存放bytes字节的缓冲
dataBuff := bytes.NewBuffer([]byte{})
// 将dataLen 写进dataBuff中
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetDataLen()); err != nil {
return nil, err
}
// 将msgID 写进dataBuff中
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetMsgId()); err != nil {
return nil, err
}
// 将data数据 写进dataBuff中
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetData()); err != nil {
return nil, err
}
return dataBuff.Bytes(), nil
}
// 拆包方法(解压数据)
// 进行拆包的时候是分两次过程的,第一次得到msgId和dataLen,第二次根据dataLen读取消息数据,第二次是依赖第一次的dataLen结果,
// 所以Unpack只能解压出包头head的内容,得到msgId和dataLen。之后调用者再根据dataLen继续从io流中读取body中的数据。
func (dp *DataPack) Unpack(binaryData []byte) (tiface.IMessage, error) {
// 创建一个存放输入二进制数据的ioReader
dataBuff := bytes.NewReader(binaryData)
// 只解压head的信息,得到dataLen和msgID
msg := &Message{}
// 读dataLen
if err := binary.Read(dataBuff, binary.LittleEndian, &msg.DataLen); err != nil {
return nil, err
}
// 读msgID
if err := binary.Read(dataBuff, binary.LittleEndian, &msg.Id); err != nil {
return nil, err
}
// 判断dataLen的长度是否超出我们允许的最大包长度
if utils.GlobalObject.MaxPacketSize > 0 && msg.DataLen > utils.GlobalObject.MaxPacketSize {
return nil, errors.New("too large msg data recieved")
}
// 这里只需要把head的数据拆包出来就可以了,然后再通过head的长度,再从conn读取一次数据
return msg, nil
}