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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Hazel
{
/// <summary>
/// 嵌套结构的Message
/// 结构:
/// ------------------------------------
/// 2bytes (ushort) 包长度
/// 1bytes (tag) 协议ID,在AmongUS里是tags.cs里定义的tag和subtag
/// ------------------------------------
/// 数据 包括嵌套的子协议
/// ------------------------------------
/// </summary>
public class MessageWriter : IRecyclable
{
public static int BufferSize = 64000;
public static readonly ObjectPool<MessageWriter> WriterPool = new ObjectPool<MessageWriter>(() => new MessageWriter(BufferSize));
public byte[] Buffer;
public int Length; // 总长度
public int Position; // 写入游标
public SendOption SendOption { get; private set; }
private Stack<int> messageStarts = new Stack<int>();
public MessageWriter(byte[] buffer)
{
this.Buffer = buffer;
this.Length = this.Buffer.Length;
}
///
public MessageWriter(int bufferSize)
{
this.Buffer = new byte[bufferSize];
}
/// <summary>
/// 去掉header
/// </summary>
/// <param name="includeHeader"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] ToByteArray(bool includeHeader)
{
if (includeHeader)
{
byte[] output = new byte[this.Length];
System.Buffer.BlockCopy(this.Buffer, 0, output, 0, this.Length);
return output;
}
else
{
switch (this.SendOption)
{
case SendOption.Reliable:
{
byte[] output = new byte[this.Length - 3];
System.Buffer.BlockCopy(this.Buffer, 3, output, 0, this.Length - 3);
return output;
}
case SendOption.None:
{
byte[] output = new byte[this.Length - 1];
System.Buffer.BlockCopy(this.Buffer, 1, output, 0, this.Length - 1);
return output;
}
}
}
throw new NotImplementedException();
}
///
/// <param name="sendOption">The option specifying how the message should be sent.</param>
public static MessageWriter Get(SendOption sendOption = SendOption.None)
{
var output = WriterPool.GetObject();
output.Clear(sendOption);
return output;
}
public bool HasBytes(int expected)
{
if (this.SendOption == SendOption.None)
{
return this.Length > 1 + expected;
}
return this.Length > 3 + expected;
}
///
public void StartMessage(byte typeFlag)
{
var messageStart = this.Position;
messageStarts.Push(messageStart);
this.Buffer[messageStart] = 0;
this.Buffer[messageStart + 1] = 0;
this.Position += 2;
this.Write(typeFlag);
}
///
public void EndMessage()
{
var lastMessageStart = messageStarts.Pop();
ushort length = (ushort)(this.Position - lastMessageStart - 3); // Minus length and type byte
this.Buffer[lastMessageStart] = (byte)length;
this.Buffer[lastMessageStart + 1] = (byte)(length >> 8);
}
///
public void CancelMessage()
{
this.Position = this.messageStarts.Pop();
this.Length = this.Position;
}
public void Clear(SendOption sendOption)
{
Array.Clear(this.Buffer, 0, this.Buffer.Length);
this.messageStarts.Clear();
this.SendOption = sendOption;
this.Buffer[0] = (byte)sendOption;
switch (sendOption)
{
default:
case SendOption.None:
this.Length = this.Position = 1;
break;
case SendOption.Reliable:
this.Length = this.Position = 3;
break;
}
}
///
public void Recycle()
{
this.Position = this.Length = 0;
WriterPool.PutObject(this);
}
#region WriteMethods
public void CopyFrom(MessageReader target)
{
int offset, length;
if (target.Tag == byte.MaxValue)
{
offset = target.Offset;
length = target.Length;
}
else
{
offset = target.Offset - 3;
length = target.Length + 3;
}
System.Buffer.BlockCopy(target.Buffer, offset, this.Buffer, this.Position, length);
this.Position += length;
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(bool value)
{
this.Buffer[this.Position++] = (byte)(value ? 1 : 0);
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(sbyte value)
{
this.Buffer[this.Position++] = (byte)value;
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(byte value)
{
this.Buffer[this.Position++] = value;
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(short value)
{
this.Buffer[this.Position++] = (byte)value;
this.Buffer[this.Position++] = (byte)(value >> 8);
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(ushort value)
{
this.Buffer[this.Position++] = (byte)value;
this.Buffer[this.Position++] = (byte)(value >> 8);
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(uint value)
{
this.Buffer[this.Position++] = (byte)value;
this.Buffer[this.Position++] = (byte)(value >> 8);
this.Buffer[this.Position++] = (byte)(value >> 16);
this.Buffer[this.Position++] = (byte)(value >> 24);
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(int value)
{
this.Buffer[this.Position++] = (byte)value;
this.Buffer[this.Position++] = (byte)(value >> 8);
this.Buffer[this.Position++] = (byte)(value >> 16);
this.Buffer[this.Position++] = (byte)(value >> 24);
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(ulong value)
{
this.Buffer[this.Position++] = (byte)value;
this.Buffer[this.Position++] = (byte)(value >> 8);
this.Buffer[this.Position++] = (byte)(value >> 16);
this.Buffer[this.Position++] = (byte)(value >> 24);
this.Buffer[this.Position++] = (byte)(value >> 32);
this.Buffer[this.Position++] = (byte)(value >> 40);
this.Buffer[this.Position++] = (byte)(value >> 48);
this.Buffer[this.Position++] = (byte)(value >> 56);
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(long value)
{
this.Buffer[this.Position++] = (byte)value;
this.Buffer[this.Position++] = (byte)(value >> 8);
this.Buffer[this.Position++] = (byte)(value >> 16);
this.Buffer[this.Position++] = (byte)(value >> 24);
this.Buffer[this.Position++] = (byte)(value >> 32);
this.Buffer[this.Position++] = (byte)(value >> 40);
this.Buffer[this.Position++] = (byte)(value >> 48);
this.Buffer[this.Position++] = (byte)(value >> 56);
if (this.Position > this.Length) this.Length = this.Position;
}
public unsafe void Write(float value)
{
fixed (byte* ptr = &this.Buffer[this.Position])
{
byte* valuePtr = (byte*)&value;
*ptr = *valuePtr;
*(ptr + 1) = *(valuePtr + 1);
*(ptr + 2) = *(valuePtr + 2);
*(ptr + 3) = *(valuePtr + 3);
}
this.Position += 4;
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(string value)
{
var bytes = UTF8Encoding.UTF8.GetBytes(value);
this.WritePacked(bytes.Length);
this.Write(bytes);
}
public void WriteBytesAndSize(byte[] bytes)
{
this.WritePacked((uint)bytes.Length);
this.Write(bytes);
}
public void WriteBytesAndSize(byte[] bytes, int length)
{
this.WritePacked((uint)length);
this.Write(bytes, length);
}
public void WriteBytesAndSize(byte[] bytes, int offset, int length)
{
this.WritePacked((uint)length);
this.Write(bytes, offset, length);
}
public void Write(byte[] bytes)
{
Array.Copy(bytes, 0, this.Buffer, this.Position, bytes.Length);
this.Position += bytes.Length;
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(byte[] bytes, int offset, int length)
{
Array.Copy(bytes, offset, this.Buffer, this.Position, length);
this.Position += length;
if (this.Position > this.Length) this.Length = this.Position;
}
public void Write(byte[] bytes, int length)
{
Array.Copy(bytes, 0, this.Buffer, this.Position, length);
this.Position += length;
if (this.Position > this.Length) this.Length = this.Position;
}
///
public void WritePacked(int value)
{
this.WritePacked((uint)value);
}
///
public void WritePacked(uint value)
{
do
{
byte b = (byte)(value & 0xFF);
if (value >= 0x80)
{
b |= 0x80;
}
this.Write(b);
value >>= 7;
} while (value > 0);
}
#endregion
public void Write(MessageWriter msg, bool includeHeader)
{
int offset = 0;
if (!includeHeader)
{
switch (msg.SendOption)
{
case SendOption.None:
offset = 1;
break;
case SendOption.Reliable:
offset = 3;
break;
}
}
this.Write(msg.Buffer, offset, msg.Length - offset);
}
public unsafe static bool IsLittleEndian()
{
byte b;
unsafe
{
int i = 1;
byte* bp = (byte*)&i;
b = *bp;
}
return b == 1;
}
}
}
|