2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2021-2023 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>
|
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 <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define LIBTRANSMISSION_PEER_MODULE
|
|
|
|
|
|
|
|
#include "peer-mgr-active-requests.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
#include "tr-assert.h"
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct peer_at
|
|
|
|
{
|
|
|
|
tr_peer* peer;
|
|
|
|
time_t when;
|
|
|
|
|
|
|
|
peer_at(tr_peer* p, time_t w)
|
|
|
|
: peer{ p }
|
|
|
|
, when{ w }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
[[nodiscard]] int compare(peer_at const& that) const // <=>
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
if (peer != that.peer)
|
|
|
|
{
|
|
|
|
return peer < that.peer ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(peer_at const& that) const
|
|
|
|
{
|
|
|
|
return compare(that) == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PeerAtHash
|
|
|
|
{
|
|
|
|
std::size_t operator()(peer_at const& pa) const noexcept
|
|
|
|
{
|
|
|
|
return std::hash<tr_peer*>{}(pa.peer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
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_;
|
|
|
|
|
|
|
|
std::unordered_map<tr_block_index_t, std::unordered_set<peer_at, PeerAtHash>> blocks_;
|
|
|
|
|
|
|
|
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);
|
|
|
|
auto const key = peer_at{ const_cast<tr_peer*>(peer), 0 };
|
|
|
|
auto const removed = it != std::end(impl_->blocks_) && it->second.erase(key) != 0;
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
auto const key = peer_at{ const_cast<tr_peer*>(peer), 0 };
|
2021-11-25 22:39:19 +00:00
|
|
|
for (auto const& [block, peers_at] : impl_->blocks_)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
if (peers_at.count(key) != 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),
|
|
|
|
[](auto const& sent) { return sent.peer; });
|
|
|
|
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
|
|
|
|
{
|
|
|
|
auto const it = impl_->blocks_.find(block);
|
2022-02-07 04:28:36 +00:00
|
|
|
return it != std::end(impl_->blocks_) && (it->second.count(peer_at{ const_cast<tr_peer*>(peer), 0 }) != 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
|
|
|
|
{
|
|
|
|
return std::size(impl_->blocks_[block]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2022-01-24 19:07:55 +00:00
|
|
|
for (auto const& sent : peers_at)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
if (sent.when < when)
|
|
|
|
{
|
2021-11-25 22:39:19 +00:00
|
|
|
sent_before.emplace_back(block, sent.peer);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sent_before;
|
|
|
|
}
|