-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormal_packer.go
64 lines (58 loc) · 1.42 KB
/
normal_packer.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
package network
import (
"encoding/binary"
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"io"
"net"
"time"
)
type NormalPacker struct {
ByteOrder binary.ByteOrder
}
func (p *NormalPacker) Pack(msgID uint16, msg interface{}) ([]byte, error) {
pbMsg, ok := msg.(proto.Message)
if !ok {
return []byte{}, fmt.Errorf("msg is not protobuf message")
}
data, err := proto.Marshal(pbMsg)
if err != nil {
return data, err
}
buffer := make([]byte, 8+8+len(data))
p.ByteOrder.PutUint64(buffer[0:8], uint64(len(buffer)))
p.ByteOrder.PutUint64(buffer[8:16], uint64(msgID))
copy(buffer[16:], data)
return buffer, nil
}
func (p *NormalPacker) Read(conn *TcpSession) ([]byte, error) {
err := conn.Conn.(*net.TCPConn).SetReadDeadline(time.Now().Add(time.Second * 10))
if err != nil {
return nil, err
}
buffer := make([]byte, 8+8)
if _, err := io.ReadFull(conn.Conn, buffer); err != nil {
return nil, err
}
totalSize := p.ByteOrder.Uint64(buffer[:8])
dataSize := totalSize - 8 - 8
data := make([]byte, 8+dataSize)
copy(data[:8], buffer[8:])
if _, err := io.ReadFull(conn.Conn, data[8:]); err != nil {
return nil, err
}
return data, nil
}
// Unpack id + protobuf data
func (p *NormalPacker) Unpack(data []byte) (*Message, error) {
if len(data) < 2 {
return nil, errors.New("protobuf data too short")
}
msgID := p.ByteOrder.Uint16(data[:2])
msg := &Message{
ID: uint64(msgID),
Data: data[2:],
}
return msg, nil
}