-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.cs
More file actions
319 lines (272 loc) · 5.84 KB
/
Buffer.cs
File metadata and controls
319 lines (272 loc) · 5.84 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
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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Kaminari
{
public class Buffer
{
private byte[] _body;
private int _offset;
private int _index;
public Buffer()
{
_body = new byte[512];
_offset = 0;
_index = Packet.DataStart;
}
public Buffer(int buffer_size)
{
_body = new byte[buffer_size];
_offset = 0;
_index = Packet.DataStart;
}
public Buffer(Buffer other)
{
_body = (byte[])other._body.Clone();
_offset = other._offset;
_index = other._index;
}
public Buffer(Buffer other, int offset, int index)
{
_body = other._body;
_offset = offset;
_index = index;
}
public Buffer(byte[] data)
{
_body = data;
_offset = 0;
_index = 0;
}
// TODO(gpascualg): This is not really C#-y
public byte[] getData()
{
return _body;
}
public int getPosition()
{
return _index;
}
public void reset()
{
_offset = 0;
_index = 0;
}
public int getSize()
{
return _body.Length;
}
public void write(byte value)
{
write(_index, value);
_index += sizeof(byte);
}
public void write(sbyte value)
{
write(_index, (byte)value);
_index += sizeof(byte);
}
public void write(bool value)
{
write(_index, (byte)(value ? 1 : 0));
_index += sizeof(byte);
}
public void write(short value)
{
write(_index, (ushort)value);
_index += sizeof(ushort);
}
public void write(ushort value)
{
write(_index, value);
_index += sizeof(ushort);
}
public void write(int value)
{
write(_index, (uint)value);
_index += sizeof(uint);
}
public void write(uint value)
{
write(_index, value);
_index += sizeof(uint);
}
public void write(long value)
{
write(_index, (ulong)value);
_index += sizeof(ulong);
}
public void write(ulong value)
{
write(_index, value);
_index += sizeof(ulong);
}
public void write(float value)
{
write(_index, value);
_index += sizeof(float);
}
public void write(int position, byte value)
{
_body[_offset + position] = value;
}
public void write(int position, ushort value)
{
_body[_offset + position + 1] = (byte)(value >> 8);
_body[_offset + position] = (byte)(value);
}
public void write(int position, uint value)
{
_body[_offset + position + 3] = (byte)(value >> 24);
_body[_offset + position + 2] = (byte)(value >> 16);
_body[_offset + position + 1] = (byte)(value >> 8);
_body[_offset + position] = (byte)(value);
}
public void write(int position, ulong value)
{
_body[_offset + position + 7] = (byte)(value >> 56);
_body[_offset + position + 6] = (byte)(value >> 48);
_body[_offset + position + 5] = (byte)(value >> 40);
_body[_offset + position + 4] = (byte)(value >> 32);
_body[_offset + position + 3] = (byte)(value >> 24);
_body[_offset + position + 2] = (byte)(value >> 16);
_body[_offset + position + 1] = (byte)(value >> 8);
_body[_offset + position] = (byte)(value);
}
public void write(int position, float value)
{
byte[] bytes = BitConverter.GetBytes(value);
for (int i = 0; i < 4; ++i)
{
write(position + i, bytes[i]);
}
}
public void write(Packet packet)
{
Buffer other = packet.getData();
for (int i = 0; i < other.getPosition(); ++i)
{
_body[_offset + _index++] = other._body[i];
}
}
public void write(string str)
{
write((byte)str.Length);
for (int i = 0; i < str.Length; ++i)
{
write((byte)str[i]);
}
}
public sbyte readSbyte()
{
return (sbyte)readByte(_index++);
}
public byte readByte()
{
return readByte(_index++);
}
public byte readByte(int pos)
{
return _body[_offset + pos];
}
public short readShort()
{
short value = (short)readUshort(_index);
_index += sizeof(ushort);
return value;
}
public ushort readUshort()
{
ushort value = readUshort(_index);
_index += sizeof(ushort);
return value;
}
public ushort readUshort(int pos)
{
ushort result = (ushort)(_body[_offset + pos + 1] << 8 | _body[_offset + pos]);
return result;
}
public int readInt()
{
int value = (int)readUint(_index);
_index += sizeof(int);
return value;
}
public uint readUint()
{
uint value = readUint(_index);
_index += sizeof(uint);
return value;
}
public uint readUint(int pos)
{
uint result = (uint)(_body[_offset + pos + 3] << 24 |
_body[_offset + pos + 2] << 16 |
_body[_offset + pos + 1] << 8 |
_body[_offset + pos]);
return result;
}
public long readLong()
{
long value = (long)readUlong(_index);
_index += sizeof(long);
return value;
}
public ulong readUlong()
{
ulong value = readUlong(_index);
_index += sizeof(ulong);
return value;
}
public ulong readUlong(int pos)
{
ulong result = (ulong)(_body[_offset + pos + 7] << 56 |
_body[_offset + pos + 6] << 48 |
_body[_offset + pos + 5] << 40 |
_body[_offset + pos + 4] << 32 |
_body[_offset + pos + 3] << 24 |
_body[_offset + pos + 2] << 16 |
_body[_offset + pos + 1] << 8 |
_body[_offset + pos]);
return result;
}
public float readFloat()
{
float value = readFloat(_index);
_index += sizeof(float);
return value;
}
public float readFloat(int pos)
{
return Convert.ToSingle(BitConverter.ToSingle(_body, _offset + pos));
}
public double readDouble()
{
double value = readDouble(_index);
_index += sizeof(double);
return value;
}
public double readDouble(int pos)
{
return Convert.ToDouble(BitConverter.ToDouble(_body, _offset + pos));
}
public bool readBool()
{
return _body[_offset + _index++] == (byte)1;
}
public string readString()
{
string str = "";
int len = readByte();
for (int i = 0; i < len; ++i)
{
str += (char)readByte();
}
return str;
}
public byte peekByte()
{
return readByte(_index);
}
}
}