-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcpack.go
86 lines (79 loc) · 2.16 KB
/
tcpack.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
package tcpack
import (
"bytes"
"encoding/binary"
"io"
"log"
"net"
)
// IMsgPack is an interface that defined a packager,
// which carries HeadLen, and provides Pack() and Unpack() method.
type IMsgPack interface {
// Get the head length of the message package.
GetHeadLen() uint32
// Pack returns bytes stream packed from a message.
Pack(Imessage) (uint32, error)
// Unpack returns a message from bytes stream.
Unpack() (Imessage, error)
}
// MsgPack implements the interface IMsgPack,
// carrying HeadLen and conn for Pack() and Unpack().
type MsgPack struct {
headLen uint32
conn net.Conn
}
// NewMsgPack returns a packager *MsgPack.
func NewMsgPack(headlen uint32, conn net.Conn) *MsgPack {
return &MsgPack{
headLen: headlen,
conn: conn,
}
}
// GetHeadLen return HeadLen of the message.
func (mp *MsgPack) GetHeadLen() uint32 {
return mp.headLen
}
// Pack packs a message to bytes stream and sends it.
func (mp *MsgPack) Pack(msg Imessage) (uint32, error) {
buffer := bytes.NewBuffer([]byte{})
if err := binary.Write(buffer, binary.LittleEndian, msg.GetDataLen()); err != nil {
return 0, err
}
if err := binary.Write(buffer, binary.LittleEndian, msg.GetMsgId()); err != nil {
return 0, err
}
if err := binary.Write(buffer, binary.LittleEndian, msg.GetMsgData()); err != nil {
return 0, err
}
num, err := mp.conn.Write(buffer.Bytes())
if err != nil {
return 0, err
}
return uint32(num), nil
}
// Unpack unpacks a certain length bytes stream to a message.
func (mp *MsgPack) Unpack() (Imessage, error) {
headDate := make([]byte, mp.GetHeadLen())
_, err := io.ReadFull(mp.conn, headDate)
if err != nil {
log.Println("read headData failed:", err)
return nil, err
}
buffer := bytes.NewReader(headDate)
msg := NewMessage(0, 0, nil)
if err := binary.Read(buffer, binary.LittleEndian, &msg.DataLen); err != nil {
return nil, err
}
if err := binary.Read(buffer, binary.LittleEndian, &msg.Id); err != nil {
return nil, err
}
if msg.GetDataLen() > 0 {
msg.Data = make([]byte, msg.GetDataLen())
_, err := io.ReadFull(mp.conn, msg.Data)
if err != nil {
log.Println("read msgData failed:", err)
return nil, err
}
}
return msg, nil
}