Skip to content

Commit

Permalink
feat: implement equality operator and hash function for Entity, updat…
Browse files Browse the repository at this point in the history
…e type definitions and header structure for improved clarity and consistency
  • Loading branch information
MasterLaplace committed Nov 22, 2024
1 parent 498a279 commit c90df9f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
18 changes: 18 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unordered_map>

namespace std {
template <>
struct hash<Flakkari::Engine::ECS::Entity>
{
size_t operator()(const Flakkari::Engine::ECS::Entity& entity) const noexcept {
return std::hash<std::size_t>()(entity.getId());
}
};
}

#endif /* !FLAKKARI_ENTITY_HPP_ */
26 changes: 20 additions & 6 deletions Flakkari/Protocol/Header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <typename Id> struct Header {
Priority _priority : 4 = Priority::LOW;
ApiVersion _apiVersion : 4 = ApiVersion::V_1;
Id _commandId;
ushort _contentLength = 0;
uint _sequenceNumber = static_cast<uint>(
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
uint16_t _contentLength = 0;
uint64_t _sequenceNumber =
static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
.count());

std::size_t size() const { return sizeof(*this); }
};

LPL_PACKED_END
Expand Down
8 changes: 4 additions & 4 deletions Flakkari/Protocol/Packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ template <typename Id> 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)
Expand All @@ -164,9 +164,9 @@ template <typename Id> 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;
}
};
Expand Down

0 comments on commit c90df9f

Please sign in to comment.