forked from hunterzonewu/unity-decompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetBuffer.cs
More file actions
178 lines (158 loc) · 5.77 KB
/
NetBuffer.cs
File metadata and controls
178 lines (158 loc) · 5.77 KB
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
// Decompiled with JetBrains decompiler
// Type: UnityEngine.Networking.NetBuffer
// Assembly: UnityEngine.Networking, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8B34E19C-EF53-416E-AE36-35C45BAFD2DE
// Assembly location: C:\Users\Blake\sandbox\unity\test-project\Library\UnityAssemblies\UnityEngine.Networking.dll
using System;
namespace UnityEngine.Networking
{
internal class NetBuffer
{
private const int k_InitialSize = 64;
private const float k_GrowthFactor = 1.5f;
private const int k_BufferSizeWarning = 134217728;
private byte[] m_Buffer;
private uint m_Pos;
public uint Position
{
get
{
return this.m_Pos;
}
}
public NetBuffer()
{
this.m_Buffer = new byte[64];
}
public NetBuffer(byte[] buffer)
{
this.m_Buffer = buffer;
}
public byte ReadByte()
{
if ((long) this.m_Pos >= (long) this.m_Buffer.Length)
throw new IndexOutOfRangeException("NetworkReader:ReadByte out of range:" + this.ToString());
return this.m_Buffer[(IntPtr) this.m_Pos++];
}
public void ReadBytes(byte[] buffer, uint count)
{
if ((long) (this.m_Pos + count) > (long) this.m_Buffer.Length)
throw new IndexOutOfRangeException("NetworkReader:ReadBytes out of range: (" + (object) count + ") " + this.ToString());
for (ushort index = 0; (uint) index < count; ++index)
buffer[(int) index] = this.m_Buffer[(IntPtr) (this.m_Pos + (uint) index)];
this.m_Pos += count;
}
public void ReadChars(char[] buffer, uint count)
{
if ((long) (this.m_Pos + count) > (long) this.m_Buffer.Length)
throw new IndexOutOfRangeException("NetworkReader:ReadChars out of range: (" + (object) count + ") " + this.ToString());
for (ushort index = 0; (uint) index < count; ++index)
buffer[(int) index] = (char) this.m_Buffer[(IntPtr) (this.m_Pos + (uint) index)];
this.m_Pos += count;
}
internal ArraySegment<byte> AsArraySegment()
{
return new ArraySegment<byte>(this.m_Buffer, 0, (int) this.m_Pos);
}
public void WriteByte(byte value)
{
this.WriteCheckForSpace((ushort) 1);
this.m_Buffer[(IntPtr) this.m_Pos] = value;
++this.m_Pos;
}
public void WriteByte2(byte value0, byte value1)
{
this.WriteCheckForSpace((ushort) 2);
this.m_Buffer[(IntPtr) this.m_Pos] = value0;
this.m_Buffer[(IntPtr) (this.m_Pos + 1U)] = value1;
this.m_Pos += 2U;
}
public void WriteByte4(byte value0, byte value1, byte value2, byte value3)
{
this.WriteCheckForSpace((ushort) 4);
this.m_Buffer[(IntPtr) this.m_Pos] = value0;
this.m_Buffer[(IntPtr) (this.m_Pos + 1U)] = value1;
this.m_Buffer[(IntPtr) (this.m_Pos + 2U)] = value2;
this.m_Buffer[(IntPtr) (this.m_Pos + 3U)] = value3;
this.m_Pos += 4U;
}
public void WriteByte8(byte value0, byte value1, byte value2, byte value3, byte value4, byte value5, byte value6, byte value7)
{
this.WriteCheckForSpace((ushort) 8);
this.m_Buffer[(IntPtr) this.m_Pos] = value0;
this.m_Buffer[(IntPtr) (this.m_Pos + 1U)] = value1;
this.m_Buffer[(IntPtr) (this.m_Pos + 2U)] = value2;
this.m_Buffer[(IntPtr) (this.m_Pos + 3U)] = value3;
this.m_Buffer[(IntPtr) (this.m_Pos + 4U)] = value4;
this.m_Buffer[(IntPtr) (this.m_Pos + 5U)] = value5;
this.m_Buffer[(IntPtr) (this.m_Pos + 6U)] = value6;
this.m_Buffer[(IntPtr) (this.m_Pos + 7U)] = value7;
this.m_Pos += 8U;
}
public void WriteBytesAtOffset(byte[] buffer, ushort targetOffset, ushort count)
{
uint num = (uint) count + (uint) targetOffset;
this.WriteCheckForSpace((ushort) num);
if ((int) targetOffset == 0 && (int) count == buffer.Length)
{
buffer.CopyTo((Array) this.m_Buffer, (long) this.m_Pos);
}
else
{
for (int index = 0; index < (int) count; ++index)
this.m_Buffer[(int) targetOffset + index] = buffer[index];
}
if (num <= this.m_Pos)
return;
this.m_Pos = num;
}
public void WriteBytes(byte[] buffer, ushort count)
{
this.WriteCheckForSpace(count);
if ((int) count == buffer.Length)
{
buffer.CopyTo((Array) this.m_Buffer, (long) this.m_Pos);
}
else
{
for (int index = 0; index < (int) count; ++index)
this.m_Buffer[(long) this.m_Pos + (long) index] = buffer[index];
}
this.m_Pos += (uint) count;
}
private void WriteCheckForSpace(ushort count)
{
if ((long) (this.m_Pos + (uint) count) < (long) this.m_Buffer.Length)
return;
int length = (int) ((double) this.m_Buffer.Length * 1.5);
while ((long) (this.m_Pos + (uint) count) >= (long) length)
{
length = (int) ((double) length * 1.5);
if (length > 134217728)
Debug.LogWarning((object) ("NetworkBuffer size is " + (object) length + " bytes!"));
}
byte[] numArray = new byte[length];
this.m_Buffer.CopyTo((Array) numArray, 0);
this.m_Buffer = numArray;
}
public void FinishMessage()
{
ushort num = (ushort) (this.m_Pos - 4U);
this.m_Buffer[0] = (byte) ((uint) num & (uint) byte.MaxValue);
this.m_Buffer[1] = (byte) ((int) num >> 8 & (int) byte.MaxValue);
}
public void SeekZero()
{
this.m_Pos = 0U;
}
public void Replace(byte[] buffer)
{
this.m_Buffer = buffer;
this.m_Pos = 0U;
}
public override string ToString()
{
return string.Format("NetBuf sz:{0} pos:{1}", (object) this.m_Buffer.Length, (object) this.m_Pos);
}
}
}