1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#ifndef ENGINE_SERVER_SNAP_ID_POOL_H
5#define ENGINE_SERVER_SNAP_ID_POOL_H
6
7#include <cstdint>
8#include <optional>
9
10class CSnapIdPool
11{
12 enum
13 {
14 MAX_IDS = 32 * 1024,
15 };
16
17 // State of a Snap ID
18 enum
19 {
20 ID_FREE = 0,
21 ID_ALLOCATED = 1,
22 ID_TIMED = 2,
23 };
24
25 class CID
26 {
27 public:
28 short m_Next;
29 short m_State; // 0 = free, 1 = allocated, 2 = timed
30 int64_t m_Timeout;
31 };
32
33 CID m_aIds[MAX_IDS];
34
35 int m_FirstFree;
36 int m_FirstTimed;
37 int m_LastTimed;
38 int m_Usage;
39 int m_InUsage;
40
41public:
42 CSnapIdPool();
43
44 void Reset();
45 void RemoveFirstTimeout();
46 std::optional<int> NewId();
47 void TimeoutIds();
48 void FreeId(int Id);
49};
50
51#endif
52