1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-12 09:09:38 +01:00
libtgvoip/controller/protocol/Reliable.cpp

93 lines
3.0 KiB
C++
Raw Normal View History

2020-03-25 15:49:13 +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-03-25 21:38:07 +01:00
void VoIPController::SendPacketReliably(PendingOutgoingPacket &pkt, double retryInterval, double timeout, uint8_t tries)
2020-01-27 17:18:33 +01:00
{
ENFORCE_MSG_THREAD;
2020-01-29 19:12:12 +01:00
#ifdef LOG_PACKETS
2020-01-27 17:18:33 +01:00
LOGV("Send reliably, type=%u, len=%u, retry=%.3f, timeout=%.3f, tries=%hhu", type, unsigned(len), retryInterval, timeout, tries);
2020-01-29 19:12:12 +01:00
#endif
2020-03-25 21:38:07 +01:00
reliablePackets.push_back(ReliableOutgoingPacket{std::move(pkt),
retryInterval,
timeout,
tries});
2020-01-27 17:18:33 +01:00
messageThread.Post(std::bind(&VoIPController::UpdateReliablePackets, this));
if (timeout > 0.0)
{
messageThread.Post(std::bind(&VoIPController::UpdateReliablePackets, this), timeout);
}
}
void VoIPController::UpdateReliablePackets()
{
vector<PendingOutgoingPacket> packetsToSend;
2020-03-24 12:15:04 +01:00
for (auto qp = reliablePackets.begin(); qp != reliablePackets.end();)
2020-01-27 17:18:33 +01:00
{
if (qp->timeout > 0 && qp->firstSentTime > 0 && GetCurrentTime() - qp->firstSentTime >= qp->timeout)
{
2020-01-29 20:11:39 +01:00
#ifdef LOG_PACKETS
2020-03-22 21:25:02 +01:00
LOGD("Removing reliable queued packet because of timeout");
2020-01-29 20:11:39 +01:00
#endif
2020-01-27 17:18:33 +01:00
qp = reliablePackets.erase(qp);
continue;
}
if (!qp->tries--)
{
2020-01-29 20:11:39 +01:00
#ifdef LOG_PACKETS
2020-03-22 21:25:02 +01:00
LOGD("Removing reliable queued packet because of no more tries");
2020-01-29 20:11:39 +01:00
#endif
2020-01-27 17:18:33 +01:00
qp = reliablePackets.erase(qp);
continue;
}
if (GetCurrentTime() - qp->lastSentTime >= qp->retryInterval)
{
messageThread.Post(std::bind(&VoIPController::UpdateReliablePackets, this), qp->retryInterval);
qp->lastSentTime = GetCurrentTime();
2020-03-26 20:37:25 +01:00
#ifdef LOG_PACKETS
2020-03-26 17:42:34 +01:00
LOGD("Sending reliable queued packet, seq=%u, len=%lu", qp->pkt.pktInfo.seq, qp->pkt.packet->Length());
2020-03-26 20:37:25 +01:00
#endif
2020-01-27 17:18:33 +01:00
if (qp->firstSentTime == 0)
qp->firstSentTime = qp->lastSentTime;
2020-03-24 12:15:04 +01:00
packetsToSend.push_back(qp->pkt);
2020-01-27 17:18:33 +01:00
}
++qp;
}
2020-03-24 12:15:04 +01:00
for (auto &pkt : packetsToSend)
2020-01-27 17:18:33 +01:00
{
2020-03-24 12:15:04 +01:00
SendOrEnqueuePacket(pkt);
2020-01-27 17:18:33 +01:00
}
}
2020-03-24 12:15:04 +01:00
void VoIPController::HandleReliablePackets(const PacketManager &pm)
2020-01-27 17:18:33 +01:00
{
for (auto it = reliablePackets.begin(); it != reliablePackets.end();)
{
2020-03-24 12:15:04 +01:00
if (pm.wasLocalAcked(it->pkt.pktInfo.seq))
2020-01-27 17:18:33 +01:00
{
2020-03-24 12:15:04 +01:00
LOGV("Acked queued packet with %hhu tries left", it->tries);
2020-01-27 17:18:33 +01:00
it = reliablePackets.erase(it);
continue;
}
++it;
}
2020-03-24 17:10:40 +01:00
for (auto it = currentExtras.begin(); it != currentExtras.end();)
{
bool acked = false;
for (const auto &seq : it->seqs)
{
if (seq && pm.wasLocalAcked(seq))
{
2020-03-25 12:37:40 +01:00
LOGV("Peer acknowledged extra type %s", it->data.print().c_str());
2020-03-24 17:10:40 +01:00
ProcessAcknowledgedOutgoingExtra(*it);
it = currentExtras.erase(it);
acked = true;
break;
}
}
if (!acked)
++it;
}
2020-01-27 17:18:33 +01:00
}