Pack packs the message (compresses the data) (封包方法,压缩数据)
(msg ziface.IMessage)
| 29 | // Pack packs the message (compresses the data) |
| 30 | // (封包方法,压缩数据) |
| 31 | func (dp *DataPack) Pack(msg ziface.IMessage) ([]byte, error) { |
| 32 | // Create a buffer to store the bytes |
| 33 | // (创建一个存放bytes字节的缓冲) |
| 34 | dataBuff := bytes.NewBuffer([]byte{}) |
| 35 | |
| 36 | // Write the message ID |
| 37 | if err := binary.Write(dataBuff, binary.BigEndian, msg.GetMsgID()); err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | |
| 41 | // Write the data length |
| 42 | if err := binary.Write(dataBuff, binary.BigEndian, msg.GetDataLen()); err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | // Write the data |
| 47 | if err := binary.Write(dataBuff, binary.BigEndian, msg.GetData()); err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | return dataBuff.Bytes(), nil |
| 52 | } |
| 53 | |
| 54 | // Unpack unpacks the message (decompresses the data) |
| 55 | // (拆包方法,解压数据) |