Repository navigation
Expand file tree
/
Copy pathTonkineseTools.h
More file actions
877 lines (680 loc) · 23.1 KB
/
Copy pathTonkineseTools.h
File metadata and controls
877 lines (680 loc) · 23.1 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
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
/** \file
\brief Tonk Implementation: Tools
\copyright Copyright (c) 2017-2018 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* 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.
* Neither the name of Tonkinese 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.
*/
#pragma once
//------------------------------------------------------------------------------
// Compile Switches
// Enable detailed stats collection?
//#define TONK_DETAILED_STATS
// Enable debug assertions in release mode?
//#define TONK_DEBUG_IN_RELEASE
// Enable verbose logging of outgoing data?
//#define TONK_ENABLE_VERBOSE_OUTGOING
// Enable verbose logging of incoming data?
//#define TONK_ENABLE_VERBOSE_INCOMING
// Enable verbose logging for bandwidth control?
//#define TONK_ENABLE_VERBOSE_BANDWIDTH
//------------------------------------------------------------------------------
// Common Includes
#include "Logger.h"
#include "Counter.h"
#include "PacketAllocator.h"
#include "thirdparty/IncludeAsio.h" // Asio
#include "SiameseTools.h"
#include "SiameseSerializers.h"
#include "tonk.h"
#include "cymric.h"
#if !defined(_WIN32)
#include <pthread.h>
#endif // _WIN32
#include <stdint.h>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <thread>
//------------------------------------------------------------------------------
// Portability macros
// Specify an intentionally unused variable (often a function parameter)
#define TONK_UNUSED(x) (void)(x);
// Compiler-specific debug break
#if defined(_DEBUG) || defined(DEBUG) || defined(TONK_DEBUG_IN_RELEASE)
#define TONK_DEBUG
#if defined(_WIN32)
#define TONK_DEBUG_BREAK() __debugbreak()
#else // _WIN32
#define TONK_DEBUG_BREAK() __builtin_trap()
#endif // _WIN32
#define TONK_DEBUG_ASSERT(cond) { if (!(cond)) { TONK_DEBUG_BREAK(); } }
#define TONK_IF_DEBUG(x) x;
#else // _DEBUG
#define TONK_DEBUG_BREAK() ;
#define TONK_DEBUG_ASSERT(cond) ;
#define TONK_IF_DEBUG(x) ;
#endif // _DEBUG
// Compiler-specific force inline keyword
#if defined(_MSC_VER)
#define TONK_FORCE_INLINE inline __forceinline
#else // _MSC_VER
#define TONK_FORCE_INLINE inline __attribute__((always_inline))
#endif // _MSC_VER
// Compiler-specific align keyword
#if defined(_MSC_VER)
#define TONK_ALIGNED(x) __declspec(align(x))
#else // _MSC_VER
#define TONK_ALIGNED(x) __attribute__ ((aligned(x)))
#endif // _MSC_VER
namespace tonk {
//------------------------------------------------------------------------------
// Logging
#ifdef TONK_DEBUG
static const logger::Level MinimumLogLevel = logger::Level::Debug;
#else
static const logger::Level MinimumLogLevel = logger::Level::Silent;
#endif
//------------------------------------------------------------------------------
// Convenience Types
/// UDP/IP source or destination network address
typedef asio::ip::udp::endpoint UDPAddress;
/// 64-bit nonce type
typedef uint64_t NonceT;
/// Windowed unsigned maximum/minimum
typedef siamese::WindowedMinMax< unsigned, siamese::WindowedMaxCompare<unsigned> > WinMaxUnsigned;
typedef siamese::WindowedMinMax< unsigned, siamese::WindowedMinCompare<unsigned> > WinMinUnsigned;
/// Use CounterSeqNo to represent reliable sequence numbers
typedef Counter<uint32_t, SIAMESE_PACKET_NUM_BITS> CounterSeqNo;
//------------------------------------------------------------------------------
// Threads
/// Set the current thread name
void SetCurrentThreadName(const char* name);
//------------------------------------------------------------------------------
// String Tools
/// Convert value to hex string
std::string HexString(uint64_t value);
/// Escapes strings for insertion into a JSON field
std::string EscapeJsonString(const std::string& s);
/// Escapes values for insertion into a JSON field
template<typename T>
inline std::string JsonValue(const T value) {
static_assert(std::is_arithmetic<T>::value, "Must be an arithmetic type or a string");
return std::to_string(value);
}
template<> inline std::string JsonValue(const std::string value) {
return "\"" + EscapeJsonString(value) + "\"";
}
template<> inline std::string JsonValue(const char* value) {
if (value == nullptr)
return "null";
return JsonValue(static_cast<std::string>(value));
}
template<> inline std::string JsonValue(const char value) {
return std::string("\"") + value + "\"";
}
template<> inline std::string JsonValue(const bool value) {
return value ? "true" : "false";
}
TONK_FORCE_INLINE void SafeCopyCStr(char* dest, size_t destBytes, const char* src)
{
#if defined(_MSC_VER)
strncpy_s(dest, destBytes, src, _TRUNCATE);
#else // _MSC_VER
strncpy(dest, src, destBytes);
#endif // _MSC_VER
// No null-character is implicitly appended at the end of destination
dest[destBytes - 1] = '\0';
}
/// Fill in the TonkAddress structure from a UDPAddress
void SetTonkAddressStr(TonkAddress& addrOut, const UDPAddress& addrIn);
/// Case-insensitive string comparison
TONK_FORCE_INLINE int StrCaseCompare(const char* a, const char* b)
{
#if defined(_WIN32)
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
return ::_stricmp(a, b);
#else
return ::stricmp(a, b);
#endif
#else
return strcasecmp(a, b);
#endif
}
/// Convert unsigned 32-bit integer to decimal string.
/// Optimized for numbers that count up from 0 and are often close to 0.
/// Does not write a null terminator '\0'.
/// Returns the number of characters (digits) written
unsigned inplace_itoa_vitaut_1_cat(char* buf, uint32_t val);
//------------------------------------------------------------------------------
// Timer
/**
Implements a polled timer that expires after a given number of microseconds.
Supports timers that tick repeatedly and timers that tick once and then stop.
Fixes a common bug in timer code: If the OS hibernates and resumes, then all
timeouts pop unexpectedly because the timestamps have jumped ahead. This is
fixed by requiring that the timer expires 2 times before firing, which
guarantees that time is moving forward.
*/
class Timer
{
public:
/// Behavior for the timer on expiration: Set by SetTimeoutUsec()
enum class Behavior
{
Restart, ///< Restart timer on expiration
Stop ///< Do not reset timer on expiration (require manual Reset)
};
/// Set the timeout in microseconds and reset behavior
void SetTimeoutUsec(uint64_t timeoutUsec, Behavior resetBehavior = Behavior::Stop);
/// Start the timer ticking
void Start(uint64_t nowUsec);
/// Is the timer expired?
bool IsExpired(uint64_t nowUsec);
/// Force timer to expire next time IsExpired() is called
void ForceExpire();
/// Stop the timer from ticking
void Stop();
protected:
/**
Number of timer divisions until timer expires.
This number must be kept small, because it is the minimum number of
calls to IsExpired() that must be made in order for it to return true.
The main cause of timers lurching forward (from the timer's perspective)
is the computer going to sleep. So the minimum number of divisions
might be set to 2. I didn't want to subdivide any further because it
imposes a minimum number of ticks before a timeout.
*/
static const unsigned kTickDivisions = 2; // Power-of-two preferred
/// Microsecond timestamp when the timer started to wait
uint64_t WaitStartUsec = 0;
/// Number of microseconds per division
uint64_t UsecPerDivision = 0;
/// Counter of divisions that have expired so far
unsigned DivisionsExpired = 0;
/// Behavior when timer resets
Behavior ResetBehavior = Behavior::Stop;
/// Is the timer currently ticking?
bool IsTicking = false;
};
//------------------------------------------------------------------------------
// Result
/// Type of error code
enum class ErrorType
{
Tonk,
Siamese,
Asio,
Cymric,
LastErr,
Encrypt,
Zstd,
Count // For asserts
};
/// Convert ErrorType to string for printing
const char* ErrorTypeToString(ErrorType errorType);
/// Error code type
typedef int64_t ErrorCodeT;
/// Error result structure allocated when errors occur
struct ErrorResult
{
const char* Source = nullptr;
std::string Description;
ErrorType Type = ErrorType::Tonk;
ErrorCodeT Code = Tonk_Error;
std::string ToJson() const;
explicit ErrorResult(const char* source,
const std::string& desc,
ErrorType type,
ErrorCodeT code)
: Source(source)
, Description(desc)
, Type(type)
, Code(code)
{
}
explicit ErrorResult(const std::string& desc)
: Description(desc)
{
}
};
/// Allows passing failure codes up the stack to the API
struct Result
{
/// Error object pointer. nullptr: Operation successful
mutable ErrorResult* Error;
TONK_FORCE_INLINE bool IsGood() const
{
return nullptr == Error;
}
TONK_FORCE_INLINE bool IsFail() const
{
return nullptr != Error;
}
static inline Result Success()
{
Result success;
return success;
}
static inline Result OutOfMemory()
{
std::string empty;
return Result("Out of memory", empty, ErrorType::Tonk, Tonk_OOM);
}
std::string ToJson() const;
TONK_FORCE_INLINE ErrorCodeT GetErrorCode() const
{
if (IsGood()) {
return Tonk_Success;
}
return Error->Code;
}
~Result()
{
delete Error;
}
explicit TONK_FORCE_INLINE Result()
: Error(nullptr)
{
}
explicit TONK_FORCE_INLINE Result(const std::string& desc)
: Error(new ErrorResult(desc))
{
}
explicit TONK_FORCE_INLINE Result(const char* source,
const std::string& desc,
ErrorType type = ErrorType::Tonk,
ErrorCodeT code = Tonk_Error)
: Error(new ErrorResult(source, desc, type, code))
{
}
/// Modifies the provided result object to move the error into lval object
TONK_FORCE_INLINE Result(const Result& result)
: Error(result.Error)
{
result.Error = nullptr;
}
/// Modifies the provided result object to move the error into lval object
TONK_FORCE_INLINE Result& operator=(const Result& result)
{
Error = result.Error;
result.Error = nullptr;
return *this;
}
};
//------------------------------------------------------------------------------
// Intrinsics
/// Returns first position with set bit (LSB = 0)
/// Precondition: x != 0
TONK_FORCE_INLINE unsigned NonzeroLowestBitIndex(uint32_t x)
{
#ifdef _MSC_VER
unsigned long index;
// Note: Ignoring result because x != 0
_BitScanReverse(&index, x);
return (unsigned)index;
#else
// Note: Ignoring return value of 0 because x != 0
return 31 - (unsigned)__builtin_clz(x);
#endif
}
//------------------------------------------------------------------------------
// Mutex
static const unsigned kMutexSpinCount = 1000;
#ifdef _WIN32
struct Lock
{
Lock(const Lock&) = delete;
Lock& operator=(const Lock&) = delete;
CRITICAL_SECTION cs;
Lock() { ::InitializeCriticalSectionAndSpinCount(&cs, (DWORD)kMutexSpinCount); }
~Lock() { ::DeleteCriticalSection(&cs); }
bool TryEnter() { return ::TryEnterCriticalSection(&cs) != FALSE; }
void Enter() { ::EnterCriticalSection(&cs); }
void Leave() { ::LeaveCriticalSection(&cs); }
};
#else
struct Lock
{
Lock(const Lock&) = delete;
Lock& operator=(const Lock&) = delete;
TONK_FORCE_INLINE Lock() {}
std::recursive_mutex cs;
bool TryEnter() { return cs.try_lock(); }
void Enter() { cs.lock(); }
void Leave() { cs.unlock(); }
};
#endif
class Locker
{
public:
Locker(Lock& lock) {
TheLock = &lock;
if (TheLock)
TheLock->Enter();
}
~Locker() { Clear(); }
bool TrySet(Lock& lock) {
Clear();
if (!lock.TryEnter())
return false;
TheLock = &lock;
return true;
}
void Set(Lock& lock) {
Clear();
lock.Enter();
TheLock = &lock;
}
void Clear() {
if (TheLock)
TheLock->Leave();
TheLock = nullptr;
}
protected:
Lock* TheLock;
};
//------------------------------------------------------------------------------
// SecureRandom
/// Initialize the generator
Result SecureRandom_Initialize();
/// Generate next value
Result SecureRandom_Next(uint8_t* key, unsigned bytes);
//------------------------------------------------------------------------------
// Source Address Hash
/// Initialize the address hash function's secret key.
/// Must be called after SecureRandom_Initialize()
Result InitializeAddressHash();
/// Hash IP address + port using fastest approach available.
/// Result is salted by a random secret on each run to make it unpredictable
uint64_t HashUDPAddress(const UDPAddress& addr);
struct UDPAddressHasher
{
TONK_FORCE_INLINE std::size_t operator()(UDPAddress const& addr) const noexcept
{
return (std::size_t)HashUDPAddress(addr);
}
};
struct UInt32Hasher
{
TONK_FORCE_INLINE std::size_t operator()(uint32_t const& id) const noexcept
{
return id;
}
};
//------------------------------------------------------------------------------
// BufferAllocator
/// Threadsafe version of pktalloc::Allocator
class BufferAllocator
{
public:
/// Allocate a new buffer
TONK_FORCE_INLINE uint8_t* Allocate(unsigned bytes)
{
Locker locker(AllocatorLock);
return Allocator.Allocate(bytes);
}
/// Shrink the given buffer to size
TONK_FORCE_INLINE void Shrink(void* buffer, unsigned bytes)
{
Locker locker(AllocatorLock);
return Allocator.Shrink(reinterpret_cast<uint8_t*>(buffer), bytes);
}
/// Free the buffer
TONK_FORCE_INLINE void Free(void* buffer)
{
Locker locker(AllocatorLock);
return Allocator.Free(reinterpret_cast<uint8_t*>(buffer));
}
/// Get statistics for the allocator
TONK_FORCE_INLINE uint64_t GetUsedMemory() const
{
Locker locker(AllocatorLock);
return Allocator.GetMemoryAllocatedBytes();
}
protected:
/// Lock protecting the allocator
mutable Lock AllocatorLock;
/// Buffer allocator
pktalloc::Allocator Allocator;
};
//------------------------------------------------------------------------------
// DatagramBuffer
struct DatagramBuffer
{
/// Pointer to valid data buffer
uint8_t* Data = nullptr;
/// Number of valid data bytes
unsigned Bytes = 0;
/// Pointer to pass when freeing
void* FreePtr = nullptr;
/// Allocator to use when freeing
BufferAllocator* AllocatorForFree = nullptr;
TONK_FORCE_INLINE void Free() const
{
if (AllocatorForFree) {
AllocatorForFree->Free(FreePtr);
}
}
};
//------------------------------------------------------------------------------
// Detailed Statistics Memory
#ifdef TONK_DETAILED_STATS
enum StatsTypes
{
// These are all reset each Connection Tick
Stats_FramingBytesSent,
Stats_AcksBytesSent,
Stats_UnreliableBytesSent,
Stats_UnorderedBytesSent,
Stats_ControlBytesSent,
Stats_ReliableBytesSent,
Stats_LowPriBytesSent,
Stats_RetransmitSent,
Stats_CompressedBytesSent,
Stats_CompressionSavedBytes,
Stats_PaddingBytesSent,
Stats_FootersBytesSent,
Stats_SiameseBytesSent,
Stats_DummyBytesSent,
Stats_BytesReceived,
Stats_MinUsecOWD,
Stats_MaxUsecOWD,
Stats_Count,
// The first few stats are incremented in the Outgoing queue
Stats_QueueCount = Stats_PaddingBytesSent,
// The first set are related to sending data
Stats_SendCount = Stats_BytesReceived,
};
const char* GetStatsTypesName(unsigned type);
struct DetailStats
{
/// Statistics counters
uint32_t Stats[Stats_Count];
/// Timestamp for this statistics
uint64_t TimestampUsec;
};
static const unsigned kDeetsPerRegion = 4000 / sizeof(DetailStats);
struct DetailStatsRegion
{
/// Array of detailed stats
DetailStats Deets[kDeetsPerRegion];
/// Number of details elements written so far
unsigned WrittenCount = 0;
};
/// Zero-copy growing statistics log
class DetailStatsMemory
{
public:
TONK_FORCE_INLINE DetailStatsMemory()
{
WriteStats = &DevNull;
}
TONK_FORCE_INLINE ~DetailStatsMemory()
{
Clear();
}
/// Clear all data and reset read/write pointers
void Clear();
/// Get current stats pointer. Never returns nullptr
TONK_FORCE_INLINE DetailStats* GetWritePtr() const
{
return WriteStats;
}
/// Advance to next detail stats to write (will be cleared to zeros)
void NextWrite();
/// Start read iterator over from the beginning
TONK_FORCE_INLINE void ResetRead()
{
NextReadRegionIndex = 0;
NextReadDetailIndex = 0;
}
/// Get next detail stats to read.
/// Returns null if no detail stats remain to be read
DetailStats* GetNextRead();
/// Get memory used
unsigned GetMemoryUsed() const;
/// Write data in JSON format to a text file.
/// If no file name is provided, it uses a default name
void WriteJsonToFile();
protected:
/// Array of all the allocated regions
pktalloc::LightVector<DetailStatsRegion*> RegionPtrs;
/// The next region index for reading
unsigned NextReadRegionIndex = 0;
/// The next detail stats index for reading
unsigned NextReadDetailIndex = 0;
/// The next region index for writing
unsigned NextWriteRegionIndex = 0;
/// Stats writing
DetailStats* WriteStats = nullptr;
/// Used if out of memory or disabled
DetailStats DevNull;
};
#endif // TONK_DETAILED_STATS
//------------------------------------------------------------------------------
// RefCounter
/**
RefCounter keeps track of the number of references to an object, and whether
or not that object is attempting to shutdown. The RefCounter itself holds
one reference that is released on StartShutdown().
*/
class RefCounter
{
public:
/// Is the object still in use by Tonk or application?
/// If not then it can be deleted.
TONK_FORCE_INLINE bool ObjectInUse() const
{
TONK_DEBUG_ASSERT(RefCount >= 0);
return AppHoldsReference || RefCount != 0;
}
//--------------------------------------------------------------------------
// Application reference
/// Release application reference
void AppRelease();
TONK_FORCE_INLINE bool DoesAppHoldReference() const
{
return AppHoldsReference;
}
//--------------------------------------------------------------------------
// Shutdown
/// Are we shutdown?
TONK_FORCE_INLINE bool IsShutdown() const
{
return Shutdown;
}
TONK_FORCE_INLINE bool NotShutdown() const
{
return !IsShutdown();
}
/// Start shutdown process asynchronously.
/// This will latch in the first result provided, ignoring the rest.
/// Latching is synchronized with the ShutdownLock
void StartShutdown(unsigned reason, const Result& result);
/// These are not valid to read until Shutdown is true:
/// Reason for shutdown provided by StartShutdown()
unsigned ShutdownReason = Tonk_Success;
/// Jsonified data about the reason for the shutdown
std::string ShutdownJson;
//--------------------------------------------------------------------------
// Internal reference count
/// Increment or decrement the reference count
TONK_FORCE_INLINE void IncrementReferences()
{
TONK_DEBUG_ASSERT(RefCount >= 0);
++RefCount;
}
TONK_FORCE_INLINE void DecrementReferences()
{
TONK_DEBUG_ASSERT(RefCount > 0);
--RefCount;
TONK_DEBUG_ASSERT(RefCount >= 0);
}
protected:
/// Internal reference count
/// Holds one reference count that is released on StartShutdown()
std::atomic<int32_t> RefCount = ATOMIC_VAR_INIT(1);
/// IsShutdown() flag
std::atomic<bool> Shutdown = ATOMIC_VAR_INIT(false);
/// Does the application hold references on the object?
std::atomic<bool> AppHoldsReference = ATOMIC_VAR_INIT(true);
/// Synchronizes threads calling StartShutdown()
std::mutex ShutdownLock;
};
//------------------------------------------------------------------------------
// Debugging: Christmas Tree
/// This is a set of diagnostic information that can help track down problems.
/// Number of active objects of different sorts
extern std::atomic<int> ActiveConnectionCount;
extern std::atomic<int> ActiveUDPSocketCount;
extern std::atomic<int> ActiveSessionSocketCount;
//------------------------------------------------------------------------------
// Tonk API Helpers
/// Fill in config data to replace null function pointers with lambda stubs
void MakeCallbacksOptional(TonkConnectionConfig& config);
//------------------------------------------------------------------------------
// Portability Helpers
template<typename T, typename... Args>
inline std::unique_ptr<T> MakeUniqueNoThrow(Args&&... args) {
return std::unique_ptr<T>(new(std::nothrow) T(std::forward<Args>(args)...));
}
template<typename T, typename... Args>
inline std::shared_ptr<T> MakeSharedNoThrow(Args&&... args) {
return std::shared_ptr<T>(new(std::nothrow) T(std::forward<Args>(args)...));
}
} // namespace tonk
namespace std {
//------------------------------------------------------------------------------
// Android Portability
#if defined(ANDROID) && !defined(DEFINED_TO_STRING)
#define DEFINED_TO_STRING
#include <sstream>
/// Polyfill for to_string() on Android
template<typename T> string to_string(T value)
{
ostringstream os;
os << value;
return os.str();
}
#endif // DEFINED_TO_STRING
} // namespace std