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

96 lines
2.9 KiB
C++
Raw Normal View History

2020-01-27 17:18:33 +01:00
#include "../PrivateDefines.cpp"
using namespace tgvoip;
using namespace std;
2020-03-24 12:15:04 +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-24 12:15:04 +01:00
ReliableOutgoingPacket pkt{
std::move(_pkt),
retryInterval,
timeout,
tries};
2020-01-27 17:18:33 +01:00
reliablePackets.push_back(move(pkt));
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-01-29 20:11:39 +01:00
#ifdef LOG_PACKETS
2020-03-24 12:15:04 +01:00
LOGD("Sending reliable queued packet, seq=%u, len=%lu", qp->seq, qp->data->Length());
2020-01-29 20:11:39 +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))
{
LOGV("Peer acknowledged extra type %u", it->data.print().c_str());
ProcessAcknowledgedOutgoingExtra(*it);
it = currentExtras.erase(it);
acked = true;
break;
}
}
if (!acked)
++it;
}
2020-01-27 17:18:33 +01:00
}