2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstddef> // size_t
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2021-11-19 18:37:38 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-06-25 17:33:32 +00:00
|
|
|
#include <small/map.hpp>
|
|
|
|
|
2021-11-19 18:37:38 +00:00
|
|
|
#define LIBTRANSMISSION_PEER_MODULE
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/peer-mgr-active-requests.h"
|
2023-06-25 17:33:32 +00:00
|
|
|
#include "libtransmission/peer-mgr-wishlist.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/tr-assert.h"
|
2021-11-19 18:37:38 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
struct tr_peer;
|
2023-07-08 15:24:03 +00:00
|
|
|
|
2021-11-19 18:37:38 +00:00
|
|
|
class ActiveRequests::Impl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count(tr_peer const* peer) const
|
|
|
|
{
|
|
|
|
auto const it = count_.find(peer);
|
|
|
|
return it != std::end(count_) ? it->second : size_t{};
|
|
|
|
}
|
|
|
|
|
|
|
|
void incCount(tr_peer const* peer)
|
|
|
|
{
|
|
|
|
++count_[peer];
|
|
|
|
++size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void decCount(tr_peer const* peer)
|
|
|
|
{
|
|
|
|
auto it = count_.find(peer);
|
|
|
|
TR_ASSERT(it != std::end(count_));
|
|
|
|
TR_ASSERT(it->second > 0);
|
|
|
|
TR_ASSERT(size_ > 0);
|
|
|
|
|
|
|
|
if (it != std::end(count_))
|
|
|
|
{
|
|
|
|
if (--it->second == 0)
|
|
|
|
{
|
|
|
|
count_.erase(it);
|
|
|
|
}
|
|
|
|
--size_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<tr_peer const*, size_t> count_;
|
|
|
|
|
2023-06-25 17:33:32 +00:00
|
|
|
std::unordered_map<tr_block_index_t, small::map<tr_peer const*, time_t, Wishlist::EndgameMaxPeers>> blocks_;
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
size_t size_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
ActiveRequests::ActiveRequests()
|
|
|
|
: impl_{ std::make_unique<Impl>() }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ActiveRequests::~ActiveRequests() = default;
|
|
|
|
|
|
|
|
bool ActiveRequests::add(tr_block_index_t block, tr_peer* peer, time_t when)
|
|
|
|
{
|
|
|
|
bool const added = impl_->blocks_[block].emplace(peer, when).second;
|
|
|
|
|
|
|
|
if (added)
|
|
|
|
{
|
|
|
|
impl_->incCount(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove a request to `peer` for `block`
|
|
|
|
bool ActiveRequests::remove(tr_block_index_t block, tr_peer const* peer)
|
|
|
|
{
|
|
|
|
auto const it = impl_->blocks_.find(block);
|
2023-06-25 17:33:32 +00:00
|
|
|
auto const removed = it != std::end(impl_->blocks_) && it->second.erase(peer) != 0;
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
if (removed)
|
|
|
|
{
|
|
|
|
impl_->decCount(peer);
|
|
|
|
|
|
|
|
if (std::empty(it->second))
|
|
|
|
{
|
|
|
|
impl_->blocks_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove requests to `peer` and return the associated blocks
|
|
|
|
std::vector<tr_block_index_t> ActiveRequests::remove(tr_peer const* peer)
|
|
|
|
{
|
|
|
|
auto removed = std::vector<tr_block_index_t>{};
|
|
|
|
removed.reserve(impl_->blocks_.size());
|
|
|
|
|
2021-11-25 22:39:19 +00:00
|
|
|
for (auto const& [block, peers_at] : impl_->blocks_)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2023-06-25 17:33:32 +00:00
|
|
|
if (peers_at.count(peer) != 0U)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2021-11-25 22:39:19 +00:00
|
|
|
removed.push_back(block);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto block : removed)
|
|
|
|
{
|
|
|
|
remove(block, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove requests for `block` and return the associated peers
|
|
|
|
std::vector<tr_peer*> ActiveRequests::remove(tr_block_index_t block)
|
|
|
|
{
|
|
|
|
auto removed = std::vector<tr_peer*>{};
|
|
|
|
|
2021-11-25 22:39:19 +00:00
|
|
|
if (auto it = impl_->blocks_.find(block); it != std::end(impl_->blocks_))
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
auto const n = std::size(it->second);
|
|
|
|
removed.resize(n);
|
|
|
|
std::transform(
|
|
|
|
std::begin(it->second),
|
|
|
|
std::end(it->second),
|
|
|
|
std::begin(removed),
|
2023-06-25 17:33:32 +00:00
|
|
|
[](auto const& iter) { return const_cast<tr_peer*>(iter.first); });
|
2021-11-19 18:37:38 +00:00
|
|
|
impl_->blocks_.erase(block);
|
|
|
|
}
|
|
|
|
|
2022-04-02 22:42:51 +00:00
|
|
|
for (auto const* const peer : removed)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
impl_->decCount(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if there's an active request to `peer` for `block`
|
|
|
|
bool ActiveRequests::has(tr_block_index_t block, tr_peer const* peer) const
|
|
|
|
{
|
2023-06-25 17:33:32 +00:00
|
|
|
auto const iter = impl_->blocks_.find(block);
|
|
|
|
return iter != std::end(impl_->blocks_) && (iter->second.count(peer) != 0U);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// count how many peers we're asking for `block`
|
|
|
|
size_t ActiveRequests::count(tr_block_index_t block) const
|
|
|
|
{
|
2023-05-13 01:18:26 +00:00
|
|
|
auto const& blocks = impl_->blocks_;
|
|
|
|
auto const iter = blocks.find(block);
|
|
|
|
return iter == std::end(blocks) ? 0U : std::size(iter->second);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// count how many active block requests we have to `peer`
|
|
|
|
size_t ActiveRequests::count(tr_peer const* peer) const
|
|
|
|
{
|
|
|
|
return impl_->count(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the total number of active requests
|
|
|
|
size_t ActiveRequests::size() const
|
|
|
|
{
|
|
|
|
return impl_->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the active requests sent before `when`
|
|
|
|
std::vector<std::pair<tr_block_index_t, tr_peer*>> ActiveRequests::sentBefore(time_t when) const
|
|
|
|
{
|
|
|
|
auto sent_before = std::vector<std::pair<tr_block_index_t, tr_peer*>>{};
|
|
|
|
sent_before.reserve(std::size(impl_->blocks_));
|
|
|
|
|
2022-04-02 22:42:51 +00:00
|
|
|
for (auto const& [block, peers_at] : impl_->blocks_)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2023-06-25 17:33:32 +00:00
|
|
|
for (auto const& [peer, sent_at] : peers_at)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2023-06-25 17:33:32 +00:00
|
|
|
if (sent_at < when)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2023-06-25 17:33:32 +00:00
|
|
|
sent_before.emplace_back(block, const_cast<tr_peer*>(peer));
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sent_before;
|
|
|
|
}
|