SimpleSeenTable.h 737 B

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. #include <Packet.h>
  3. #include <string.h>
  4. #define MAX_PACKET_HASHES 64
  5. class SimpleSeenTable {
  6. uint8_t _hashes[MAX_PACKET_HASHES*MAX_HASH_SIZE];
  7. int _next_idx;
  8. public:
  9. SimpleSeenTable() {
  10. memset(_hashes, 0, sizeof(_hashes));
  11. _next_idx = 0;
  12. }
  13. bool hasSeenPacket(const mesh::Packet* packet) {
  14. uint8_t hash[MAX_HASH_SIZE];
  15. packet->calculatePacketHash(hash);
  16. const uint8_t* sp = _hashes;
  17. for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) {
  18. if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) return true;
  19. }
  20. memcpy(&_hashes[_next_idx*MAX_HASH_SIZE], hash, MAX_HASH_SIZE);
  21. _next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; // cyclic table
  22. return false;
  23. }
  24. };