forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetDataReader.cs
More file actions
769 lines (659 loc) · 18.8 KB
/
NetDataReader.cs
File metadata and controls
769 lines (659 loc) · 18.8 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
using System;
using System.Net;
using System.Runtime.CompilerServices;
namespace LiteNetLib.Utils
{
public class NetDataReader
{
protected byte[] _data;
protected int _position;
protected int _dataSize;
private int _offset;
public byte[] RawData
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _data;
}
public int RawDataSize
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _dataSize;
}
public int UserDataOffset
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _offset;
}
public int UserDataSize
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _dataSize - _offset;
}
public bool IsNull
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _data == null;
}
public int Position
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _position;
}
public bool EndOfData
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _position == _dataSize;
}
public int AvailableBytes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _dataSize - _position;
}
public void SkipBytes(int count)
{
_position += count;
}
public void SetPosition(int position)
{
_position = position;
}
public void SetSource(NetDataWriter dataWriter)
{
_data = dataWriter.Data;
_position = 0;
_offset = 0;
_dataSize = dataWriter.Length;
}
public void SetSource(byte[] source)
{
_data = source;
_position = 0;
_offset = 0;
_dataSize = source.Length;
}
public void SetSource(byte[] source, int offset, int maxSize)
{
_data = source;
_position = offset;
_offset = offset;
_dataSize = maxSize;
}
public NetDataReader()
{
}
public NetDataReader(NetDataWriter writer)
{
SetSource(writer);
}
public NetDataReader(byte[] source)
{
SetSource(source);
}
public NetDataReader(byte[] source, int offset, int maxSize)
{
SetSource(source, offset, maxSize);
}
#region GetMethods
public void Get<T>(out T result) where T : struct, INetSerializable
{
result = default(T);
result.Deserialize(this);
}
public void Get<T>(out T result, Func<T> constructor) where T : class, INetSerializable
{
result = constructor();
result.Deserialize(this);
}
public void Get(out IPEndPoint result)
{
result = GetNetEndPoint();
}
public void Get(out byte result)
{
result = GetByte();
}
public void Get(out sbyte result)
{
result = (sbyte)GetByte();
}
public void Get(out bool result)
{
result = GetBool();
}
public void Get(out char result)
{
result = GetChar();
}
public void Get(out ushort result)
{
result = GetUShort();
}
public void Get(out short result)
{
result = GetShort();
}
public void Get(out ulong result)
{
result = GetULong();
}
public void Get(out long result)
{
result = GetLong();
}
public void Get(out uint result)
{
result = GetUInt();
}
public void Get(out int result)
{
result = GetInt();
}
public void Get(out double result)
{
result = GetDouble();
}
public void Get(out float result)
{
result = GetFloat();
}
public void Get(out string result)
{
result = GetString();
}
public void Get(out string result, int maxLength)
{
result = GetString(maxLength);
}
public IPEndPoint GetNetEndPoint()
{
string host = GetString(1000);
int port = GetInt();
return NetUtils.MakeEndPoint(host, port);
}
public byte GetByte()
{
byte res = _data[_position];
_position++;
return res;
}
public sbyte GetSByte()
{
return (sbyte)GetByte();
}
public T[] GetArray<T>(ushort size)
{
ushort length = BitConverter.ToUInt16(_data, _position);
_position += 2;
T[] result = new T[length];
length *= size;
Buffer.BlockCopy(_data, _position, result, 0, length);
_position += length;
return result;
}
public bool[] GetBoolArray()
{
return GetArray<bool>(1);
}
public ushort[] GetUShortArray()
{
return GetArray<ushort>(2);
}
public short[] GetShortArray()
{
return GetArray<short>(2);
}
public int[] GetIntArray()
{
return GetArray<int>(4);
}
public uint[] GetUIntArray()
{
return GetArray<uint>(4);
}
public float[] GetFloatArray()
{
return GetArray<float>(4);
}
public double[] GetDoubleArray()
{
return GetArray<double>(8);
}
public long[] GetLongArray()
{
return GetArray<long>(8);
}
public ulong[] GetULongArray()
{
return GetArray<ulong>(8);
}
public string[] GetStringArray()
{
ushort length = GetUShort();
string[] arr = new string[length];
for (int i = 0; i < length; i++)
{
arr[i] = GetString();
}
return arr;
}
/// <summary>
/// Note that "maxStringLength" only limits the number of characters in a string, not its size in bytes.
/// Strings that exceed this parameter are returned as empty
/// </summary>
public string[] GetStringArray(int maxStringLength)
{
ushort length = GetUShort();
string[] arr = new string[length];
for (int i = 0; i < length; i++)
{
arr[i] = GetString(maxStringLength);
}
return arr;
}
public bool GetBool()
{
return GetByte() == 1;
}
public char GetChar()
{
return (char)GetUShort();
}
public ushort GetUShort()
{
ushort result = BitConverter.ToUInt16(_data, _position);
_position += 2;
return result;
}
public short GetShort()
{
short result = BitConverter.ToInt16(_data, _position);
_position += 2;
return result;
}
public long GetLong()
{
long result = BitConverter.ToInt64(_data, _position);
_position += 8;
return result;
}
public ulong GetULong()
{
ulong result = BitConverter.ToUInt64(_data, _position);
_position += 8;
return result;
}
public int GetInt()
{
int result = BitConverter.ToInt32(_data, _position);
_position += 4;
return result;
}
public uint GetUInt()
{
uint result = BitConverter.ToUInt32(_data, _position);
_position += 4;
return result;
}
public float GetFloat()
{
float result = BitConverter.ToSingle(_data, _position);
_position += 4;
return result;
}
public double GetDouble()
{
double result = BitConverter.ToDouble(_data, _position);
_position += 8;
return result;
}
/// <summary>
/// Note that "maxLength" only limits the number of characters in a string, not its size in bytes.
/// </summary>
/// <returns>"string.Empty" if value > "maxLength"</returns>
public string GetString(int maxLength)
{
ushort size = GetUShort();
if (size == 0)
{
return string.Empty;
}
int actualSize = size - 1;
if (actualSize >= NetDataWriter.StringBufferMaxLength)
{
return null;
}
ArraySegment<byte> data = GetBytesSegment(actualSize);
return (maxLength > 0 && NetDataWriter.uTF8Encoding.Value.GetCharCount(data.Array, data.Offset, data.Count) > maxLength) ?
string.Empty :
NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count);
}
public string GetString()
{
ushort size = GetUShort();
if (size == 0)
{
return string.Empty;
}
int actualSize = size - 1;
if (actualSize >= NetDataWriter.StringBufferMaxLength)
{
return null;
}
ArraySegment<byte> data = GetBytesSegment(actualSize);
return NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count);
}
public ArraySegment<byte> GetBytesSegment(int count)
{
ArraySegment<byte> segment = new ArraySegment<byte>(_data, _position, count);
_position += count;
return segment;
}
public ArraySegment<byte> GetRemainingBytesSegment()
{
ArraySegment<byte> segment = new ArraySegment<byte>(_data, _position, AvailableBytes);
_position = _data.Length;
return segment;
}
public T Get<T>() where T : struct, INetSerializable
{
var obj = default(T);
obj.Deserialize(this);
return obj;
}
public T Get<T>(Func<T> constructor) where T : class, INetSerializable
{
var obj = constructor();
obj.Deserialize(this);
return obj;
}
#if LITENETLIB_SPANS || NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1 || NETCOREAPP3_1 || NET5_0 || NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<byte> GetRemainingBytesSpan()
{
return new ReadOnlySpan<byte>(_data, _position, _dataSize - _position);
}
#endif
public byte[] GetRemainingBytes()
{
byte[] outgoingData = new byte[AvailableBytes];
Buffer.BlockCopy(_data, _position, outgoingData, 0, AvailableBytes);
_position = _data.Length;
return outgoingData;
}
public void GetBytes(byte[] destination, int start, int count)
{
Buffer.BlockCopy(_data, _position, destination, start, count);
_position += count;
}
public void GetBytes(byte[] destination, int count)
{
Buffer.BlockCopy(_data, _position, destination, 0, count);
_position += count;
}
public sbyte[] GetSBytesWithLength()
{
return GetArray<sbyte>(1);
}
public byte[] GetBytesWithLength()
{
return GetArray<byte>(1);
}
#endregion
#region PeekMethods
public byte PeekByte()
{
return _data[_position];
}
public sbyte PeekSByte()
{
return (sbyte)_data[_position];
}
public bool PeekBool()
{
return _data[_position] == 1;
}
public char PeekChar()
{
return (char)PeekUShort();
}
public ushort PeekUShort()
{
return BitConverter.ToUInt16(_data, _position);
}
public short PeekShort()
{
return BitConverter.ToInt16(_data, _position);
}
public long PeekLong()
{
return BitConverter.ToInt64(_data, _position);
}
public ulong PeekULong()
{
return BitConverter.ToUInt64(_data, _position);
}
public int PeekInt()
{
return BitConverter.ToInt32(_data, _position);
}
public uint PeekUInt()
{
return BitConverter.ToUInt32(_data, _position);
}
public float PeekFloat()
{
return BitConverter.ToSingle(_data, _position);
}
public double PeekDouble()
{
return BitConverter.ToDouble(_data, _position);
}
/// <summary>
/// Note that "maxLength" only limits the number of characters in a string, not its size in bytes.
/// </summary>
public string PeekString(int maxLength)
{
ushort size = PeekUShort();
if (size == 0)
{
return string.Empty;
}
int actualSize = size - 1;
if (actualSize >= NetDataWriter.StringBufferMaxLength)
{
return null;
}
return (maxLength > 0 && NetDataWriter.uTF8Encoding.Value.GetCharCount(_data, _position + 2, actualSize) > maxLength) ?
string.Empty :
NetDataWriter.uTF8Encoding.Value.GetString(_data, _position + 2, actualSize);
}
public string PeekString()
{
ushort size = PeekUShort();
if (size == 0)
{
return string.Empty;
}
int actualSize = size - 1;
if (actualSize >= NetDataWriter.StringBufferMaxLength)
{
return null;
}
return NetDataWriter.uTF8Encoding.Value.GetString(_data, _position + 2, actualSize);
}
#endregion
#region TryGetMethods
public bool TryGetByte(out byte result)
{
if (AvailableBytes >= 1)
{
result = GetByte();
return true;
}
result = 0;
return false;
}
public bool TryGetSByte(out sbyte result)
{
if (AvailableBytes >= 1)
{
result = GetSByte();
return true;
}
result = 0;
return false;
}
public bool TryGetBool(out bool result)
{
if (AvailableBytes >= 1)
{
result = GetBool();
return true;
}
result = false;
return false;
}
public bool TryGetChar(out char result)
{
if (!TryGetUShort(out ushort uShortValue))
{
result = '\0';
return false;
}
result = (char)uShortValue;
return true;
}
public bool TryGetShort(out short result)
{
if (AvailableBytes >= 2)
{
result = GetShort();
return true;
}
result = 0;
return false;
}
public bool TryGetUShort(out ushort result)
{
if (AvailableBytes >= 2)
{
result = GetUShort();
return true;
}
result = 0;
return false;
}
public bool TryGetInt(out int result)
{
if (AvailableBytes >= 4)
{
result = GetInt();
return true;
}
result = 0;
return false;
}
public bool TryGetUInt(out uint result)
{
if (AvailableBytes >= 4)
{
result = GetUInt();
return true;
}
result = 0;
return false;
}
public bool TryGetLong(out long result)
{
if (AvailableBytes >= 8)
{
result = GetLong();
return true;
}
result = 0;
return false;
}
public bool TryGetULong(out ulong result)
{
if (AvailableBytes >= 8)
{
result = GetULong();
return true;
}
result = 0;
return false;
}
public bool TryGetFloat(out float result)
{
if (AvailableBytes >= 4)
{
result = GetFloat();
return true;
}
result = 0;
return false;
}
public bool TryGetDouble(out double result)
{
if (AvailableBytes >= 8)
{
result = GetDouble();
return true;
}
result = 0;
return false;
}
public bool TryGetString(out string result)
{
if (AvailableBytes >= 2)
{
ushort strSize = PeekUShort();
if (AvailableBytes >= strSize + 1)
{
result = GetString();
return true;
}
}
result = null;
return false;
}
public bool TryGetStringArray(out string[] result)
{
if (!TryGetUShort(out ushort strArrayLength)) {
result = null;
return false;
}
result = new string[strArrayLength];
for (int i = 0; i < strArrayLength; i++)
{
if (!TryGetString(out result[i]))
{
result = null;
return false;
}
}
return true;
}
public bool TryGetBytesWithLength(out byte[] result)
{
if (AvailableBytes >= 2)
{
ushort length = PeekUShort();
if (length >= 0 && AvailableBytes >= 2 + length)
{
result = GetBytesWithLength();
return true;
}
}
result = null;
return false;
}
#endregion
public void Clear()
{
_position = 0;
_dataSize = 0;
_data = null;
}
}
}