1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-02 17:51:06 +01:00
libtgvoip/controller/protocol/packets/PacketManager.cpp

98 lines
2.5 KiB
C++
Raw Normal View History

2020-01-28 13:18:38 +01:00
#include "PacketManager.h"
2020-03-25 15:49:13 +01:00
#include "PacketStructs.h"
2020-03-21 21:33:51 +01:00
#include "../../../tools/logging.h"
2020-03-24 17:46:50 +01:00
#include "../../../VoIPController.h"
2020-01-27 17:18:33 +01:00
using namespace tgvoip;
2020-03-25 15:49:13 +01:00
2020-01-27 17:18:33 +01:00
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-03-24 15:07:56 +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-03-24 15:07:56 +01:00
bool PacketManager::wasLocalAcked(uint32_t seq) const
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-03-24 17:10:40 +01:00
bool PacketManager::ackRemoteSeq(const Packet &pkt)
{
if (pkt.legacy) {
if (pkt.legacySeq) {
return ackRemoteSeq(pkt.legacySeq);
}
} else {
return ackRemoteSeq(pkt.seq);
}
return true;
}
bool PacketManager::ackRemoteSeq(const 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
{
2020-03-26 20:37:25 +01:00
//LOGW("Received duplicated packet for seq %u, streamId=%hhu", ackId, transportId);
2020-01-27 19:53:32 +01:00
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)
{
2020-03-24 17:10:40 +01:00
LOGW("Received duplicated packet for seq %u, streamId=%hhu", ackId, transportId);
2020-01-28 21:45:56 +01:00
return false;
}
lastRemoteSeqsMask |= pos;
}
2020-01-27 19:53:32 +01:00
}
else
{
2020-03-24 17:10:40 +01:00
LOGW("Packet %u, streamId=%hhu is out of order and too late", ackId, transportId);
2020-01-27 19:53:32 +01:00
return false;
}
return true;
2020-01-29 15:52:43 +01:00
}
2020-03-24 15:07:56 +01:00
std::vector<RecentOutgoingPacket> &PacketManager::getRecentOutgoingPackets()
{
return recentOutgoingPackets;
}
void PacketManager::addRecentOutgoingPacket(const PendingOutgoingPacket &pkt)
2020-01-29 15:52:43 +01:00
{
2020-03-25 15:49:13 +01:00
addRecentOutgoingPacket(RecentOutgoingPacket(pkt, VoIPController::GetCurrentTime()));
2020-03-24 15:07:56 +01:00
}
void PacketManager::addRecentOutgoingPacket(RecentOutgoingPacket &&pkt)
{
recentOutgoingPackets.push_back(std::move(pkt));
while (recentOutgoingPackets.size() > MAX_RECENT_PACKETS)
2020-01-29 15:52:43 +01:00
{
2020-03-24 15:07:56 +01:00
recentOutgoingPackets.erase(recentOutgoingPackets.begin());
2020-01-29 15:52:43 +01:00
}
2020-03-24 15:07:56 +01:00
lastSentSeq = pkt.pkt.seq;
2020-01-29 15:52:43 +01:00
}