diff --git a/Flakkari/Engine/EntityComponentSystem/Entity.hpp b/Flakkari/Engine/EntityComponentSystem/Entity.hpp index 6ee6464e..f3babb1f 100644 --- a/Flakkari/Engine/EntityComponentSystem/Entity.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Entity.hpp @@ -43,10 +43,28 @@ class Entity { return *this; } + bool operator==(const Entity& other) const { + return _id == other._id; + } + + std::size_t getId() const { return _id; } + private: std::size_t _id; }; } // namespace Flakkari::Engine::ECS +#include + +namespace std { + template <> + struct hash + { + size_t operator()(const Flakkari::Engine::ECS::Entity& entity) const noexcept { + return std::hash()(entity.getId()); + } + }; +} + #endif /* !FLAKKARI_ENTITY_HPP_ */ diff --git a/Flakkari/Protocol/Header.hpp b/Flakkari/Protocol/Header.hpp index 20686eef..ed24e720 100644 --- a/Flakkari/Protocol/Header.hpp +++ b/Flakkari/Protocol/Header.hpp @@ -28,9 +28,9 @@ namespace Flakkari::Protocol { -using ushort = unsigned short; // 16 bits (max: 65535) -using uint = unsigned int; // 32 bits (max: 4294967295) -using ulong = unsigned long; // 64 bits (max: 18446744073709551615) +using ushort = uint16_t; // 16 bits (max: 65535) (2 bytes) +using uint = uint32_t; // 32 bits (max: 4294967295) (4 bytes) +using ulong = uint64_t; // 64 bits (max: 18446744073709551615) (8 bytes) /** * @brief The version of the protocol used @@ -78,14 +78,28 @@ inline namespace V_1 { LPL_PACKED_START +/** + * @brief Flakkari Header v1 (new header) + * + * 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |Priority|Api V.| CommandId | ContentLength | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | SequenceNumber | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ template struct Header { Priority _priority : 4 = Priority::LOW; ApiVersion _apiVersion : 4 = ApiVersion::V_1; Id _commandId; - ushort _contentLength = 0; - uint _sequenceNumber = static_cast( - std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) + uint16_t _contentLength = 0; + uint64_t _sequenceNumber = + static_cast(std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) .count()); + + std::size_t size() const { return sizeof(*this); } }; LPL_PACKED_END diff --git a/Flakkari/Protocol/Packet.hpp b/Flakkari/Protocol/Packet.hpp index 47908f98..49ac8af7 100644 --- a/Flakkari/Protocol/Packet.hpp +++ b/Flakkari/Protocol/Packet.hpp @@ -153,9 +153,9 @@ template struct Packet { */ [[nodiscard]] bool deserialize(const Network::Buffer &buffer) { - if (buffer.size() < sizeof(header)) + if (buffer.size() < header.size()) return FLAKKARI_LOG_WARNING("Buffer is too small to deserialize a packet."), false; - std::memcpy(&header, buffer.data(), sizeof(header)); + std::memcpy(&header, buffer.data(), header.size()); if (header._priority >= Priority::MAX_PRIORITY) return FLAKKARI_LOG_WARNING("Priority is too big (" + std::to_string((int) header._priority) + ")"), false; if (header._apiVersion >= ApiVersion::MAX_VERSION) @@ -164,9 +164,9 @@ template struct Packet { if (header._commandId >= CommandId::MAX_COMMAND_ID) return FLAKKARI_LOG_WARNING("CommandId is too big (" + std::to_string((int) header._commandId) + ")"), false; - if (header._contentLength > buffer.size() - sizeof(header)) + if (header._contentLength > buffer.size() - header.size()) return false; - payload = buffer.extractData(sizeof(header), header._contentLength); + payload = buffer.extractData(header.size(), header._contentLength); return true; } };