forked from mas-bandwidth/yojimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyojimbo_utility.h
1010 lines (744 loc) · 31 KB
/
yojimbo_utility.h
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
/*
Yojimbo Network Library.
Copyright © 2016 - 2017, The Network Protocol Company, Inc.
*/
#ifndef YOJIMBO_UTILITY_H
#define YOJIMBO_UTILITY_H
#include "yojimbo_config.h"
#include "yojimbo_platform.h"
#include "yojimbo_allocator.h"
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <memory.h>
/** @file */
namespace yojimbo
{
/**
Generate random bytes.
This is a cryptographically secure random number.
*/
void random_bytes( uint8_t * data, int bytes );
/**
Generate a random integer between a and b (inclusive).
IMPORTANT: This is not a cryptographically secure random. It's used only for test functions and in the network simulator.
@param a The minimum integer value to generate.
@param b The maximum integer value to generate.
@returns A pseudo random integer value in [a,b].
*/
inline int random_int( int a, int b )
{
yojimbo_assert( a < b );
int result = a + rand() % ( b - a + 1 );
yojimbo_assert( result >= a );
yojimbo_assert( result <= b );
return result;
}
/**
Generate a random float between a and b.
IMPORTANT: This is not a cryptographically secure random. It's used only for test functions and in the network simulator.
@param a The minimum integer value to generate.
@param b The maximum integer value to generate.
@returns A pseudo random float value in [a,b].
*/
inline float random_float( float a, float b )
{
yojimbo_assert( a < b );
float random = ( (float) rand() ) / (float) RAND_MAX;
float diff = b - a;
float r = random * diff;
return a + r;
}
/**
Calculates the population count of an unsigned 32 bit integer at compile time.
Population count is the number of bits in the integer that set to 1.
See "Hacker's Delight" and http://www.hackersdelight.org/hdcodetxt/popArrayHS.c.txt
@see yojimbo::Log2
@see yojimbo::BitsRequired
*/
template <uint32_t x> struct PopCount
{
enum { a = x - ( ( x >> 1 ) & 0x55555555 ),
b = ( ( ( a >> 2 ) & 0x33333333 ) + ( a & 0x33333333 ) ),
c = ( ( ( b >> 4 ) + b ) & 0x0f0f0f0f ),
d = c + ( c >> 8 ),
e = d + ( d >> 16 ),
result = e & 0x0000003f
};
};
/**
Calculates the log 2 of an unsigned 32 bit integer at compile time.
@see yojimbo::Log2
@see yojimbo::BitsRequired
*/
template <uint32_t x> struct Log2
{
enum { a = x | ( x >> 1 ),
b = a | ( a >> 2 ),
c = b | ( b >> 4 ),
d = c | ( c >> 8 ),
e = d | ( d >> 16 ),
f = e >> 1,
result = PopCount<f>::result
};
};
/**
Calculates the number of bits required to serialize an integer value in [min,max] at compile time.
@see Log2
@see PopCount
*/
template <int64_t min, int64_t max> struct BitsRequired
{
static const uint32_t result = ( min == max ) ? 0 : ( Log2<uint32_t(max-min)>::result + 1 );
};
/**
Calculates the population count of an unsigned 32 bit integer.
The population count is the number of bits in the integer set to 1.
@param x The input integer value.
@returns The number of bits set to 1 in the input value.
*/
inline uint32_t popcount( uint32_t x )
{
#ifdef __GNUC__
return __builtin_popcount( x );
#else // #ifdef __GNUC__
const uint32_t a = x - ( ( x >> 1 ) & 0x55555555 );
const uint32_t b = ( ( ( a >> 2 ) & 0x33333333 ) + ( a & 0x33333333 ) );
const uint32_t c = ( ( ( b >> 4 ) + b ) & 0x0f0f0f0f );
const uint32_t d = c + ( c >> 8 );
const uint32_t e = d + ( d >> 16 );
const uint32_t result = e & 0x0000003f;
return result;
#endif // #ifdef __GNUC__
}
/**
Calculates the log base 2 of an unsigned 32 bit integer.
@param x The input integer value.
@returns The log base 2 of the input.
*/
inline uint32_t log2( uint32_t x )
{
const uint32_t a = x | ( x >> 1 );
const uint32_t b = a | ( a >> 2 );
const uint32_t c = b | ( b >> 4 );
const uint32_t d = c | ( c >> 8 );
const uint32_t e = d | ( d >> 16 );
const uint32_t f = e >> 1;
return popcount( f );
}
/**
Calculates the number of bits required to serialize an integer in range [min,max].
@param min The minimum value.
@param max The maximum value.
@returns The number of bits required to serialize the integer.
*/
inline int bits_required( uint32_t min, uint32_t max )
{
#ifdef __GNUC__
return 32 - __builtin_clz( max - min );
#else // #ifdef __GNUC__
return ( min == max ) ? 0 : log2( max - min ) + 1;
#endif // #ifdef __GNUC__
}
/**
Reverse the order of bytes in a 64 bit integer.
@param value The input value.
@returns The input value with the byte order reversed.
*/
inline uint64_t bswap( uint64_t value )
{
#ifdef __GNUC__
return __builtin_bswap64( value );
#else // #ifdef __GNUC__
value = ( value & 0x00000000FFFFFFFF ) << 32 | ( value & 0xFFFFFFFF00000000 ) >> 32;
value = ( value & 0x0000FFFF0000FFFF ) << 16 | ( value & 0xFFFF0000FFFF0000 ) >> 16;
value = ( value & 0x00FF00FF00FF00FF ) << 8 | ( value & 0xFF00FF00FF00FF00 ) >> 8;
return value;
#endif // #ifdef __GNUC__
}
/**
Reverse the order of bytes in a 32 bit integer.
@param value The input value.
@returns The input value with the byte order reversed.
*/
inline uint32_t bswap( uint32_t value )
{
#ifdef __GNUC__
return __builtin_bswap32( value );
#else // #ifdef __GNUC__
return ( value & 0x000000ff ) << 24 | ( value & 0x0000ff00 ) << 8 | ( value & 0x00ff0000 ) >> 8 | ( value & 0xff000000 ) >> 24;
#endif // #ifdef __GNUC__
}
/**
Reverse the order of bytes in a 16 bit integer.
@param value The input value.
@returns The input value with the byte order reversed.
*/
inline uint16_t bswap( uint16_t value )
{
return ( value & 0x00ff ) << 8 | ( value & 0xff00 ) >> 8;
}
/**
Template to convert an integer value from local byte order to network byte order.
IMPORTANT: Yojimbo defines network byte order to be little endian, because most machines running yojimbo are little endian, this creates the least amount of work.
@param value The input value in local byte order. Supported integer types: uint64_t, uint32_t, uint16_t.
@returns The input value converted to network byte order. If this processor is little endian the output is the same as the input. If the processor is big endian, the output is the input byte swapped.
@see yojimbo::bswap
*/
template <typename T> T host_to_network( T value )
{
#if YOJIMBO_BIG_ENDIAN
return bswap( value );
#else // #if YOJIMBO_BIG_ENDIAN
return value;
#endif // #if YOJIMBO_BIG_ENDIAN
}
/**
Template to convert an integer value from network byte order to local byte order.
IMPORTANT: Yojimbo defines network byte order to be little endian, because most machines running yojimbo are little endian, this creates the least amount of work.
@param value The input value in network byte order. Supported integer types: uint64_t, uint32_t, uint16_t.
@returns The input value converted to local byte order. If this processor is little endian the output is the same as the input. If the processor is big endian, the output is the input byte swapped.
@see yojimbo::bswap
*/
template <typename T> T network_to_host( T value )
{
#if YOJIMBO_BIG_ENDIAN
return bswap( value );
#else // #if YOJIMBO_BIG_ENDIAN
return value;
#endif // #if YOJIMBO_BIG_ENDIAN
}
/**
Compares two 16 bit sequence numbers and returns true if the first one is greater than the second (considering wrapping).
IMPORTANT: This is not the same as s1 > s2!
Greater than is defined specially to handle wrapping sequence numbers.
If the two sequence numbers are close together, it is as normal, but they are far apart, it is assumed that they have wrapped around.
Thus, sequence_greater_than( 1, 0 ) returns true, and so does sequence_greater_than( 0, 65535 )!
@param s1 The first sequence number.
@param s2 The second sequence number.
@returns True if the s1 is greater than s2, with sequence number wrapping considered.
*/
inline bool sequence_greater_than( uint16_t s1, uint16_t s2 )
{
return ( ( s1 > s2 ) && ( s1 - s2 <= 32768 ) ) ||
( ( s1 < s2 ) && ( s2 - s1 > 32768 ) );
}
/**
Compares two 16 bit sequence numbers and returns true if the first one is less than the second (considering wrapping).
IMPORTANT: This is not the same as s1 < s2!
Greater than is defined specially to handle wrapping sequence numbers.
If the two sequence numbers are close together, it is as normal, but they are far apart, it is assumed that they have wrapped around.
Thus, sequence_less_than( 0, 1 ) returns true, and so does sequence_greater_than( 65535, 0 )!
@param s1 The first sequence number.
@param s2 The second sequence number.
@returns True if the s1 is less than s2, with sequence number wrapping considered.
*/
inline bool sequence_less_than( uint16_t s1, uint16_t s2 )
{
return sequence_greater_than( s2, s1 );
}
/**
Convert a signed integer to an unsigned integer with zig-zag encoding.
0,-1,+1,-2,+2... becomes 0,1,2,3,4 ...
@param n The input value
@returns The input value converted from signed to unsigned with zig-zag encoding.
*/
inline int signed_to_unsigned( int n )
{
return ( n << 1 ) ^ ( n >> 31 );
}
/**
Convert an unsigned integer to as signed integer with zig-zag encoding.
0,1,2,3,4... becomes 0,-1,+1,-2,+2...
@param n The input value
@returns The input value converted from unsigned to signed with zig-zag encoding.
*/
inline int unsigned_to_signed( uint32_t n )
{
return ( n >> 1 ) ^ ( -int32_t( n & 1 ) );
}
/**
Implementation of the 64 bit murmur hash.
@param key The input value.
@param length The length of the key (bytes).
@param seed The initial seed for the hash. Used to chain together multiple hash calls.
@returns A 64 bit hash of the input value.
*/
uint64_t murmur_hash_64( const void * key, uint32_t length, uint64_t seed );
/**
Base 64 encode a string.
@param input The input string value. Must be null terminated.
@param output The output base64 encoded string. Will be null terminated.
@param output_size The size of the output buffer (bytes). Must be large enough to store the base 64 encoded string.
@returns The number of bytes in the base64 encoded string, including terminating null. -1 if the base64 encode failed because the output buffer was too small.
*/
int base64_encode_string( const char * input, char * output, int output_size );
/**
Base 64 decode a string.
@param input The base64 encoded string.
@param output The decoded string. Guaranteed to be null terminated, even if the base64 is maliciously encoded.
@param output_size The size of the output buffer (bytes).
@returns The number of bytes in the decoded string, including terminating null. -1 if the base64 decode failed.
*/
int base64_decode_string( const char * input, char * output, int output_size );
/**
Base 64 encode a block of data.
@param input The data to encode.
@param input_length The length of the input data (bytes).
@param output The output base64 encoded string. Will be null terminated.
@param output_size The size of the output buffer. Must be large enough to store the base 64 encoded string.
@returns The number of bytes in the base64 encoded string, including terminating null. -1 if the base64 encode failed because the output buffer was too small.
*/
int base64_encode_data( const uint8_t * input, int input_length, char * output, int output_size );
/**
Base 64 decode a block of data.
@param input The base 64 data to decode. Must be a null terminated string.
@param output The output data. Will *not* be null terminated.
@param output_size The size of the output buffer.
@returns The number of bytes of decoded data. -1 if the base64 decode failed.
*/
int base64_decode_data( const char * input, uint8_t * output, int output_size );
/**
Print bytes with a label.
Useful for printing out packets, encryption keys, nonce etc.
@param label The label to print out before the bytes.
@param data The data to print out to stdout.
@param data_bytes The number of bytes of data to print.
*/
void print_bytes( const char * label, const uint8_t * data, int data_bytes );
/**
A simple bit array class.
You can create a bit array with a number of bits, set, clear and test if each bit is set.
*/
class BitArray
{
public:
/**
The bit array constructor.
@param allocator The allocator to use.
@param size The number of bits in the bit array.
All bits are initially set to zero.
*/
BitArray( Allocator & allocator, int size )
{
yojimbo_assert( size > 0 );
m_allocator = &allocator;
m_size = size;
m_bytes = 8 * ( ( size / 64 ) + ( ( size % 64 ) ? 1 : 0 ) );
yojimbo_assert( m_bytes > 0 );
m_data = (uint64_t*) YOJIMBO_ALLOCATE( allocator, m_bytes );
Clear();
}
/**
The bit array destructor.
*/
~BitArray()
{
yojimbo_assert( m_data );
yojimbo_assert( m_allocator );
YOJIMBO_FREE( *m_allocator, m_data );
m_allocator = NULL;
}
/**
Clear all bit values to zero.
*/
void Clear()
{
yojimbo_assert( m_data );
memset( m_data, 0, m_bytes );
}
/**
Set a bit to 1.
@param index The index of the bit.
*/
void SetBit( int index )
{
yojimbo_assert( index >= 0 );
yojimbo_assert( index < m_size );
const int data_index = index >> 6;
const int bit_index = index & ( (1<<6) - 1 );
yojimbo_assert( bit_index >= 0 );
yojimbo_assert( bit_index < 64 );
m_data[data_index] |= uint64_t(1) << bit_index;
}
/**
Clear a bit to 0.
@param index The index of the bit.
*/
void ClearBit( int index )
{
yojimbo_assert( index >= 0 );
yojimbo_assert( index < m_size );
const int data_index = index >> 6;
const int bit_index = index & ( (1<<6) - 1 );
m_data[data_index] &= ~( uint64_t(1) << bit_index );
}
/**
Get the value of the bit.
Returns 1 if the bit is set, 0 if the bit is not set.
@param index The index of the bit.
*/
uint64_t GetBit( int index ) const
{
yojimbo_assert( index >= 0 );
yojimbo_assert( index < m_size );
const int data_index = index >> 6;
const int bit_index = index & ( (1<<6) - 1 );
yojimbo_assert( bit_index >= 0 );
yojimbo_assert( bit_index < 64 );
return ( m_data[data_index] >> bit_index ) & 1;
}
/**
Gets the size of the bit array, in number of bits.
@returns The number of bits.
*/
int GetSize() const
{
return m_size;
}
private:
Allocator * m_allocator; ///< Allocator passed in to the constructor.
int m_size; ///< The size of the bit array in bits.
int m_bytes; ///< The size of the bit array in bytes.
uint64_t * m_data; ///< The data backing the bit array is an array of 64 bit integer values.
BitArray( const BitArray & other );
BitArray & operator = ( const BitArray & other );
};
/**
A simple templated queue.
This is a FIFO queue. First entry in, first entry out.
*/
template <typename T> class Queue
{
public:
/**
Queue constructor.
@param allocator The allocator to use.
@param size The maximum number of entries in the queue.
*/
Queue( Allocator & allocator, int size )
{
yojimbo_assert( size > 0 );
m_arraySize = size;
m_startIndex = 0;
m_numEntries = 0;
m_allocator = &allocator;
m_entries = (T*) YOJIMBO_ALLOCATE( allocator, sizeof(T) * size );
memset( m_entries, 0, sizeof(T) * size );
}
/**
Queue destructor.
*/
~Queue()
{
yojimbo_assert( m_allocator );
YOJIMBO_FREE( *m_allocator, m_entries );
m_arraySize = 0;
m_startIndex = 0;
m_numEntries = 0;
m_allocator = NULL;
}
/**
Clear all entries in the queue and reset back to default state.
*/
void Clear()
{
m_numEntries = 0;
m_startIndex = 0;
}
/**
Pop a value off the queue.
IMPORTANT: This will assert if the queue is empty. Check Queue::IsEmpty or Queue::GetNumEntries first!
@returns The value popped off the queue.
*/
T Pop()
{
yojimbo_assert( !IsEmpty() );
const T & entry = m_entries[m_startIndex];
m_startIndex = ( m_startIndex + 1 ) % m_arraySize;
m_numEntries--;
return entry;
}
/**
Push a value on to the queue.
@param value The value to push onto the queue.
IMPORTANT: Will assert if the queue is already full. Check Queue::IsFull before calling this!
*/
void Push( const T & value )
{
yojimbo_assert( !IsFull() );
const int index = ( m_startIndex + m_numEntries ) % m_arraySize;
m_entries[index] = value;
m_numEntries++;
}
/**
Random access for entries in the queue.
@param index The index into the queue. 0 is the oldest entry, Queue::GetNumEntries() - 1 is the newest.
@returns The value in the queue at the index.
*/
T & operator [] ( int index )
{
yojimbo_assert( !IsEmpty() );
yojimbo_assert( index >= 0 );
yojimbo_assert( index < m_numEntries );
return m_entries[ ( m_startIndex + index ) % m_arraySize ];
}
/**
Random access for entries in the queue (const version).
@param index The index into the queue. 0 is the oldest entry, Queue::GetNumEntries() - 1 is the newest.
@returns The value in the queue at the index.
*/
const T & operator [] ( int index ) const
{
yojimbo_assert( !IsEmpty() );
yojimbo_assert( index >= 0 );
yojimbo_assert( index < m_numEntries );
return m_entries[ ( m_startIndex + index ) % m_arraySize ];
}
/**
Get the size of the queue.
This is the maximum number of values that can be pushed on the queue.
@returns The size of the queue.
*/
int GetSize() const
{
return m_arraySize;
}
/**
Is the queue currently full?
@returns True if the queue is full. False otherwise.
*/
bool IsFull() const
{
return m_numEntries == m_arraySize;
}
/**
Is the queue currently empty?
@returns True if there are no entries in the queue.
*/
bool IsEmpty() const
{
return m_numEntries == 0;
}
/**
Get the number of entries in the queue.
@returns The number of entries in the queue in [0,GetSize()].
*/
int GetNumEntries() const
{
return m_numEntries;
}
private:
Allocator * m_allocator; ///< The allocator passed in to the constructor.
T * m_entries; ///< Array of entries backing the queue (circular buffer).
int m_arraySize; ///< The size of the array, in number of entries. This is the "size" of the queue.
int m_startIndex; ///< The start index for the queue. This is the next value that gets popped off.
int m_numEntries; ///< The number of entries currently stored in the queue.
};
/**
Data structure that stores data indexed by sequence number.
Entries may or may not exist. If they don't exist the sequence value for the entry at that index is set to 0xFFFFFFFF.
This provides a constant time lookup for an entry by sequence number. If the entry at sequence modulo buffer size doesn't have the same sequence number, that sequence number is not stored.
This is incredibly useful and is used as the foundation of the packet level ack system and the reliable message send and receive queues.
@see Connection
*/
template <typename T> class SequenceBuffer
{
public:
/**
Sequence buffer constructor.
@param allocator The allocator to use.
@param size The size of the sequence buffer.
*/
SequenceBuffer( Allocator & allocator, int size )
{
yojimbo_assert( size > 0 );
m_size = size;
m_sequence = 0;
m_allocator = &allocator;
m_entry_sequence = (uint32_t*) YOJIMBO_ALLOCATE( allocator, sizeof( uint32_t ) * size );
m_entries = (T*) YOJIMBO_ALLOCATE( allocator, sizeof(T) * size );
Reset();
}
/**
Sequence buffer destructor.
*/
~SequenceBuffer()
{
yojimbo_assert( m_allocator );
YOJIMBO_FREE( *m_allocator, m_entries );
YOJIMBO_FREE( *m_allocator, m_entry_sequence );
m_allocator = NULL;
}
/**
Reset the sequence buffer.
Removes all entries from the sequence buffer and restores it to initial state.
*/
void Reset()
{
m_sequence = 0;
memset( m_entry_sequence, 0xFF, sizeof( uint32_t ) * m_size );
}
/**
Insert an entry in the sequence buffer.
IMPORTANT: If another entry exists at the sequence modulo buffer size, it is overwritten.
@param sequence The sequence number.
@returns The sequence buffer entry, which you must fill with your data. NULL if a sequence buffer entry could not be added for your sequence number (if the sequence number is too old for example).
*/
T * Insert( uint16_t sequence )
{
if ( sequence_greater_than( sequence + 1, m_sequence ) )
{
RemoveEntries( m_sequence, sequence );
m_sequence = sequence + 1;
}
else if ( sequence_less_than( sequence, m_sequence - m_size ) )
{
return NULL;
}
const int index = sequence % m_size;
m_entry_sequence[index] = sequence;
return &m_entries[index];
}
/**
Remove an entry from the sequence buffer.
@param sequence The sequence number of the entry to remove.
*/
void Remove( uint16_t sequence )
{
m_entry_sequence[ sequence % m_size ] = 0xFFFFFFFF;
}
/**
Is the entry corresponding to the sequence number available? eg. Currently unoccupied.
This works because older entries are automatically set back to unoccupied state as the sequence buffer advances forward.
@param sequence The sequence number.
@returns True if the sequence buffer entry is available, false if it is already occupied.
*/
bool Available( uint16_t sequence ) const
{
return m_entry_sequence[ sequence % m_size ] == 0xFFFFFFFF;
}
/**
Does an entry exist for a sequence number?
@param sequence The sequence number.
@returns True if an entry exists for this sequence number.
*/
bool Exists( uint16_t sequence ) const
{
return m_entry_sequence[ sequence % m_size ] == uint32_t( sequence );
}
/**
Get the entry corresponding to a sequence number.
@param sequence The sequence number.
@returns The entry if it exists. NULL if no entry is in the buffer for this sequence number.
*/
T * Find( uint16_t sequence )
{
const int index = sequence % m_size;
if ( m_entry_sequence[index] == uint32_t( sequence ) )
return &m_entries[index];
else
return NULL;
}
/**
Get the entry corresponding to a sequence number (const version).
@param sequence The sequence number.
@returns The entry if it exists. NULL if no entry is in the buffer for this sequence number.
*/
const T * Find( uint16_t sequence ) const
{
const int index = sequence % m_size;
if ( m_entry_sequence[index] == uint32_t( sequence ) )
return &m_entries[index];
else
return NULL;
}
/**
Get the entry at the specified index.
Use this to iterate across entries in the sequence buffer.
@param index The entry index in [0,GetSize()-1].
@returns The entry if it exists. NULL if no entry is in the buffer at the specified index.
*/
T * GetAtIndex( int index )
{
yojimbo_assert( index >= 0 );
yojimbo_assert( index < m_size );
return m_entry_sequence[index] != 0xFFFFFFFF ? &m_entries[index] : NULL;
}
/**
Get the entry at the specified index (const version).
Use this to iterate across entries in the sequence buffer.
@param index The entry index in [0,GetSize()-1].
@returns The entry if it exists. NULL if no entry is in the buffer at the specified index.
*/
const T * GetAtIndex( int index ) const
{
yojimbo_assert( index >= 0 );
yojimbo_assert( index < m_size );
return m_entry_sequence[index] != 0xFFFFFFFF ? &m_entries[index] : NULL;
}
/**
Get the most recent sequence number added to the buffer.
This sequence number can wrap around, so if you are at 65535 and add an entry for sequence 0, then 0 becomes the new "most recent" sequence number.
@returns The most recent sequence number.
@see yojimbo::sequence_greater_than
@see yojimbo::sequence_less_than
*/
uint16_t GetSequence() const
{
return m_sequence;
}
/**
Get the entry index for a sequence number.
This is simply the sequence number modulo the sequence buffer size.
@param sequence The sequence number.
@returns The sequence buffer index corresponding of the sequence number.
*/
int GetIndex( uint16_t sequence ) const
{
return sequence % m_size;
}
/**
Get the size of the sequence buffer.
@returns The size of the sequence buffer (number of entries).
*/
int GetSize() const
{
return m_size;
}
protected:
/**
Helper function to remove entries.
This is used to remove old entries as we advance the sequence buffer forward.
Otherwise, if when entries are added with holes (eg. receive buffer for packets or messages, where not all sequence numbers are added to the buffer because we have high packet loss),
and we are extremely unlucky, we can have old sequence buffer entries from the previous sequence # wrap around still in the buffer, which corrupts our internal connection state.
This actually happened in the soak test at high packet loss levels (>90%). It took me days to track it down :)
*/
void RemoveEntries( int start_sequence, int finish_sequence )
{
if ( finish_sequence < start_sequence )
finish_sequence += 65535;
yojimbo_assert( finish_sequence >= start_sequence );
if ( finish_sequence - start_sequence < m_size )
{
for ( int sequence = start_sequence; sequence <= finish_sequence; ++sequence )
m_entry_sequence[sequence % m_size] = 0xFFFFFFFF;
}
else
{
for ( int i = 0; i < m_size; ++i )
m_entry_sequence[i] = 0xFFFFFFFF;
}
}
private:
Allocator * m_allocator; ///< The allocator passed in to the constructor.
int m_size; ///< The size of the sequence buffer.
uint16_t m_sequence; ///< The most recent sequence number added to the buffer.