/
gateproto.go
103 lines (91 loc) · 2.8 KB
/
gateproto.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package messages
import (
"encoding/binary"
"fmt"
"github.com/buguang01/util"
"github.com/gogo/protobuf/proto"
)
type GateProtoMessageHandle struct {
msgHead uint32 //消息头
msgMaxLen uint16 //消息最大长度
routelist map[uint32]interface{} //消息路由列表
}
func GateProtoMessageSetMsgHead(msghead uint32) options {
return func(msghandle IMessageHandle) {
msghandle.(*GateProtoMessageHandle).msgHead = msghead
}
}
func GateProtoMessageSetMsgMaxLen(max uint16) options {
return func(msghandle IMessageHandle) {
msghandle.(*GateProtoMessageHandle).msgMaxLen = max
}
}
func GateProtoMessageHandleNew(opts ...options) (msghandle *GateProtoMessageHandle) {
msghandle = &GateProtoMessageHandle{
routelist: make(map[uint32]interface{}),
msgHead: uint32(0x12340000),
msgMaxLen: ^uint16(0),
}
for _, opt := range opts {
opt(msghandle)
}
return msghandle
}
func (msghandle *GateProtoMessageHandle) GateMarshal(gate IGateMessage, data interface{}) ([]byte, error) {
return nil, nil
}
//编码
func (msghandle *GateProtoMessageHandle) Marshal(msgid uint32, data interface{}) ([]byte, error) {
return nil, nil
// buff := &bytes.Buffer{}
// in_data, err := json.Marshal(data)
// tmpbuf := make([]byte, 4)
// pklen := uint32(len(in_data)+8) | msghandle.msgHead
// binary.BigEndian.PutUint32(tmpbuf, pklen)
// buff.Write(tmpbuf)
// binary.BigEndian.PutUint32(tmpbuf, msgid)
// buff.Write(tmpbuf)
// buff.Write(in_data)
// return buff.Bytes(), err
}
//解码
func (msghandle *GateProtoMessageHandle) Unmarshal(buff []byte) (data interface{}, err error) {
pklen := binary.BigEndian.Uint32(buff[:4])
pklen = pklen ^ msghandle.msgHead
if pklen != uint32(len(buff)) {
return nil, fmt.Errorf("MsgLen Error:%d.", pklen)
}
buff = buff[4:]
msgid := binary.BigEndian.Uint32(buff[:4])
msget, err := msghandle.GetRoute(msgid)
if err != nil {
return nil, err
}
buff = buff[4:]
err = proto.Unmarshal(buff, msget.(proto.Message))
return msget, err
}
//设置消息路由
func (msghandle *GateProtoMessageHandle) SetRoute(msgid uint32, msg interface{}) {
msghandle.routelist[msgid] = msg
}
//按消息拿出消息处理实例
func (msghandle *GateProtoMessageHandle) GetRoute(msgid uint32) (result interface{}, err error) {
if msget, ok := msghandle.routelist[msgid]; ok {
return util.ReflectNew(msget)
}
return nil, fmt.Errorf("Not exist MsgID:%d.", msgid)
}
//一个消息是否收完了
//返回这个消息应该的长度,和是否收完的信息
func (msghandle *GateProtoMessageHandle) CheckMaxLenVaild(buff []byte) (msglen uint32, ok bool) {
pklen := binary.BigEndian.Uint32(buff[:4])
pklen = pklen ^ msghandle.msgHead
if pklen > uint32(msghandle.msgMaxLen) {
return 0, false
}
if pklen > uint32(len(buff)) {
return pklen, false
}
return pklen, true
}