-
Notifications
You must be signed in to change notification settings - Fork 114
/
msgque_msg.go
277 lines (245 loc) · 5.2 KB
/
msgque_msg.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package antnet
import (
"fmt"
"unsafe"
)
const (
MsgHeadSize = 12
)
const (
FlagEncrypt = 1 << 0 //数据是经过加密的
FlagCompress = 1 << 1 //数据是经过压缩的
FlagCanDiscard = 1 << 2 //消息是否可以丢弃,基于tcp网络的帧同步中,包可以主动丢弃
FlagNeedAck = 1 << 3 //消息需要确认
FlagAck = 1 << 4 //确认消息
FlagReSend = 1 << 5 //重发消息
FlagClient = 1 << 6 //消息来自客服端,用于判断index来之服务器还是其他玩家
FlagUdpProxy = 1 << 7 //udp代理
)
var MaxMsgDataSize uint32 = 1024 * 1024
type MessageHead struct {
Len uint32 //数据长度
Error uint16 //错误码
Cmd uint8 //命令
Act uint8 //动作
Index uint16 //序号
Flags uint16 //标记
forever bool
data []byte
}
func (r *MessageHead) Bytes() []byte {
if r.forever && r.data != nil {
return r.data
}
r.data = make([]byte, MsgHeadSize)
phead := (*MessageHead)(unsafe.Pointer(&r.data[0]))
phead.Len = r.Len
phead.Error = r.Error
phead.Cmd = r.Cmd
phead.Act = r.Act
phead.Index = r.Index
phead.Flags = r.Flags
return r.data
}
func (r *MessageHead) FastBytes(data []byte) []byte {
phead := (*MessageHead)(unsafe.Pointer(&data[0]))
phead.Len = r.Len
phead.Error = r.Error
phead.Cmd = r.Cmd
phead.Act = r.Act
phead.Index = r.Index
phead.Flags = r.Flags
return data
}
func (r *MessageHead) BytesWithData(wdata []byte) []byte {
if r.forever && r.data != nil {
return r.data
}
r.Len = uint32(len(wdata))
r.data = make([]byte, MsgHeadSize+r.Len)
phead := (*MessageHead)(unsafe.Pointer(&r.data[0]))
phead.Len = r.Len
phead.Error = r.Error
phead.Cmd = r.Cmd
phead.Act = r.Act
phead.Index = r.Index
phead.Flags = r.Flags
if wdata != nil {
copy(r.data[MsgHeadSize:], wdata)
}
return r.data
}
func (r *MessageHead) FromBytes(data []byte) error {
if len(data) < MsgHeadSize {
return ErrMsgLenTooShort
}
phead := (*MessageHead)(unsafe.Pointer(&data[0]))
r.Len = phead.Len
r.Error = phead.Error
r.Cmd = phead.Cmd
r.Act = phead.Act
r.Index = phead.Index
r.Flags = phead.Flags
if r.Len > MaxMsgDataSize {
return ErrMsgLenTooLong
}
return nil
}
func (r *MessageHead) CmdAct() int {
return CmdAct(r.Cmd, r.Act)
}
func (r *MessageHead) Tag() int {
return Tag(r.Cmd, r.Act, r.Index)
}
func (r *MessageHead) String() string {
return fmt.Sprintf("Len:%v Error:%v Cmd:%v Act:%v Index:%v Flags:%v", r.Len, r.Error, r.Cmd, r.Act, r.Index, r.Flags)
}
func NewMessageHead(data []byte) *MessageHead {
head := &MessageHead{}
if err := head.FromBytes(data); err != nil {
return nil
}
return head
}
func MessageHeadFromByte(data []byte) *MessageHead {
if len(data) < MsgHeadSize {
return nil
}
phead := new(*MessageHead)
*phead = (*MessageHead)(unsafe.Pointer(&data[0]))
if (*phead).Len > MaxMsgDataSize {
return nil
}
return *phead
}
type Message struct {
Head *MessageHead //消息头,可能为nil
Data []byte //消息数据
IMsgParser //解析器
User interface{} //用户自定义数据
}
func (r *Message) CmdAct() int {
if r.Head != nil {
return CmdAct(r.Head.Cmd, r.Head.Act)
}
return 0
}
func (r *Message) Len() uint32 {
if r.Head != nil {
return r.Head.Len
}
return 0
}
func (r *Message) Error() uint16 {
if r.Head != nil {
return r.Head.Error
}
return 0
}
func (r *Message) Cmd() uint8 {
if r.Head != nil {
return r.Head.Cmd
}
return 0
}
func (r *Message) Act() uint8 {
if r.Head != nil {
return r.Head.Act
}
return 0
}
func (r *Message) Index() uint16 {
if r.Head != nil {
return r.Head.Index
}
return 0
}
func (r *Message) Flags() uint16 {
if r.Head != nil {
return r.Head.Flags
}
return 0
}
func (r *Message) Tag() int {
if r.Head != nil {
return Tag(r.Head.Cmd, r.Head.Act, r.Head.Index)
}
return 0
}
func (r *Message) Bytes() []byte {
if r.Head != nil {
if r.Data != nil {
return r.Head.BytesWithData(r.Data)
}
return r.Head.Bytes()
}
return r.Data
}
func (r *Message) CopyTag(old *Message) *Message {
if r.Head != nil && old.Head != nil {
r.Head.Cmd = old.Head.Cmd
r.Head.Act = old.Head.Act
r.Head.Index = old.Head.Index
}
return r
}
func NewErrMsg(err error) *Message {
errcode, ok := errIdMap.Load(err)
if !ok {
errcode, _ = errIdMap.Load(ErrErrIdNotFound)
}
return &Message{
Head: &MessageHead{
Error: errcode.(uint16),
},
}
}
func NewStrMsg(str string) *Message {
return &Message{
Data: []byte(str),
}
}
func NewDataMsg(data []byte) *Message {
return &Message{
Head: &MessageHead{
Len: uint32(len(data)),
},
Data: data,
}
}
func NewMsg(cmd, act uint8, index, err uint16, data []byte) *Message {
return &Message{
Head: &MessageHead{
Len: uint32(len(data)),
Error: err,
Cmd: cmd,
Act: act,
Index: index,
},
Data: data,
}
}
func NewForverMsg(cmd, act uint8, index, err uint16, data []byte) *Message {
msg := &Message{
Head: &MessageHead{
Len: uint32(len(data)),
Error: err,
Cmd: cmd,
Act: act,
Index: index,
forever: true,
},
Data: data,
}
msg.Head.data = msg.Bytes()
return msg
}
func NewTagMsg(cmd, act uint8, index uint16) *Message {
return &Message{
Head: &MessageHead{
Cmd: cmd,
Act: act,
Index: index,
},
}
}