1010
1111class SimpleMeshTables : public mesh ::MeshTables {
1212 uint8_t _hashes[MAX_PACKET_HASHES *MAX_HASH_SIZE ];
13- int _next_idx;
13+ uint32_t _last_seen[ MAX_PACKET_HASHES ]; // timestamp for LRU eviction
1414 uint32_t _direct_dups, _flood_dups;
1515
1616public:
17- SimpleMeshTables () {
17+ SimpleMeshTables () {
1818 memset (_hashes, 0 , sizeof (_hashes));
19- _next_idx = 0 ;
19+ memset (_last_seen, 0 , sizeof (_last_seen)) ;
2020 _direct_dups = _flood_dups = 0 ;
2121 }
2222
2323#ifdef ESP32
2424 void restoreFrom (File f) {
2525 f.read (_hashes, sizeof (_hashes));
26- f.read ((uint8_t *) &_next_idx, sizeof (_next_idx));
26+ int dummy_idx;
27+ f.read ((uint8_t *) &dummy_idx, sizeof (dummy_idx)); // legacy _next_idx, ignore
28+ // Treat restored hashes as just seen - give them fresh timestamps
29+ uint32_t now = millis ();
30+ const uint8_t * sp = _hashes;
31+ for (int i = 0 ; i < MAX_PACKET_HASHES ; i++, sp += MAX_HASH_SIZE ) {
32+ // Check if slot has data (not all zeros)
33+ bool empty = true ;
34+ for (int j = 0 ; j < MAX_HASH_SIZE && empty; j++) {
35+ if (sp[j] != 0 ) empty = false ;
36+ }
37+ _last_seen[i] = empty ? 0 : now;
38+ }
2739 }
2840 void saveTo (File f) {
2941 f.write (_hashes, sizeof (_hashes));
30- f.write ((const uint8_t *) &_next_idx, sizeof (_next_idx));
42+ int dummy_idx = 0 ;
43+ f.write ((const uint8_t *) &dummy_idx, sizeof (dummy_idx)); // legacy format
3144 }
3245#endif
3346
3447 bool wasSeen (const mesh::Packet* packet) override {
48+ uint32_t now = millis ();
3549 uint8_t hash[MAX_HASH_SIZE ];
3650 packet->calculatePacketHash (hash);
3751
3852 const uint8_t * sp = _hashes;
3953 for (int i = 0 ; i < MAX_PACKET_HASHES ; i++, sp += MAX_HASH_SIZE ) {
40- if (memcmp (hash, sp, MAX_HASH_SIZE ) == 0 ) {
54+ if (memcmp (hash, sp, MAX_HASH_SIZE ) == 0 && _last_seen[i] != 0 ) {
55+ // Match found - refresh timestamp (LRU touch) and return true
56+ _last_seen[i] = now;
4157 if (packet->isRouteDirect ()) {
4258 _direct_dups++;
4359 } else {
@@ -50,10 +66,24 @@ class SimpleMeshTables : public mesh::MeshTables {
5066 }
5167
5268 void markSeen (const mesh::Packet* packet) override {
69+ uint32_t now = millis ();
5370 uint8_t hash[MAX_HASH_SIZE ];
5471 packet->calculatePacketHash (hash);
55- memcpy (&_hashes[_next_idx * MAX_HASH_SIZE ], hash, MAX_HASH_SIZE );
56- _next_idx = (_next_idx + 1 ) % MAX_PACKET_HASHES ;
72+
73+ // Insert into oldest slot (LRU eviction). Empty slots have _last_seen == 0,
74+ // hence maximal age, so they get filled before any real entry is evicted.
75+ int oldest_idx = 0 ;
76+ uint32_t oldest_age = 0 ;
77+ const uint8_t * sp = _hashes;
78+ for (int i = 0 ; i < MAX_PACKET_HASHES ; i++, sp += MAX_HASH_SIZE ) {
79+ uint32_t age = now - _last_seen[i];
80+ if (age > oldest_age) {
81+ oldest_age = age;
82+ oldest_idx = i;
83+ }
84+ }
85+ memcpy (&_hashes[oldest_idx*MAX_HASH_SIZE ], hash, MAX_HASH_SIZE );
86+ _last_seen[oldest_idx] = now;
5787 }
5888
5989 void clear (const mesh::Packet* packet) override {
@@ -62,8 +92,9 @@ class SimpleMeshTables : public mesh::MeshTables {
6292
6393 uint8_t * sp = _hashes;
6494 for (int i = 0 ; i < MAX_PACKET_HASHES ; i++, sp += MAX_HASH_SIZE ) {
65- if (memcmp (hash, sp, MAX_HASH_SIZE ) == 0 ) {
95+ if (memcmp (hash, sp, MAX_HASH_SIZE ) == 0 ) {
6696 memset (sp, 0 , MAX_HASH_SIZE );
97+ _last_seen[i] = 0 ;
6798 break ;
6899 }
69100 }
0 commit comments