| 1 | #ifndef _IDAllocator_h_ |
| 2 | #define _IDAllocator_h_ |
| 3 | |
| 4 | |
| 5 | #include <random> |
| 6 | #include <unordered_map> |
| 7 | #include <vector> |
| 8 | #include "../util/Logger.h" |
| 9 | |
| 10 | class Universe; |
| 11 | |
| 12 | /** The IDAllocator coordinates the allocation of new IDs between the server |
| 13 | and a number of empires, id consumers. |
| 14 | |
| 15 | IDs are ints. The id consumers have disjoint id spaces. All consumers have |
| 16 | a common m_stride and a unique initial offset modulo m_stride. Each id |
| 17 | consumer only receives new ids from the set number of numbers x, where |
| 18 | (x % m_stride == consumer's initial offset). |
| 19 | |
| 20 | The IDAllocator is associated with a single empire id. The constructor |
| 21 | creates the server association. Once serialized for another empire_id then |
| 22 | it is associated with that new (lesser) empire id, and will no longer have |
| 23 | the complete table. |
| 24 | |
| 25 | TODO: Replace IDAllocator with using UUID as the id type ID_t instead of int. This does not |
| 26 | require coordination between clients and servers and does not leak any information to clients |
| 27 | about other client's allocations. |
| 28 | */ |
| 29 | |
| 30 | class IDAllocator { |
| 31 | public: |
| 32 | using ID_t = int; |
| 33 | |
| 34 | /** \p client_ids are all the player ids in the game. \p highest_pre_allocated_id is the used for |
| 35 | legacy loads to offset the newly allocated id to after the ones from the save game. |
| 36 | */ |
| 37 | IDAllocator(const int server_id, |
| 38 | const std::vector<int>& client_ids, |
| 39 | const ID_t invalid_id, |
| 40 | const ID_t temp_id, |
| 41 | const ID_t highest_pre_allocated_id); |
| 42 | |
| 43 | IDAllocator& operator=(IDAllocator&& other) noexcept = default; |
| 44 | |
| 45 | /// Return a valid new id. This is used by both clients and servers. |
| 46 | ID_t NewID(const Universe& universe); |
| 47 | |
| 48 | /** Return {hard_success, soft_success} where \p hard_success determines if |
| 49 | \p id is unused and valid and \p soft_success determines if \p id is in |
| 50 | the id space of \p empire_id. This allows errors that are probably due |
| 51 | to legacy loading and order processing to be ignored for now. |
| 52 | */ |
| 53 | std::pair<bool, bool> IsIDValidAndUnused(const ID_t id, const int empire_id); |
| 54 | |
| 55 | /** UpdateIDAndCheckIfOwned behaves differently on the server and clients. |
| 56 | |
| 57 | On the server, it determines which client allocated \p id and updates |
| 58 | the next allocated id of that client. It then returns true. This means |
| 59 | that the server has a master list of the maximum id used by each client |
| 60 | and can use all ids as valid ids when executing orders. |
| 61 | |
| 62 | On the client it returns true iff this client allocated \p id and does |
| 63 | nothing else. That means that id modulo m_stride == client's offset.*/ |
| 64 | bool UpdateIDAndCheckIfOwned(const ID_t id); |
| 65 | |
| 66 | /** ObfuscateBeforeSerialization randomizes which client is using which modulus each turn |
| 67 | before IDAllocator is serialized and sent to the clients. */ |
| 68 | void ObfuscateBeforeSerialization(); |
| 69 | |
| 70 | /** Serialize while stripping out information not known to \p empire_id. */ |
| 71 | template <typename Archive> |
| 72 | void SerializeForEmpire(Archive& ar, const unsigned int version, int empire_id); |
| 73 | |
| 74 | private: |
| 75 | /** Return the empire that should have assigned \p id. */ |
| 76 | ID_t& AssigningEmpireForID(ID_t id); |
| 77 | |
| 78 | /// Increment the next assigned id for an empire until it is past checked_id. |
| 79 | void IncrementNextAssignedId(const int assigning_empire, const int checked_id); |
| 80 | |
| 81 | /// Return a string representing the state. |
| 82 | std::string StateString() const; |
| 83 | |
| 84 | ID_t m_invalid_id; |
| 85 | ID_t m_temp_id; |
| 86 | |
| 87 | /// m_stride is used to partition the id space into modulo m_stride sections. |
| 88 | ID_t m_stride; |
| 89 | /// The zero point or first allocated id. |
| 90 | ID_t m_zero; |
| 91 | |
| 92 | // The server id and the empire id are equal on construction. Serialization |
| 93 | // and deserialization may downgrade empire id to one of the lesser (not |
| 94 | // server) ids. |
| 95 | int m_server_id; |
| 96 | int m_empire_id; |
| 97 | |
| 98 | // A map from empire id to next used object id; |
| 99 | std::unordered_map<int, ID_t> m_empire_id_to_next_assigned_object_id; |
| 100 | |
| 101 | // An index from id % m_stride to empire ids |
| 102 | std::vector<int> m_offset_to_empire_id; |
| 103 | |
| 104 | /// if less than m_next_assigned_id warn about id exhaustion. |
| 105 | ID_t m_warn_threshold; |
| 106 | /// Stop assigning ids and start generating errors. |
| 107 | ID_t m_exhausted_threshold; |
| 108 | |
| 109 | /// Random number generator |
| 110 | std::mt19937 m_random_generator; |
| 111 | }; |
| 112 | |
| 113 | |
| 114 | #endif |