-
Notifications
You must be signed in to change notification settings - Fork 6
/
yojimbo.cs
7572 lines (6535 loc) · 335 KB
/
yojimbo.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
/*
Yojimbo Client/Server Network Library.
Copyright © 2016 - 2019, The Network Protocol Company, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define YOJIMBO_SERIALIZE_CHECKS
#define YOJIMBO_DEBUG_MEMORY_LEAKS
//#define YOJIMBO_DEBUG_MESSAGE_LEAKS
#define YOJIMBO_DEBUG_MESSAGE_BUDGET
#define YOJIMBO_ENABLE_LOGGING
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
namespace networkprotocol
{
#region defines
public class Allocator : IDisposable
{
internal static Allocator Default = new Allocator();
public void Dispose() { }
}
public static partial class yojimbo
{
public const int MAJOR_VERSION = 1;
public const int MINOR_VERSION = 0;
public const int PATCH_VERSION = 0;
public const int DEFAULT_TIMEOUT = 5;
public static Allocator DefaultAllocator => Allocator.Default;
}
#endregion
#region config
/// The library namespace.
static partial class yojimbo
{
public const int MaxClients = 64; ///< The maximum number of clients supported by this library. You can increase this if you want, but this library is designed around patterns that work best for [2,64] player games. If your game has less than 64 clients, reducing this will save memory.
public const int MaxChannels = 64; ///< The maximum number of message channels supported by this library. If you need less than 64 channels per-packet, reducing this will save memory.
public const int KeyBytes = 32; ///< Size of encryption key for dedicated client/server in bytes. Must be equal to key size for libsodium encryption primitive. Do not change.
public const int ConnectTokenBytes = 2048; ///< Size of the encrypted connect token data return from the matchmaker. Must equal size of NETCODE_CONNECT_TOKEN_BYTE (2048).
public const uint SerializeCheckValue = 0x12345678U; ///< The value written to the stream for serialize checks. See WriteStream::SerializeCheck and ReadStream::SerializeCheck.
public const int ConservativeMessageHeaderBits = 32; ///< Conservative number of bits per-message header.
public const int ConservativeFragmentHeaderBits = 64; ///< Conservative number of bits per-fragment header.
public const int ConservativeChannelHeaderBits = 32; ///< Conservative number of bits per-channel header.
public const int ConservativePacketHeaderBits = 16; ///< Conservative number of bits per-packet header.
}
/// Determines the reliability and ordering guarantees for a channel.
public enum ChannelType
{
CHANNEL_TYPE_RELIABLE_ORDERED, ///< Messages are received reliably and in the same order they were sent.
CHANNEL_TYPE_UNRELIABLE_UNORDERED ///< Messages are sent unreliably. Messages may arrive out of order, or not at all.
}
/**
Configuration properties for a message channel.
Channels let you specify different reliability and ordering guarantees for messages sent across a connection.
They may be configured as one of two types: reliable-ordered or unreliable-unordered.
Reliable ordered channels guarantee that messages (see Message) are received reliably and in the same order they were sent.
This channel type is designed for control messages and RPCs sent between the client and server.
Unreliable unordered channels are like UDP. There is no guarantee that messages will arrive, and messages may arrive out of order.
This channel type is designed for data that is time critical and should not be resent if dropped, like snapshots of world state sent rapidly
from server to client, or cosmetic events such as effects and sounds.
Both channel types support blocks of data attached to messages (see BlockMessage), but their treatment of blocks is quite different.
Reliable ordered channels are designed for blocks that must be received reliably and in-order with the rest of the messages sent over the channel.
Examples of these sort of blocks include the initial state of a level, or server configuration data sent down to a client on connect. These blocks
are sent by splitting them into fragments and resending each fragment until the other side has received the entire block. This allows for sending
blocks of data larger that maximum packet size quickly and reliably even under packet loss.
Unreliable-unordered channels send blocks as-is without splitting them up into fragments. The idea is that transport level packet fragmentation
should be used on top of the generated packet to split it up into into smaller packets that can be sent across typical Internet MTU (<1500 bytes).
Because of this, you need to make sure that the maximum block size for an unreliable-unordered channel fits within the maximum packet size.
Channels are typically configured as part of a ConnectionConfig, which is included inside the ClientServerConfig that is passed into the Client and Server constructors.
*/
public class ChannelConfig
{
public ChannelType type = ChannelType.CHANNEL_TYPE_RELIABLE_ORDERED; ///< Channel type: reliable-ordered or unreliable-unordered.
public bool disableBlocks = false; ///< Disables blocks being sent across this channel.
public int sentPacketBufferSize = 1024; ///< Number of packet entries in the sent packet sequence buffer. Please consider your packet send rate and make sure you have at least a few seconds worth of entries in this buffer.
public int messageSendQueueSize = 1024; ///< Number of messages in the send queue for this channel.
public int messageReceiveQueueSize = 1024; ///< Number of messages in the receive queue for this channel.
public int maxMessagesPerPacket = 256; ///< Maximum number of messages to include in each packet. Will write up to this many messages, provided the messages fit into the channel packet budget and the number of bytes remaining in the packet.
public int packetBudget = -1; ///< Maximum amount of message data to write to the packet for this channel (bytes). Specifying -1 means the channel can use up to the rest of the bytes remaining in the packet.
public int maxBlockSize = 256 * 1024; ///< The size of the largest block that can be sent across this channel (bytes).
public int blockFragmentSize = 1024; ///< Blocks are split up into fragments of this size (bytes). Reliable-ordered channel only.
public float messageResendTime = 0.1f; ///< Minimum delay between message resends (seconds). Avoids sending the same message too frequently. Reliable-ordered channel only.
public float blockFragmentResendTime = 0.25f; ///< Minimum delay between block fragment resends (seconds). Avoids sending the same fragment too frequently. Reliable-ordered channel only.
public int MaxFragmentsPerBlock =>
maxBlockSize / blockFragmentSize;
}
/**
Configures connection properties and the set of channels for sending and receiving messages.
Specifies the maximum packet size to generate, and the number of message channels, and the per-channel configuration data. See ChannelConfig for details.
Typically configured as part of a ClientServerConfig which is passed into Client and Server constructors.
*/
public class ConnectionConfig
{
public int numChannels = 1; ///< Number of message channels in [1,MaxChannels]. Each message channel must have a corresponding configuration below.
public int maxPacketSize = 8 * 1024; ///< The maximum size of packets generated to transmit messages between client and server (bytes).
public ChannelConfig[] channel = BufferEx.NewT<ChannelConfig>(yojimbo.MaxChannels); ///< Per-channel configuration. See ChannelConfig for details.
}
/**
Configuration shared between client and server.
Passed to Client and Server constructors to configure their behavior.
Please make sure that the message configuration is identical between client and server.
*/
public class ClientServerConfig : ConnectionConfig
{
public ulong protocolId = 0; ///< Clients can only connect to servers with the same protocol id. Use this for versioning.
public int timeout = yojimbo.DEFAULT_TIMEOUT; ///< Timeout value in seconds. Set to negative value to disable timeouts (for debugging only).
public int clientMemory = 0; /* 10 * 1024 * 1024 */ ///< Memory allocated inside Client for packets, messages and stream allocations (bytes)
public int serverGlobalMemory = 0; /* 10 * 1024 * 1024 */ ///< Memory allocated inside Server for global connection request and challenge response packets (bytes)
public int serverPerClientMemory = 0; /* 10 * 1024 * 1024 */ ///< Memory allocated inside Server for packets, messages and stream allocations per-client (bytes)
public bool networkSimulator = true; ///< If true then a network simulator is created for simulating latency, jitter, packet loss and duplicates.
public int maxSimulatorPackets = 4 * 1024; ///< Maximum number of packets that can be stored in the network simulator. Additional packets are dropped.
public int fragmentPacketsAbove = 1024; ///< Packets above this size (bytes) are split apart into fragments and reassembled on the other side.
public int packetFragmentSize = 1024; ///< Size of each packet fragment (bytes).
public int maxPacketFragments; ///< Maximum number of fragments a packet can be split up into.
public int packetReassemblyBufferSize = 64; ///< Number of packet entries in the fragmentation reassembly buffer.
public int ackedPacketsBufferSize = 256; ///< Number of packet entries in the acked packet buffer. Consider your packet send rate and aim to have at least a few seconds worth of entries.
public int receivedPacketsBufferSize = 256; ///< Number of packet entries in the received packet sequence buffer. Consider your packet send rate and aim to have at least a few seconds worth of entries.
public ClientServerConfig()
{
maxPacketFragments = (int)Math.Ceiling((decimal)(maxPacketSize / packetFragmentSize));
}
}
#endregion
static partial class yojimbo
{
#region init / term
/**
Initialize the yojimbo library.
Call this before calling any yojimbo library functions.
@returns True if the library was successfully initialized, false otherwise.
*/
public static bool InitializeYojimbo()
{
if (netcode.init() != netcode.OK)
return false;
if (reliable.init() != reliable.OK)
return false;
return true;
}
/**
Shutdown the yojimbo library.
Call this after you finish using the library and it will run some checks for you (for example, checking for memory leaks in debug build).
*/
public static void ShutdownYojimbo()
{
reliable.term();
netcode.term();
}
#endregion
#region utils
///**
// Template function to get the minimum of two values.
// @param a The first value.
// @param b The second value.
// @returns The minimum of a and b.
// */
//public static T min<T>(T a, T b) where T : IComparable =>
// (a < b) ? a : b;
///**
// Template function to get the maximum of two values.
// @param a The first value.
// @param b The second value.
// @returns The maximum of a and b.
// */
//public static T max<T>(T a, T b) where T : IComparable =>
// (a > b) ? a : b;
///**
// Template function to clamp a value.
// @param value The value to be clamped.
// @param a The minimum value.
// @param b The minimum value.
// @returns The clamped value in [a,b].
// */
//public static T clamp<T>(T value, T a, T b) where T : IComparable
//{
// if (value < a)
// return a;
// else if (value > b)
// return b;
// else
// return value;
//}
///**
// Swap two values.
// @param a First value.
// @param b Second value.
// */
//public static void swap<T>(T a, T b)
//{
// T tmp = a;
// a = b;
// b = tmp;
//}
///**
// Get the absolute value.
// @param value The input value.
// @returns The absolute value.
// */
//public static T abs<T>(T value) where T : IComparable =>
// (value < 0) ? -value : value;
/**
Sleep for approximately this number of seconds.
@param time number of seconds to sleep for.
*/
public static void sleep(double time)
{
var milliseconds = (int)(time * 1000);
Thread.Sleep(milliseconds);
}
/**
Get a high precision time in seconds since the application has started.
Please store time in doubles so you retain sufficient precision as time increases.
@returns Time value in seconds.
*/
public static double time() => DateTime.Now.ToOADate();
public static ulong ctime() => (ulong)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
#endregion
#region assert / logging
public const int LOG_LEVEL_NONE = 0;
public const int LOG_LEVEL_ERROR = 1;
public const int LOG_LEVEL_INFO = 2;
public const int LOG_LEVEL_DEBUG = 3;
static int log_level_ = 0;
/**
Set the yojimbo log level.
Valid log levels are: YOJIMBO_LOG_LEVEL_NONE, YOJIMBO_LOG_LEVEL_ERROR, YOJIMBO_LOG_LEVEL_INFO and YOJIMBO_LOG_LEVEL_DEBUG
@param level The log level to set. Initially set to YOJIMBO_LOG_LEVEL_NONE.
*/
public static void log_level(int level)
{
log_level_ = level;
netcode.log_level(level);
reliable.log_level(level);
}
public static Action<string, string, string, int> assert_function = default_assert_handler;
static void default_assert_handler(string condition, string function, string file, int line)
{
Console.Write($"assert failed: ( {condition} ), function {function}, file {file}, line {line}\n");
Debugger.Break();
Environment.Exit(1);
}
static Action<string> printf_function =
x => Console.Write(x);
/**
Printf function used by yojimbo to emit logs.
This function internally calls the printf callback set by the user.
@see yojimbo_set_printf_function
*/
#if YOJIMBO_ENABLE_LOGGING
public static void printf(int level, string format)
{
if (level > log_level_) return;
printf_function(format);
}
#else
public static void printf(int level, string format) { }
#endif
/**
Assert function used by yojimbo.
This assert function lets the user override the assert presentation.
@see yojimbo_set_assert_functio
*/
[DebuggerStepThrough, Conditional("DEBUG")]
public static void assert(bool condition)
{
if (!condition)
{
var stackFrame = new StackTrace().GetFrame(1);
assert_function?.Invoke(null, stackFrame.GetMethod().Name, stackFrame.GetFileName(), stackFrame.GetFileLineNumber());
Environment.Exit(1);
}
}
/**
Call this to set the printf function to use for logging.
@param function The printf callback function.
*/
public static void set_printf_function(Action<string> function)
{
assert(function != null);
printf_function = function;
netcode.set_printf_function(function);
reliable.set_printf_function(function);
}
/**
Call this to set the function to call when an assert triggers.
@param function The assert callback function.
*/
public static void set_assert_function(Action<string, string, string, int> function)
{
assert_function = function;
netcode.set_assert_function(function);
reliable.set_assert_function(function);
}
#endregion
#region utils
/**
Generate cryptographically secure random data.
@param data The buffer to store the random data.
@param bytes The number of bytes of random data to generate.
*/
public static void random_bytes(ref ulong data, int bytes) =>
netcode.random_bytes(ref data, bytes);
public static void random_bytes(byte[] data, int bytes) =>
netcode.random_bytes(data, 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].
*/
public static int random_int(int a, int b)
{
assert(a < b);
var result = a + BufferEx.Rand() % (b - a + 1);
assert(result >= a);
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].
*/
public static float random_float(float a, float b)
{
assert(a < b);
var random = BufferEx.Rand() / (float)BufferEx.RAND_MAX;
var diff = b - a;
var 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<uint 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<uint 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 uint result = (min == max) ? 0 : (Log2 < uint(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.
*/
public static uint popcount(uint x)
{
var result = x - ((x >> 1) & 0x5555555555555555UL);
result = (result & 0x3333333333333333UL) + ((result >> 2) & 0x3333333333333333UL);
return (byte)(unchecked(((result + (result >> 4)) & 0xF0F0F0F0F0F0F0FUL) * 0x101010101010101UL) >> 56);
}
/**
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.
*/
public static uint log2(uint x)
{
var a = x | (x >> 1);
var b = a | (a >> 2);
var c = b | (b >> 4);
var d = c | (c >> 8);
var e = d | (d >> 16);
var 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.
*/
public static int bits_required(uint min, uint max) =>
(min == max) ? 0 : (int)log2(max - min) + 1;
/**
Reverse the order of bytes in a 64 bit integer.
@param value The input value.
@returns The input value with the byte order reversed.
*/
public static ulong bswap(ulong value)
{
value = (value & 0x00000000FFFFFFFF) << 32 | (value & 0xFFFFFFFF00000000) >> 32;
value = (value & 0x0000FFFF0000FFFF) << 16 | (value & 0xFFFF0000FFFF0000) >> 16;
value = (value & 0x00FF00FF00FF00FF) << 8 | (value & 0xFF00FF00FF00FF00) >> 8;
return value;
}
/**
Reverse the order of bytes in a 32 bit integer.
@param value The input value.
@returns The input value with the byte order reversed.
*/
public static uint bswap(uint value) =>
(value & 0x000000ff) << 24 | (value & 0x0000ff00) << 8 | (value & 0x00ff0000) >> 8 | (value & 0xff000000) >> 24;
/**
Reverse the order of bytes in a 16 bit integer.
@param value The input value.
@returns The input value with the byte order reversed.
*/
public static ushort bswap(ushort value) =>
(ushort)((value & 0x00ff) << 8 | (value & 0xff00) >> 8);
/**
Template to convert an integer value from local byte order to network byte order.
IMPORTANT: Because most machines running yojimbo are little endian, yojimbo defines network byte order to be little endian.
@param value The input value in local byte order. Supported integer types: uint64_t, uint, 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
*/
public static T host_to_network<T>(T value) =>
#if YOJIMBO_BIG_ENDIAN
bswap(value);
#else
value;
#endif
/**
Template to convert an integer value from network byte order to local byte order.
IMPORTANT: Because most machines running yojimbo are little endian, yojimbo defines network byte order to be little endian.
@param value The input value in network byte order. Supported integer types: uint64_t, uint, 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
*/
public static T network_to_host<T>(T value) =>
#if YOJIMBO_BIG_ENDIAN
bswap(value);
#else
value;
#endif
/**
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.
*/
public static bool sequence_greater_than(ushort s1, ushort s2) =>
((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.
*/
public static bool sequence_less_than(ushort s1, ushort s2) =>
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.
*/
public static int signed_to_unsigned(int n) =>
(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.
*/
public static int unsigned_to_signed(uint n) =>
(int)((n >> 1) ^ (-(int)(n & 1)));
#if YOJIMBO_WITH_MBEDTLS
/**
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.
*/
public static int base64_encode_string(string input, string output, int output_size) =>
throw new NotImplementedException();
/**
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.
*/
public static int base64_decode_string(string input, string output, int output_size) =>
throw new NotImplementedException();
/**
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.
*/
public static int base64_encode_data(byte[] input, int input_length, string output, int output_size) =>
throw new NotImplementedException();
/**
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.
*/
public static int base64_decode_data(string input, byte[] output, int output_size) =>
throw new NotImplementedException();
/**
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.
*/
public static void print_bytes(string label, byte[] data, int data_bytes)
{
Console.Write($"{label}: ");
for (var i = 0; i < data_bytes; ++i)
Console.Write($"0x{(int)data[i]},");
Console.Write($" ({data_bytes} bytes)\n");
}
#endif
#endregion
}
#region BitArray
/**
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.
*/
public class BitArray
{
/**
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.
*/
public BitArray(Allocator allocator, int size)
{
yojimbo.assert(size > 0);
m_allocator = allocator;
m_size = size;
m_bytes = 8 * ((size / 64) + ((size % 64) != 0 ? 1 : 0));
yojimbo.assert(m_bytes > 0);
m_data = new ulong[m_bytes];
Clear();
}
/**
The bit array destructor.
*/
public void Dispose()
{
yojimbo.assert(m_data != null);
yojimbo.assert(m_allocator != null);
m_data = null;
m_allocator = null;
}
/**
Clear all bit values to zero.
*/
public void Clear()
{
yojimbo.assert(m_data != null);
BufferEx.Set(m_data, 0, m_bytes);
}
/**
Set a bit to 1.
@param index The index of the bit.
*/
public void SetBit(int index)
{
yojimbo.assert(index >= 0);
yojimbo.assert(index < m_size);
var data_index = index >> 6;
var bit_index = index & ((1 << 6) - 1);
yojimbo.assert(bit_index >= 0);
yojimbo.assert(bit_index < 64);
m_data[data_index] |= 1UL << bit_index;
}
/**
Clear a bit to 0.
@param index The index of the bit.
*/
public void ClearBit(int index)
{
yojimbo.assert(index >= 0);
yojimbo.assert(index < m_size);
var data_index = index >> 6;
var bit_index = index & ((1 << 6) - 1);
m_data[data_index] &= ~(1UL << 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.
*/
public ulong GetBit(int index)
{
yojimbo.assert(index >= 0);
yojimbo.assert(index < m_size);
var data_index = index >> 6;
var 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.
*/
public int GetSize() =>
m_size;
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.
ulong[] 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 );
}
#endregion
#region QueueEx<T>
/**
A simple templated queue.
This is a FIFO queue. First entry in, first entry out.
*/
public class QueueEx<T> : Queue<T>, IDisposable
{
static readonly MethodInfo GetElementInfo = typeof(Queue<T>).GetMethod("GetElement", BindingFlags.Instance | BindingFlags.NonPublic);
public QueueEx(Allocator allocator, int capacity) : base(capacity) { Size = capacity; }
T[] _elements;
public T this[int id] => GetElementInfo != null
? (T)GetElementInfo.Invoke(this, new object[] { id })
: (_elements ?? (_elements = ToArray()))[id];
public bool IsEmpty => Count == 0;
public bool IsFull => Count == Size;
public int NumEntries => Count;
public readonly int Size;
public void Dispose() { }
public new T Dequeue()
{
_elements = null;
yojimbo.assert(!IsEmpty);
return base.Dequeue();
}
public new void Enqueue(T item)
{
_elements = null;
yojimbo.assert(!IsFull);
base.Enqueue(item);
}
}
#endregion
#region SequenceBuffer<T>
/**
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
*/
public class SequenceBuffer<T> where T : class, new()
{
/**
Sequence buffer constructor.
@param allocator The allocator to use.
@param size The size of the sequence buffer.
*/
public SequenceBuffer(Allocator allocator, int size)
{
yojimbo.assert(size > 0);
m_size = size;
m_sequence = 0;
m_allocator = allocator;
m_entry_sequence = new uint[size];
m_entries = BufferEx.NewT<T>(size);
Reset();
}
/**
Sequence buffer destructor.
*/
public void Dispose()
{
yojimbo.assert(m_allocator != null);
m_entries = null;
m_entry_sequence = null;
m_allocator = null;
}
/**
Reset the sequence buffer.
Removes all entries from the sequence buffer and restores it to initial state.
*/
public void Reset()
{
m_sequence = 0;
BufferEx.Set(m_entry_sequence, 0xFF, sizeof(uint) * 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).
*/
public T Insert(ushort sequence)
{
if (yojimbo.sequence_greater_than((ushort)(sequence + 1), m_sequence))
{
RemoveEntries(m_sequence, sequence);
m_sequence = (ushort)(sequence + 1);
}
else if (yojimbo.sequence_less_than(sequence, (ushort)(m_sequence - m_size)))
return null;
var 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.
*/
public void Remove(ushort 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.
*/
public bool Available(ushort sequence) =>
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.
*/
public bool Exists(ushort sequence) =>
m_entry_sequence[sequence % m_size] == 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.
*/
public T Find(ushort sequence)
{
var index = sequence % m_size;
return (m_entry_sequence[index] == sequence) ?
m_entries[index] :
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.
*/
public 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 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
*/
public ushort GetSequence() =>
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.
*/
public int GetIndex(ushort sequence) =>
sequence % m_size;
/**
Get the size of the sequence buffer.
@returns The size of the sequence buffer (number of entries).
*/
public int GetSize() =>
m_size;
/**
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 :)
*/
protected 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;
}
Allocator m_allocator; ///< The allocator passed in to the constructor.
int m_size; ///< The size of the sequence buffer.
ushort m_sequence; ///< The most recent sequence number added to the buffer.
uint[] m_entry_sequence; ///< Array of sequence numbers corresponding to each sequence buffer entry for fast lookup. Set to 0xFFFFFFFF if no entry exists at that index.
T[] m_entries; ///< The sequence buffer entries. This is where the data is stored per-entry. Separate from the sequence numbers for fast lookup (hot/cold split) when the data per-sequence number is relatively large.
//SequenceBuffer( SequenceBuffer<T> other );
//SequenceBuffer<T> & operator = ( const SequenceBuffer<T> & other );
}
#endregion
#region BitWriter