diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 64db28ce..b2d87696 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -144,7 +144,7 @@ protected: return airtime_factor; } - bool allowPacketForward(mesh::Packet* packet) override { + bool allowPacketForward(const mesh::Packet* packet) override { uint8_t hash[MAX_HASH_SIZE]; packet->calculatePacketHash(hash); diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 5dbcbecc..7ff55e9f 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -11,9 +11,14 @@ void Mesh::loop() { Dispatcher::loop(); } -bool Mesh::allowPacketForward(mesh::Packet* packet) { +bool Mesh::allowPacketForward(const mesh::Packet* packet) { return false; // by default, Transport NOT enabled } +uint32_t Mesh::getRetransmitDelay(const mesh::Packet* packet) { + uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * 52 / 50) / 2; + + return _rng->nextInt(0, 5)*t; +} int Mesh::searchPeersByHash(const uint8_t* hash) { return 0; // not found @@ -34,7 +39,7 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { // remove our hash from 'path', then re-broadcast pkt->path_len -= PATH_HASH_SIZE; memcpy(pkt->path, &pkt->path[PATH_HASH_SIZE], pkt->path_len); - return ACTION_RETRANSMIT(0); // Routed traffic is HIGHEST priority + return ACTION_RETRANSMIT(0); // Routed traffic is HIGHEST priority (and NO per-hop delay) } return ACTION_RELEASE; // this node is NOT the next hop (OR this packet has already been forwarded), so discard. } @@ -204,8 +209,9 @@ DispatcherAction Mesh::routeRecvPacket(Packet* packet) { // append this node's hash to 'path' packet->path_len += self_id.copyHashTo(&packet->path[packet->path_len]); + uint32_t d = getRetransmitDelay(packet); // as this propagates outwards, give it lower and lower priority - return ACTION_RETRANSMIT(packet->path_len); // give priority to closer sources, than ones further away + return ACTION_RETRANSMIT_DELAYED(packet->path_len, d); // give priority to closer sources, than ones further away } return ACTION_RELEASE; } diff --git a/src/Mesh.h b/src/Mesh.h index 2b7a449d..746179d5 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -46,7 +46,12 @@ protected: * \brief Check whether this packet should be forwarded (re-transmitted) or not. * Is sub-classes responsibility to make sure given packet is only transmitted ONCE (by this node) */ - virtual bool allowPacketForward(Packet* packet); + virtual bool allowPacketForward(const Packet* packet); + + /** + * \returns number of milliseconds delay to apply to retransmitting the given packet. + */ + virtual uint32_t getRetransmitDelay(const Packet* packet); /** * \brief Perform search of local DB of peers/contacts. diff --git a/src/Utils.h b/src/Utils.h index 366797ff..9d662f6f 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -9,6 +9,10 @@ namespace mesh { class RNG { public: virtual void random(uint8_t* dest, size_t sz) = 0; + + /** + * \returns random number between _min (inclusive) and _max (exclusive) + */ uint32_t nextInt(uint32_t _min, uint32_t _max); };