TeaSpeakLibrary/src/protocol/AcknowledgeManager.cpp

147 lines
5.1 KiB
C++
Raw Normal View History

2019-06-26 22:11:22 +02:00
#include "AcknowledgeManager.h"
2020-02-18 11:53:32 +01:00
#include <cmath>
2019-06-26 22:11:22 +02:00
#include <misc/endianness.h>
using namespace ts;
using namespace ts::connection;
using namespace ts::protocol;
using namespace std;
using namespace std::chrono;
2020-02-18 11:53:32 +01:00
AcknowledgeManager::AcknowledgeManager() = default;
2019-06-26 22:11:22 +02:00
AcknowledgeManager::~AcknowledgeManager() {
2020-02-18 11:53:32 +01:00
this->reset();
2019-06-26 22:11:22 +02:00
}
void AcknowledgeManager::reset() {
2020-01-24 02:49:59 +01:00
{
2020-02-18 11:53:32 +01:00
std::unique_lock lock{this->entry_lock};
auto pending_entries = std::move(this->entries);
lock.unlock();
/* save because entries are not accessable anymore */
for(const auto& entry : pending_entries)
2020-01-24 02:49:59 +01:00
if(entry->acknowledge_listener)
entry->acknowledge_listener->executionFailed("reset");
}
2019-06-26 22:11:22 +02:00
}
size_t AcknowledgeManager::awaiting_acknowledge() {
2020-02-18 11:53:32 +01:00
std::lock_guard lock(this->entry_lock);
2020-01-24 02:49:59 +01:00
return this->entries.size();
2019-06-26 22:11:22 +02:00
}
void AcknowledgeManager::process_packet(ts::protocol::BasicPacket &packet) {
2020-01-24 02:49:59 +01:00
if(!packet.type().requireAcknowledge()) return;
2019-06-26 22:11:22 +02:00
2020-01-24 02:49:59 +01:00
auto entry = make_shared<Entry>();
entry->acknowledge_listener = std::move(packet.getListener());
2019-06-26 22:11:22 +02:00
2020-01-24 02:49:59 +01:00
entry->buffer = packet.buffer();
2019-06-26 22:11:22 +02:00
2020-01-24 02:49:59 +01:00
entry->resend_count = 0;
entry->resend_period = milliseconds((int) ceil(this->average_response * 3/2));
entry->first_send = system_clock::now();
entry->next_resend = entry->first_send + entry->resend_period;
2019-06-26 22:11:22 +02:00
2020-01-24 02:49:59 +01:00
entry->packet_type = packet.type().type();
entry->packet_id = packet.packetId();
2019-06-26 22:11:22 +02:00
2020-01-24 02:49:59 +01:00
entry->acknowledged = false;
entry->send_count = 1;
{
2020-02-18 11:53:32 +01:00
std::lock_guard lock(this->entry_lock);
2020-01-24 02:49:59 +01:00
this->entries.push_front(std::move(entry));
}
2019-06-26 22:11:22 +02:00
}
2020-01-27 02:21:39 +01:00
bool AcknowledgeManager::process_acknowledge(uint8_t packet_type, const pipes::buffer_view& payload, std::string& error) {
if(payload.length() < 2) return false;
2020-02-18 11:53:32 +01:00
PacketType target_type{packet_type == protocol::ACK_LOW ? PacketType::COMMAND_LOW : PacketType::COMMAND};
uint16_t target_id{be2le16((char*) payload.data_ptr())};
2020-01-24 02:49:59 +01:00
std::shared_ptr<Entry> entry;
2020-02-18 11:53:32 +01:00
std::unique_ptr<threads::Future<bool>> ack_listener;
2020-01-24 02:49:59 +01:00
{
2020-02-18 11:53:32 +01:00
std::lock_guard lock{this->entry_lock};
2020-01-24 02:49:59 +01:00
for(auto it = this->entries.begin(); it != this->entries.end(); it++) {
if((*it)->packet_type == target_type && (*it)->packet_id == target_id) {
entry = *it;
2020-02-18 11:53:32 +01:00
ack_listener = std::move(entry->acknowledge_listener); /* move it out so nobody else could call it as well */
2020-01-24 02:49:59 +01:00
entry->send_count--;
if(entry->send_count == 0)
this->entries.erase(it);
break;
}
}
}
if(!entry) {
error = "Missing packet id (" + to_string(target_id) + ")";
return false;
}
auto time = system_clock::now() - entry->next_resend + entry->resend_period;
auto ms_time = duration_cast<milliseconds>(time).count();
if(ms_time > 5) {
this->average_response = this->average_response * .80 + ms_time * .2;
}
entry->acknowledged = true;
2020-02-18 11:53:32 +01:00
if(ack_listener) ack_listener->executionSucceed(true);
2020-01-24 02:49:59 +01:00
return true;
2019-06-26 22:11:22 +02:00
}
ssize_t AcknowledgeManager::execute_resend(const system_clock::time_point& now , std::chrono::system_clock::time_point &next_resend,std::deque<pipes::buffer>& buffers, string& error) {
2020-02-18 11:53:32 +01:00
size_t resend_count{0};
2020-01-24 02:49:59 +01:00
2020-02-18 11:53:32 +01:00
vector<shared_ptr<Entry>> need_resend;
2020-01-24 02:49:59 +01:00
{
2020-02-18 11:53:32 +01:00
bool cleanup{false};
std::lock_guard lock{this->entry_lock};
need_resend.resize(this->entries.size());
2020-01-24 02:49:59 +01:00
for (auto &entry : this->entries) {
if(entry->acknowledged) {
2020-02-18 11:53:32 +01:00
if(entry->next_resend + entry->resend_period <= now) { // Some resends are lost. So we just drop it after time
entry.reset();
cleanup = true;
2020-01-24 02:49:59 +01:00
}
} else {
2020-02-18 11:53:32 +01:00
if(entry->next_resend <= now) {
entry->resend_period = entry->resend_period + milliseconds{(int) ceil(this->average_response * 2)};
if(entry->resend_period.count() > 1000)
entry->resend_period = milliseconds(1000);
else if(entry->resend_period.count() < 25)
entry->resend_period = milliseconds(25);
entry->next_resend = now + entry->resend_period;
need_resend.push_back(entry);
}
2020-01-24 02:49:59 +01:00
if(next_resend > entry->next_resend)
next_resend = entry->next_resend;
}
}
2020-02-18 11:53:32 +01:00
if(cleanup) {
this->entries.erase(std::remove_if(this->entries.begin(), this->entries.end(),
[](const auto& entry) { return !entry; }), this->entries.end());
2020-01-24 02:49:59 +01:00
}
}
for(const auto& packet : need_resend) {
if(packet->resend_count > 15 && packet->first_send + seconds(15) < now) { //FIXME configurable
error = "Failed to receive acknowledge for packet " + to_string(packet->packet_id) + " of type " + PacketTypeInfo::fromid(packet->packet_type).name();
return -1;
}
resend_count++;
packet->resend_count++;
packet->send_count++;
buffers.push_back(packet->buffer);
}
return resend_count;
2019-06-26 22:11:22 +02:00
}