1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-03 18:17:45 +01:00
libtgvoip/controller/protocol/PacketManager.cpp

77 lines
1.8 KiB
C++
Raw Normal View History

2020-01-28 13:18:38 +01:00
#include "PacketManager.h"
2020-01-27 19:53:32 +01:00
#include "../../tools/logging.h"
2020-01-27 17:18:33 +01:00
using namespace tgvoip;
using namespace std;
2020-01-28 23:45:47 +01:00
PacketManager::PacketManager(uint8_t transportId) : transportId(transportId)
{
2020-01-29 15:52:43 +01:00
recentOutgoingPackets.reserve(MAX_RECENT_PACKETS);
2020-01-28 23:45:47 +01:00
}
2020-01-28 13:18:38 +01:00
void PacketManager::ackLocal(uint32_t ackId, uint32_t mask)
2020-01-27 17:18:33 +01:00
{
2020-01-28 21:45:56 +01:00
lastAckedSeq = ackId;
lastAckedSeqsMask = mask;
2020-01-27 17:18:33 +01:00
}
2020-01-28 13:18:38 +01:00
bool PacketManager::wasLocalAcked(uint32_t seq)
2020-01-27 17:18:33 +01:00
{
2020-01-28 21:45:56 +01:00
if (seq == lastAckedSeq)
return true;
if (seq > lastAckedSeq)
2020-01-27 17:18:33 +01:00
return false;
2020-01-28 21:45:56 +01:00
uint32_t distance = lastAckedSeq - seq;
if (distance > 0 && distance <= 32)
2020-01-27 17:18:33 +01:00
{
2020-01-28 21:45:56 +01:00
return (lastAckedSeqsMask >> (32 - distance)) & 1;
2020-01-27 17:18:33 +01:00
}
return false;
2020-01-27 19:53:32 +01:00
}
2020-01-28 13:18:38 +01:00
bool PacketManager::ackRemoteSeq(uint32_t ackId)
2020-01-27 19:53:32 +01:00
{
if (seqgt(ackId, lastRemoteSeq - MAX_RECENT_PACKETS))
{
2020-01-28 21:45:56 +01:00
if (ackId == lastRemoteSeq)
2020-01-27 19:53:32 +01:00
{
LOGW("Received duplicated packet for seq %u", ackId);
return false;
}
2020-01-28 21:45:56 +01:00
else if (ackId > lastRemoteSeq)
{
lastRemoteSeqsMask = (lastRemoteSeqsMask << 1) | 1;
lastRemoteSeqsMask <<= (ackId - lastRemoteSeq) - 1;
2020-01-27 19:53:32 +01:00
lastRemoteSeq = ackId;
2020-01-28 21:45:56 +01:00
}
else
{
uint32_t pos = 1 << ((lastRemoteSeq - ackId) - 1);
if (lastRemoteSeqsMask & pos)
{
LOGW("Received duplicated packet for seq %u", ackId);
return false;
}
lastRemoteSeqsMask |= pos;
}
2020-01-27 19:53:32 +01:00
}
else
{
LOGW("Packet %u is out of order and too late", ackId);
return false;
}
return true;
2020-01-29 15:52:43 +01:00
}
RecentOutgoingPacket *PacketManager::GetRecentOutgoingPacket(uint32_t seq)
{
for (RecentOutgoingPacket &opkt : recentOutgoingPackets)
{
if (opkt.seq == seq)
{
return &opkt;
}
}
return nullptr;
}