-
Notifications
You must be signed in to change notification settings - Fork 172
/
Bundle.cs
218 lines (183 loc) · 4.23 KB
/
Bundle.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
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
namespace KBEngine
{
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/*
这个模块将多个数据包打捆在一起
由于每个数据包都有最大上限, 向Bundle中写入大量数据将会在内部产生多个MemoryStream
在send时会全部发送出去
*/
public class Bundle : ObjectPool<Bundle>
{
public MemoryStream stream = new MemoryStream();
public List<MemoryStream> streamList = new List<MemoryStream>();
public int numMessage = 0;
public int messageLength = 0;
public Message msgtype = null;
private int _curMsgStreamIndex = 0;
public Bundle()
{
}
public void clear()
{
// 把不用的MemoryStream放回缓冲池,以减少垃圾回收的消耗
for (int i = 0; i < streamList.Count; ++i)
{
if (stream != streamList[i])
streamList[i].reclaimObject();
}
streamList.Clear();
if(stream != null)
stream.clear();
else
stream = MemoryStream.createObject();
numMessage = 0;
messageLength = 0;
msgtype = null;
_curMsgStreamIndex = 0;
}
/// <summary>
/// 把自己放回缓冲池
/// </summary>
public void reclaimObject()
{
clear();
reclaimObject(this);
}
public void newMessage(Message mt)
{
fini(false);
msgtype = mt;
numMessage += 1;
writeUint16(msgtype.id);
if(msgtype.msglen == -1)
{
writeUint16(0);
messageLength = 0;
}
_curMsgStreamIndex = 0;
}
public void writeMsgLength()
{
if(msgtype.msglen != -1)
return;
MemoryStream stream = this.stream;
if(_curMsgStreamIndex > 0)
{
stream = streamList[streamList.Count - _curMsgStreamIndex];
}
stream.data()[2] = (Byte)(messageLength & 0xff);
stream.data()[3] = (Byte)(messageLength >> 8 & 0xff);
}
public void fini(bool issend)
{
if(numMessage > 0)
{
writeMsgLength();
streamList.Add(stream);
stream = MemoryStream.createObject();
}
if(issend)
{
numMessage = 0;
msgtype = null;
}
_curMsgStreamIndex = 0;
}
public void send(NetworkInterface networkInterface)
{
fini(true);
if(networkInterface.valid())
{
for(int i=0; i<streamList.Count; i++)
{
MemoryStream tempStream = streamList[i];
networkInterface.send(tempStream);
}
}
else
{
Dbg.ERROR_MSG("Bundle::send: networkInterface invalid!");
}
// 我们认为,发送完成,就视为这个bundle不再使用了,
// 所以我们会把它放回对象池,以减少垃圾回收带来的消耗,
// 如果需要继续使用,应该重新Bundle.createObject(),
// 如果外面不重新createObject()而直接使用,就可能会出现莫名的问题,
// 仅以此备注,警示使用者。
reclaimObject();
}
public void checkStream(int v)
{
if(v > stream.space())
{
streamList.Add(stream);
stream = MemoryStream.createObject();
++ _curMsgStreamIndex;
}
messageLength += v;
}
//---------------------------------------------------------------------------------
public void writeInt8(SByte v)
{
checkStream(1);
stream.writeInt8(v);
}
public void writeInt16(Int16 v)
{
checkStream(2);
stream.writeInt16(v);
}
public void writeInt32(Int32 v)
{
checkStream(4);
stream.writeInt32(v);
}
public void writeInt64(Int64 v)
{
checkStream(8);
stream.writeInt64(v);
}
public void writeUint8(Byte v)
{
checkStream(1);
stream.writeUint8(v);
}
public void writeUint16(UInt16 v)
{
checkStream(2);
stream.writeUint16(v);
}
public void writeUint32(UInt32 v)
{
checkStream(4);
stream.writeUint32(v);
}
public void writeUint64(UInt64 v)
{
checkStream(8);
stream.writeUint64(v);
}
public void writeFloat(float v)
{
checkStream(4);
stream.writeFloat(v);
}
public void writeDouble(double v)
{
checkStream(8);
stream.writeDouble(v);
}
public void writeString(string v)
{
checkStream(v.Length + 1);
stream.writeString(v);
}
public void writeBlob(byte[] v)
{
checkStream(v.Length + 4);
stream.writeBlob(v);
}
}
}