-
Notifications
You must be signed in to change notification settings - Fork 172
/
MessageReader.cs
185 lines (161 loc) · 4.14 KB
/
MessageReader.cs
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
namespace KBEngine
{
using UnityEngine;
using System;
using MessageID = System.UInt16;
using MessageLength = System.UInt16;
using MessageLengthEx = System.UInt32;
/*
消息阅读模块
从数据包流中分析出所有的消息包并将其交给对应的消息处理函数
*/
public class MessageReader
{
enum READ_STATE
{
// 消息ID
READ_STATE_MSGID = 0,
// 消息的长度65535以内
READ_STATE_MSGLEN = 1,
// 当上面的消息长度都无法到达要求时使用扩展长度
// uint32
READ_STATE_MSGLEN_EX = 2,
// 消息的内容
READ_STATE_BODY = 3
}
private MessageID msgid = 0;
private MessageLength msglen = 0;
private MessageLengthEx expectSize = 2;
private READ_STATE state = READ_STATE.READ_STATE_MSGID;
private MemoryStream stream = new MemoryStream();
public MessageReader()
{
}
public void process(byte[] datas, MessageLengthEx offset, MessageLengthEx length)
{
MessageLengthEx totallen = offset;
while(length > 0 && expectSize > 0)
{
if(state == READ_STATE.READ_STATE_MSGID)
{
if(length >= expectSize)
{
Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
totallen += expectSize;
stream.wpos += (int)expectSize;
length -= expectSize;
msgid = stream.readUint16();
stream.clear();
Message msg = Message.clientMessages[msgid];
if(msg.msglen == -1)
{
state = READ_STATE.READ_STATE_MSGLEN;
expectSize = 2;
}
else if(msg.msglen == 0)
{
// 如果是0个参数的消息,那么没有后续内容可读了,处理本条消息并且直接跳到下一条消息
#if UNITY_EDITOR
Dbg.profileStart(msg.name);
#endif
msg.handleMessage(stream);
#if UNITY_EDITOR
Dbg.profileEnd(msg.name);
#endif
state = READ_STATE.READ_STATE_MSGID;
expectSize = 2;
}
else
{
expectSize = (MessageLengthEx)msg.msglen;
state = READ_STATE.READ_STATE_BODY;
}
}
else
{
Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
stream.wpos += (int)length;
expectSize -= length;
break;
}
}
else if(state == READ_STATE.READ_STATE_MSGLEN)
{
if(length >= expectSize)
{
Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
totallen += expectSize;
stream.wpos += (int)expectSize;
length -= expectSize;
msglen = stream.readUint16();
stream.clear();
// 长度扩展
if(msglen >= 65535)
{
state = READ_STATE.READ_STATE_MSGLEN_EX;
expectSize = 4;
}
else
{
state = READ_STATE.READ_STATE_BODY;
expectSize = msglen;
}
}
else
{
Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
stream.wpos += (int)length;
expectSize -= length;
break;
}
}
else if(state == READ_STATE.READ_STATE_MSGLEN_EX)
{
if(length >= expectSize)
{
Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
totallen += expectSize;
stream.wpos += (int)expectSize;
length -= expectSize;
expectSize = stream.readUint32();
stream.clear();
state = READ_STATE.READ_STATE_BODY;
}
else
{
Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
stream.wpos += (int)length;
expectSize -= length;
break;
}
}
else if(state == READ_STATE.READ_STATE_BODY)
{
if(length >= expectSize)
{
stream.append (datas, totallen, expectSize);
totallen += expectSize;
length -= expectSize;
Message msg = Message.clientMessages[msgid];
#if UNITY_EDITOR
Dbg.profileStart(msg.name);
#endif
msg.handleMessage(stream);
#if UNITY_EDITOR
Dbg.profileEnd(msg.name);
#endif
stream.clear();
state = READ_STATE.READ_STATE_MSGID;
expectSize = 2;
}
else
{
stream.append (datas, totallen, length);
expectSize -= length;
break;
}
}
}
}
}
}