From 8dde5b1c23f0d84ca1b56a8c80389fb4e887b062 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sun, 21 Mar 2021 23:51:36 +0100 Subject: [PATCH] SOme updates --- src/misc/advanced_mutex.h | 22 ++++++++++------ src/protocol/AcknowledgeManager.cpp | 39 +++++++++++++---------------- src/protocol/AcknowledgeManager.h | 4 +-- src/protocol/PacketDecoder.h | 2 +- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/misc/advanced_mutex.h b/src/misc/advanced_mutex.h index 40a1c68..f1c77d5 100644 --- a/src/misc/advanced_mutex.h +++ b/src/misc/advanced_mutex.h @@ -279,25 +279,33 @@ namespace std { }; */ + /** + * Test if a `std::shared_mutex` is unique locked by trying to lock it in shared mode. + * @tparam T + * @param mutex + * @return + */ template inline bool mutex_locked(T& mutex) { + /* std::shared_mutex can be recursively unique locked?? */ return true; try { - unique_lock lock_try(mutex, try_to_lock); /* should throw EDEADLK */ + std::unique_lock lock_try(mutex, try_to_lock); /* should throw EDEADLK */ return false; } catch(const std::system_error& ex) { return ex.code() == errc::resource_deadlock_would_occur; } } + /** + * Test if a `std::shared_mutex` is shared (or unique) locked by try locking it in unique mode + * @tparam T + * @param mutex + * @return + */ template inline bool mutex_shared_locked(T& mutex) { return true; - try { - shared_lock lock_try(mutex, try_to_lock); /* should throw EDEADLK */ - return false; - } catch(const std::system_error& ex) { - return ex.code() == errc::resource_deadlock_would_occur; - } + //return mutex_locked(mutex); } } \ No newline at end of file diff --git a/src/protocol/AcknowledgeManager.cpp b/src/protocol/AcknowledgeManager.cpp index 69e1afb..c0190af 100644 --- a/src/protocol/AcknowledgeManager.cpp +++ b/src/protocol/AcknowledgeManager.cpp @@ -95,46 +95,41 @@ bool AcknowledgeManager::process_acknowledge(uint8_t packet_type, uint16_t targe } void AcknowledgeManager::execute_resend(const system_clock::time_point& now , std::chrono::system_clock::time_point &next_resend,std::deque>& buffers) { - vector> resend_failed; - { - bool cleanup{false}; - std::lock_guard lock{this->entry_lock}; - resend_failed.reserve(this->entries.size()); + std::deque> resend_failed; - for (auto &entry : this->entries) { - if (entry->acknowledged) { + { + std::lock_guard lock{this->entry_lock}; + + this->entries.erase(std::remove_if(this->entries.begin(), this->entries.end(), [&](std::shared_ptr& entry) { + if(entry->acknowledged) { if (entry->next_resend + std::chrono::milliseconds{(int64_t) ceil(this->rto * 4)} <= now) { /* Some resends are lost. So we just drop it after time */ - entry.reset(); - cleanup = true; + return true; } } else { if (entry->next_resend <= now) { if (entry->resend_count > 15 && entry->first_send + seconds(15) < now) { - resend_failed.push_back(std::move(entry)); /* transfer the ownership */ - cleanup = true; - continue; + /* packet resend seems to have failed */ + resend_failed.push_back(std::move(entry)); + return true; } else { - entry->next_resend = - now + std::chrono::milliseconds{(int64_t) std::min(ceil(this->rto), 1500.f)}; + entry->next_resend = now + std::chrono::milliseconds{(int64_t) std::min(ceil(this->rto), 1500.f)}; buffers.push_back(entry); //entry->resend_count++; /* this MUST be incremented by the result handler (resend may fails) */ entry->send_count++; } } - if (next_resend > entry->next_resend) + if (next_resend > entry->next_resend) { next_resend = entry->next_resend; + } } - } - - if (cleanup) { - this->entries.erase(std::remove_if(this->entries.begin(), this->entries.end(), - [](const auto &entry) { return !entry; }), this->entries.end()); - } + return false; + }), this->entries.end()); } - for(const auto& failed : resend_failed) + for(const auto& failed : resend_failed) { this->callback_resend_failed(this->callback_data, failed); + } } /* we're not taking the clock granularity into account because its nearly 1ms and it would only add more branches */ diff --git a/src/protocol/AcknowledgeManager.h b/src/protocol/AcknowledgeManager.h index 93cb4ad..68cd70e 100644 --- a/src/protocol/AcknowledgeManager.h +++ b/src/protocol/AcknowledgeManager.h @@ -8,8 +8,6 @@ #define DEBUG_ACKNOWLEDGE namespace ts::connection { - class VoiceClientConnection; - class AcknowledgeManager { public: struct Entry { @@ -32,7 +30,7 @@ namespace ts::connection { AcknowledgeManager(); virtual ~AcknowledgeManager(); - size_t awaiting_acknowledge(); + [[nodiscard]] size_t awaiting_acknowledge(); void reset(); void process_packet(uint8_t /* packet type */, uint32_t /* full packet id */, void* /* packet ptr */, std::unique_ptr> /* ack listener */); diff --git a/src/protocol/PacketDecoder.h b/src/protocol/PacketDecoder.h index b924640..10d5060 100644 --- a/src/protocol/PacketDecoder.h +++ b/src/protocol/PacketDecoder.h @@ -54,7 +54,7 @@ namespace ts::protocol { public: /* direct function calls are better optimized out */ typedef void(*callback_decoded_packet_t)(void* /* cb argument */, const protocol::PacketParser&); - typedef void(*callback_decoded_command_t)(void* /* cb argument */, ReassembledCommand*& /* command */); /* must move the command, else it gets freed*/ + typedef void(*callback_decoded_command_t)(void* /* cb argument */, ReassembledCommand*& /* command */); /* must move the command, else it gets freed */ typedef void(*callback_send_acknowledge_t)(void* /* cb argument */, uint16_t /* packet id */, bool /* is command low */); explicit PacketDecoder(connection::CryptHandler* /* crypt handler */, bool /* is server */);