forked from fbsamples/oculus-networked-physics-sample
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.cs
1244 lines (1055 loc) · 36.1 KB
/
Network.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
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the Scripts directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
using UnityEngine;
using UnityEngine.Assertions;
using System.Collections.Generic;
namespace Network
{
public static class Constants
{
public const int MaxStringLength = 255;
public const int STREAM_ERROR_NONE = 0;
public const int STREAM_ERROR_OVERFLOW = 1;
public const int STREAM_ERROR_ALIGNMENT = 2;
public const int STREAM_ERROR_VALUE_OUT_OF_RANGE = 3;
};
public static class Util
{
public static uint SignedToUnsigned( int n )
{
return (uint) ( ( n << 1 ) ^ ( n >> 31 ) );
}
public static int UnsignedToSigned( uint n )
{
return (int) ( ( n >> 1 ) ^ ( -( n & 1 ) ) );
}
public static void GenerateAckBits<T>( SequenceBuffer<T> sequenceBuffer, out ushort ack, out uint ack_bits )
{
ack = (ushort) ( sequenceBuffer.GetSequence() - 1 );
ack_bits = 0;
uint mask = 1;
for ( int i = 0; i < 32; ++i )
{
ushort sequence = (ushort) ( ack - i );
if ( sequenceBuffer.Exists( sequence ) )
ack_bits |= mask;
mask <<= 1;
}
}
public static bool SequenceGreaterThan( ushort s1, ushort s2 )
{
return ( ( s1 > s2 ) && ( s1 - s2 <= 32768 ) ) ||
( ( s1 < s2 ) && ( s2 - s1 > 32768 ) );
}
public static bool SequenceLessThan( ushort s1, ushort s2 )
{
return SequenceGreaterThan( s2, s1 );
}
public static int BaselineDifference( ushort current, ushort baseline )
{
if ( current > baseline )
{
return current - baseline;
}
else
{
return (ushort) ( ( ( (uint) current ) + 65536 ) - baseline );
}
}
public static uint SwapBytes( uint value )
{
return ( ( value & 0x000000FF ) << 24 ) |
( ( value & 0x0000FF00 ) << 8 ) |
( ( value & 0x00FF0000 ) >> 8 ) |
( ( value & 0xFF000000 ) >> 24 );
}
public static uint HostToNetwork( uint value )
{
if ( BitConverter.IsLittleEndian )
return value;
else
return SwapBytes( value );
}
public static uint NetworkToHost( uint value )
{
if ( BitConverter.IsLittleEndian )
return value;
else
return SwapBytes( value );
}
public static int PopCount( uint value )
{
value = value - ( ( value >> 1 ) & 0x55555555 );
value = ( value & 0x33333333 ) + ( ( value >> 2 ) & 0x33333333 );
value = ( ( value + ( value >> 4 ) & 0xF0F0F0F ) * 0x1010101 ) >> 24;
return unchecked( (int) value );
}
public static int Log2( uint x )
{
uint a = x | ( x >> 1 );
uint b = a | ( a >> 2 );
uint c = b | ( b >> 4 );
uint d = c | ( c >> 8 );
uint e = d | ( d >> 16 );
uint f = e >> 1;
return PopCount( f );
}
public static int BitsRequired( int min, int max )
{
return ( min == max ) ? 1 : Log2( (uint) ( max - min ) ) + 1;
}
public static int BitsRequired( uint min, uint max )
{
return ( min == max ) ? 1 : Log2( max - min ) + 1;
}
}
public class BitWriter
{
uint[] m_data;
ulong m_scratch;
int m_numBits;
int m_numWords;
int m_bitsWritten;
int m_wordIndex;
int m_scratchBits;
public void Start( uint[] data )
{
Assert.IsTrue( data != null );
m_data = data;
m_numWords = data.Length / 4;
m_numBits = m_numWords * 32;
m_bitsWritten = 0;
m_wordIndex = 0;
m_scratch = 0;
m_scratchBits = 0;
}
public void WriteBits( uint value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 32 );
Assert.IsTrue( m_bitsWritten + bits <= m_numBits );
value &= (uint) ( ( ( (ulong)1 ) << bits ) - 1 );
m_scratch |= ( (ulong) value ) << m_scratchBits;
m_scratchBits += bits;
if ( m_scratchBits >= 32 )
{
Assert.IsTrue( m_wordIndex < m_numWords );
m_data[m_wordIndex] = Util.HostToNetwork( (uint) ( m_scratch & 0xFFFFFFFF ) );
m_scratch >>= 32;
m_scratchBits -= 32;
m_wordIndex++;
}
m_bitsWritten += bits;
}
public void WriteAlign()
{
int remainderBits = (int) ( m_bitsWritten % 8 );
if ( remainderBits != 0 )
{
uint zero = 0;
WriteBits( zero, 8 - remainderBits );
Assert.IsTrue( ( m_bitsWritten % 8 ) == 0 );
}
}
public void WriteBytes( byte[] data, int bytes )
{
Assert.IsTrue( GetAlignBits() == 0 );
for ( int i = 0; i < bytes; ++i )
WriteBits( data[i], 8 );
}
public void Finish()
{
if ( m_scratchBits != 0 )
{
Assert.IsTrue( m_wordIndex < m_numWords );
m_data[m_wordIndex] = Util.HostToNetwork( (uint) ( m_scratch & 0xFFFFFFFF ) );
m_scratch >>= 32;
m_scratchBits -= 32;
m_wordIndex++;
}
}
public int GetAlignBits()
{
return ( 8 - ( m_bitsWritten % 8 ) ) % 8;
}
public int GetBitsWritten()
{
return m_bitsWritten;
}
public int GetBitsAvailable()
{
return m_numBits - m_bitsWritten;
}
public byte[] GetData()
{
int bytesWritten = GetBytesWritten();
byte[] output = new byte[bytesWritten];
Buffer.BlockCopy( m_data, 0, output, 0, bytesWritten );
return output;
}
public int GetBytesWritten()
{
return ( m_bitsWritten + 7 ) / 8;
}
public int GetTotalBytes()
{
return m_numWords * 4;
}
}
public class BitReader
{
uint[] m_data;
ulong m_scratch;
int m_numBits;
int m_numWords;
int m_bitsRead;
int m_scratchBits;
int m_wordIndex;
public void Start( byte[] data )
{
int bytes = data.Length;
m_numWords = ( bytes + 3 ) / 4;
m_numBits = bytes * 8;
m_bitsRead = 0;
m_scratch = 0;
m_scratchBits = 0;
m_wordIndex = 0;
m_data = new uint[m_numWords];
Buffer.BlockCopy( data, 0, m_data, 0, bytes );
}
public bool WouldOverflow( int bits )
{
return m_bitsRead + bits > m_numBits;
}
public uint ReadBits( int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 32 );
Assert.IsTrue( m_bitsRead + bits <= m_numBits );
m_bitsRead += bits;
Assert.IsTrue( m_scratchBits >= 0 && m_scratchBits <= 64 );
if ( m_scratchBits < bits )
{
Assert.IsTrue( m_wordIndex < m_numWords );
m_scratch |= ( (ulong) ( Util.NetworkToHost( m_data[m_wordIndex] ) ) ) << m_scratchBits;
m_scratchBits += 32;
m_wordIndex++;
}
Assert.IsTrue( m_scratchBits >= bits );
uint output = (uint) ( m_scratch & ( (((ulong)1)<<bits) - 1 ) );
m_scratch >>= bits;
m_scratchBits -= bits;
return output;
}
public bool ReadAlign()
{
int remainderBits = m_bitsRead % 8;
if ( remainderBits != 0 )
{
uint value = ReadBits( 8 - remainderBits );
Assert.IsTrue( m_bitsRead % 8 == 0 );
if ( value != 0 )
return false;
}
return true;
}
public void ReadBytes( byte[] data, int bytes )
{
Assert.IsTrue( GetAlignBits() == 0 );
for ( int i = 0; i < bytes; ++i )
data[i] = (byte) ReadBits( 8 );
}
public void Finish()
{
// ...
}
public int GetAlignBits()
{
return ( 8 - m_bitsRead % 8 ) % 8;
}
public int GetBitsRead()
{
return m_bitsRead;
}
public int GetBytesRead()
{
return m_wordIndex * 4;
}
public int GetBitsRemaining()
{
return m_numBits - m_bitsRead;
}
public int GetBytesRemaining()
{
return GetBitsRemaining() / 8;
}
}
public class WriteStream
{
BitWriter m_writer = new BitWriter();
int m_error = Constants.STREAM_ERROR_NONE;
public void Start( uint[] buffer )
{
m_writer.Start( buffer );
}
public void SerializeSignedInteger( int value, int min, int max )
{
Assert.IsTrue( min < max );
Assert.IsTrue( value >= min );
Assert.IsTrue( value <= max );
int bits = Util.BitsRequired( min, max );
uint unsigned_value = (uint) ( value - min );
m_writer.WriteBits( unsigned_value, bits );
}
public void SerializeUnsignedInteger( uint value, uint min, uint max )
{
Assert.IsTrue( min < max );
Assert.IsTrue( value >= min );
Assert.IsTrue( value <= max );
int bits = Util.BitsRequired( min, max );
uint unsigned_value = value - min;
m_writer.WriteBits( unsigned_value, bits );
}
public void SerializeBits( byte value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 8 );
Assert.IsTrue( bits == 8 || ( value < ( 1 << bits ) ) );
m_writer.WriteBits( value, bits );
}
public void SerializeBits( ushort value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 16 );
Assert.IsTrue( bits == 16 || ( value < ( 1 << bits ) ) );
m_writer.WriteBits( value, bits );
}
public void SerializeBits( uint value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 32 );
Assert.IsTrue( bits == 32 || ( value < ( 1 << bits ) ) );
m_writer.WriteBits( value, bits );
}
public void SerializeBits( ulong value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 64 );
Assert.IsTrue( bits == 64 || ( value < ( 1UL << bits ) ) );
uint loword = (uint) value;
uint hiword = (uint) ( value >> 32 );
if ( bits <= 32 )
{
m_writer.WriteBits( loword, bits );
}
else
{
m_writer.WriteBits( loword, 32 );
m_writer.WriteBits( hiword, bits - 32 );
}
}
public void SerializeBytes( byte[] data, int bytes )
{
Assert.IsTrue( data != null );
Assert.IsTrue( bytes >= 0 );
SerializeAlign();
m_writer.WriteBytes( data, bytes );
}
public void SerializeString( string s )
{
SerializeAlign();
int stringLength = (int) s.Length;
Assert.IsTrue( stringLength <= Network.Constants.MaxStringLength );
m_writer.WriteBits( (byte) stringLength, Util.BitsRequired( 0, Network.Constants.MaxStringLength ) );
for ( int i = 0; i < stringLength; ++i )
{
char charValue = s[i];
m_writer.WriteBits( charValue, 16 );
}
}
public void SerializeFloat( float f )
{
byte[] byteArray = BitConverter.GetBytes( f );
for ( int i = 0; i < 4; ++i )
m_writer.WriteBits( byteArray[i], 8 );
}
public void SerializeAlign()
{
m_writer.WriteAlign();
}
public void Finish()
{
m_writer.Finish();
}
public int GetAlignBits()
{
return m_writer.GetAlignBits();
}
public byte[] GetData()
{
return m_writer.GetData();
}
public int GetBytesProcessed()
{
return m_writer.GetBytesWritten();
}
public int GetBitsProcessed()
{
return m_writer.GetBitsWritten();
}
public int GetError()
{
return m_error;
}
}
public class ReadStream
{
BitReader m_reader = new BitReader();
int m_bitsRead = 0;
int m_error = Constants.STREAM_ERROR_NONE;
byte[] m_floatBytes = new byte[4];
public void Start( byte[] data )
{
m_reader.Start( data );
}
public bool SerializeSignedInteger( out int value, int min, int max )
{
Assert.IsTrue( min < max );
int bits = Util.BitsRequired( min, max );
if ( m_reader.WouldOverflow( bits ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
value = 0;
return false;
}
uint unsigned_value = m_reader.ReadBits( bits );
value = (int) ( unsigned_value + min );
m_bitsRead += bits;
return true;
}
public bool SerializeUnsignedInteger( out uint value, uint min, uint max )
{
Assert.IsTrue( min < max );
int bits = Util.BitsRequired( min, max );
if ( m_reader.WouldOverflow( bits ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
value = 0;
return false;
}
uint unsigned_value = m_reader.ReadBits( bits );
value = unsigned_value + min;
m_bitsRead += bits;
return true;
}
public bool SerializeBits( out byte value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 8 );
if ( m_reader.WouldOverflow( bits ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
value = 0;
return false;
}
byte read_value = (byte) m_reader.ReadBits( bits );
value = read_value;
m_bitsRead += bits;
return true;
}
public bool SerializeBits( out ushort value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 16 );
if ( m_reader.WouldOverflow( bits ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
value = 0;
return false;
}
ushort read_value = (ushort) m_reader.ReadBits( bits );
value = read_value;
m_bitsRead += bits;
return true;
}
public bool SerializeBits( out uint value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 32 );
if ( m_reader.WouldOverflow( bits ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
value = 0;
return false;
}
uint read_value = m_reader.ReadBits( bits );
value = read_value;
m_bitsRead += bits;
return true;
}
public bool SerializeBits( out ulong value, int bits )
{
Assert.IsTrue( bits > 0 );
Assert.IsTrue( bits <= 64 );
if ( m_reader.WouldOverflow( bits ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
value = 0;
return false;
}
if ( bits <= 32 )
{
uint loword = m_reader.ReadBits( bits );
value = (ulong) loword;
}
else
{
uint loword = m_reader.ReadBits( 32 );
uint hiword = m_reader.ReadBits( bits - 32 );
value = ( (ulong) loword ) | ( ( (ulong) hiword ) << 32 );
}
return true;
}
public bool SerializeBytes( byte[] data, int bytes )
{
if ( !SerializeAlign() )
return false;
if ( m_reader.WouldOverflow( bytes * 8 ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
return false;
}
m_reader.ReadBytes( data, bytes );
m_bitsRead += bytes * 8;
return true;
}
public bool SerializeString( out string s )
{
if ( !SerializeAlign() )
{
s = null;
return false;
}
int stringLength;
if ( !SerializeSignedInteger( out stringLength, 0, Network.Constants.MaxStringLength ) )
{
s = null;
return false;
}
if ( m_reader.WouldOverflow( (int) ( stringLength * 16 ) ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
s = null;
return false;
}
char[] stringData = new char[Network.Constants.MaxStringLength];
for ( int i = 0; i < stringLength; ++i )
{
stringData[i] = (char) m_reader.ReadBits( 16 );
}
s = new string( stringData, 0, stringLength );
return true;
}
public bool SerializeFloat( out float f )
{
if ( m_reader.WouldOverflow( 32 ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
f = 0.0f;
return false;
}
for ( int i = 0; i < 4; ++i )
m_floatBytes[i] = (byte) m_reader.ReadBits( 8 );
f = BitConverter.ToSingle( m_floatBytes, 0 );
return true;
}
public bool SerializeAlign()
{
int alignBits = m_reader.GetAlignBits();
if ( m_reader.WouldOverflow( alignBits ) )
{
m_error = Constants.STREAM_ERROR_OVERFLOW;
return false;
}
if ( !m_reader.ReadAlign() )
{
m_error = Constants.STREAM_ERROR_ALIGNMENT;
return false;
}
m_bitsRead += alignBits;
return true;
}
public void Finish()
{
m_reader.Finish();
}
public int GetAlignBits()
{
return m_reader.GetAlignBits();
}
public int GetBitsProcessed()
{
return m_bitsRead;
}
public int GetBytesProcessed()
{
return ( m_bitsRead + 7 ) / 8;
}
public int GetError()
{
return m_error;
}
}
public class SerializeException : Exception
{
public SerializeException() { }
};
public class Serializer
{
// write
public void write_bool( WriteStream stream, bool value )
{
uint unsigned_value = ( value == true ) ? 1U : 0U;
stream.SerializeBits( unsigned_value, 1 );
}
public void write_int( WriteStream stream, int value, int min, int max )
{
stream.SerializeSignedInteger( value, min, max );
}
public void write_uint( WriteStream stream, uint value, uint min, uint max )
{
stream.SerializeUnsignedInteger( value, min, max );
}
public void write_bits( WriteStream stream, byte value, int bits )
{
stream.SerializeBits( value, bits );
}
public void write_bits( WriteStream stream, ushort value, int bits )
{
stream.SerializeBits( value, bits );
}
public void write_bits( WriteStream stream, uint value, int bits )
{
stream.SerializeBits( value, bits );
}
public void write_bits( WriteStream stream, ulong value, int bits )
{
stream.SerializeBits( value, bits );
}
public void write_string( WriteStream stream, string value )
{
stream.SerializeString( value );
}
public void write_float( WriteStream stream, float value )
{
stream.SerializeFloat( value );
}
// read
public void read_bool( ReadStream stream, out bool value )
{
uint unsigned_value;
if ( !stream.SerializeBits( out unsigned_value, 1 ) )
throw new SerializeException();
value = ( unsigned_value == 1 ) ? true : false;
}
public void read_int( ReadStream stream, out int value, int min, int max )
{
if ( !stream.SerializeSignedInteger( out value, min, max ) )
throw new SerializeException();
}
public void read_uint( ReadStream stream, out uint value, uint min, uint max )
{
if ( !stream.SerializeUnsignedInteger( out value, min, max ) )
throw new SerializeException();
}
public void read_bits( ReadStream stream, out byte value, int bits )
{
if ( !stream.SerializeBits( out value, bits ) )
throw new SerializeException();
}
public void read_bits( ReadStream stream, out ushort value, int bits )
{
if ( !stream.SerializeBits( out value, bits ) )
throw new SerializeException();
}
public void read_bits( ReadStream stream, out uint value, int bits )
{
if ( !stream.SerializeBits( out value, bits ) )
throw new SerializeException();
}
public void read_bits( ReadStream stream, out ulong value, int bits )
{
if ( !stream.SerializeBits( out value, bits ) )
throw new SerializeException();
}
public void read_string( ReadStream stream, out string value )
{
if ( !stream.SerializeString( out value ) )
throw new SerializeException();
}
public void read_float( ReadStream stream, out float value )
{
if ( !stream.SerializeFloat( out value ) )
throw new SerializeException();
}
}
public class SequenceBuffer<T>
{
public T[] m_entries;
uint[] m_entry_sequence;
int m_size;
ushort m_sequence;
public T[] Entries
{
get
{
return m_entries;
}
}
public SequenceBuffer( int size )
{
Assert.IsTrue( size > 0 );
m_size = size;
m_sequence = 0;
m_entry_sequence = new uint[size];
m_entries = new T[size];
Reset();
}
public void Reset()
{
m_sequence = 0;
for ( int i = 0; i < m_size; ++i )
{
m_entry_sequence[i] = 0xFFFFFFFF;
}
}
public int Insert( ushort sequence )
{
if ( Util.SequenceGreaterThan( (ushort) ( sequence + 1 ), m_sequence ) )
{
RemoveEntries( m_sequence, sequence );
m_sequence = (ushort) ( sequence + 1 );
}
else if ( Util.SequenceLessThan( sequence, (ushort) ( m_sequence - m_size ) ) )
{
return -1;
}
int index = sequence % m_size;
m_entry_sequence[index] = sequence;
return index;
}
public void Remove( ushort sequence )
{
m_entry_sequence[ sequence % m_size ] = 0xFFFFFFFF;
}
public bool Available( ushort sequence )
{
return m_entry_sequence[ sequence % m_size ] == 0xFFFFFFFF;
}
public bool Exists( ushort sequence )
{
return m_entry_sequence[ sequence % m_size ] == (uint) sequence;
}
public int Find( ushort sequence )
{
int index = sequence % m_size;
if ( m_entry_sequence[index] == (uint)sequence )
return index;
else
return -1;
}
public ushort GetSequence()
{
return m_sequence;
}
public int GetSize()
{
return m_size;
}
public void RemoveEntries( ushort start_sequence, ushort finish_sequence )
{
int start_sequence_int = (int) start_sequence;
int finish_sequence_int = (int) finish_sequence;
if ( finish_sequence_int < start_sequence_int )
finish_sequence_int += 65535;
for ( int sequence = start_sequence_int; sequence <= finish_sequence_int; ++sequence )
m_entry_sequence[sequence % m_size] = 0xFFFFFFFF;
}
}
public class SequenceBuffer32<T> where T : new()
{
public T[] m_entries;
uint[] m_entry_sequence;
int m_size;
uint m_sequence;
public T[] Entries
{
get
{
return m_entries;
}
}
public SequenceBuffer32( int size )
{
Assert.IsTrue( size > 0 );
m_size = size;
m_sequence = 0;
m_entry_sequence = new uint[size];
m_entries = new T[size];
for ( int i = 0; i < size; ++i )
m_entries[i] = new T();
Reset();
}
public void Reset()
{
m_sequence = 0;
for ( int i = 0; i < m_size; ++i )
{
m_entry_sequence[i] = 0xFFFFFFFF;
}
}
public int Insert( uint sequence )
{
Assert.IsTrue( sequence != 0xFFFFFFFF );
if ( sequence + 1 > m_sequence )
{
RemoveEntries( m_sequence, sequence );
m_sequence = sequence + 1;
}
else if ( sequence < m_sequence - m_size )
{
return -1;
}
int index = (int) ( sequence % m_size );
m_entry_sequence[index] = sequence;
return index;
}
public void Remove( uint sequence )
{
Assert.IsTrue( sequence != 0xFFFFFFFF );
m_entry_sequence[sequence % m_size] = 0xFFFFFFFF;
}
public bool Available( uint sequence )
{
Assert.IsTrue( sequence != 0xFFFFFFFF );
return m_entry_sequence[sequence % m_size] == 0xFFFFFFFF;
}
public bool Exists( uint sequence )
{
Assert.IsTrue( sequence != 0xFFFFFFFF );
return m_entry_sequence[sequence % m_size] == sequence;
}