C rewrite: phase out bitset

This commit is contained in:
Théophile Bastian 2020-07-22 16:17:38 +02:00
parent 8f5d3b9805
commit b012375427
2 changed files with 14 additions and 10 deletions

View file

@ -109,27 +109,32 @@ void PacketLossLogger::log_packet(uint32_t seqno) {
if(diff == 1) {
_cur_seqno++;
maybe_start_window();
while(_received_ahead.test((_cur_seqno + 1) % PACKET_LOST_AFTER)) {
while(_received_ahead & (1 << (_cur_seqno + 1 % PACKET_LOST_AFTER))) {
_cur_seqno++;
_received_ahead.reset(_cur_seqno % PACKET_LOST_AFTER);
_received_ahead &= ~(1 << (_cur_seqno + 1 % PACKET_LOST_AFTER));
maybe_start_window();
}
} else if(LIKELY(diff > 1)) {
if(diff < PACKET_LOST_AFTER)
_received_ahead.set(seqno % PACKET_LOST_AFTER);
_received_ahead |= (1 << (seqno % PACKET_LOST_AFTER));
else if(diff < PACKET_LOSS_WINDOW) {
// Packet too much forwards -- consider _cur_seqno lost
if(_cur_seqno % PACKET_LOSS_WINDOW > seqno % PACKET_LOSS_WINDOW) {
// This loss crosses a window border
for(int offs=0; offs < PACKET_LOST_AFTER; ++offs) {
maybe_start_window(offs);
if(!_received_ahead[(_cur_seqno + offs) % PACKET_LOST_AFTER])
if((_received_ahead & (1 << ((_cur_seqno + offs) % PACKET_LOST_AFTER))) == 0)
_tot_losses++;
}
} else {
_tot_losses += PACKET_LOST_AFTER - _received_ahead.count();
_tot_losses += PACKET_LOST_AFTER;
while(_received_ahead > 0) {
if(_received_ahead & 1)
_tot_losses--;
_received_ahead >>= 1;
}
}
_received_ahead.reset();
_received_ahead = 0;
_cur_seqno = seqno;
} else
reboot(); // This is a huge gap -- reboot
@ -141,7 +146,7 @@ void PacketLossLogger::log_packet(uint32_t seqno) {
}
void PacketLossLogger::reboot() {
_received_ahead.reset();
_received_ahead = 0;
// _tot_losses unchanged
}

View file

@ -3,7 +3,6 @@
/** A peer of a bound (server) instance of UdpVpn */
#include <netinet/in.h>
#include <bitset>
#include <time.h>
#include "util.hpp"
#include "VpnPacket.hpp"
@ -22,7 +21,7 @@ class PacketLossLogger {
/ (double)PACKET_LOSS_WINDOW;
}
const std::bitset<PACKET_LOST_AFTER> get_received_ahead() const {
uint8_t get_received_ahead() const {
return _received_ahead;
}
@ -35,7 +34,7 @@ class PacketLossLogger {
/// roll loss window values if `_cur_seqno + offs` is a window start.
void maybe_start_window(int offs=0);
std::bitset<PACKET_LOST_AFTER> _received_ahead;
uint8_t _received_ahead; // Warning: must have >= bits than PACKET_LOSS_AFTER
uint32_t _cur_seqno;
unsigned int _tot_losses;
unsigned int _last_window_losses, _win_start_losses;