mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
refactor: small again (#5666)
* refactor: restore the non-small parts of 5654 * refactor: restore the peer-mgr-active-requests parts of 5654 * chore: iwyu evbuffer in web.h
This commit is contained in:
@@ -50,6 +50,7 @@ using tau_connection_t = uint64_t;
|
|||||||
using tau_transaction_t = uint32_t;
|
using tau_transaction_t = uint32_t;
|
||||||
|
|
||||||
using InBuf = libtransmission::BufferReader<std::byte>;
|
using InBuf = libtransmission::BufferReader<std::byte>;
|
||||||
|
using PayloadBuffer = libtransmission::Buffer;
|
||||||
|
|
||||||
constexpr auto TauConnectionTtlSecs = time_t{ 45 };
|
constexpr auto TauConnectionTtlSecs = time_t{ 45 };
|
||||||
|
|
||||||
@@ -85,7 +86,7 @@ struct tau_scrape_request
|
|||||||
}
|
}
|
||||||
|
|
||||||
// build the payload
|
// build the payload
|
||||||
auto buf = libtransmission::Buffer{};
|
auto buf = PayloadBuffer{};
|
||||||
buf.add_uint32(TAU_ACTION_SCRAPE);
|
buf.add_uint32(TAU_ACTION_SCRAPE);
|
||||||
buf.add_uint32(transaction_id);
|
buf.add_uint32(transaction_id);
|
||||||
for (int i = 0; i < in.info_hash_count; ++i)
|
for (int i = 0; i < in.info_hash_count; ++i)
|
||||||
@@ -179,7 +180,7 @@ struct tau_announce_request
|
|||||||
response.info_hash = in.info_hash;
|
response.info_hash = in.info_hash;
|
||||||
|
|
||||||
// build the payload
|
// build the payload
|
||||||
auto buf = libtransmission::Buffer{};
|
auto buf = PayloadBuffer{};
|
||||||
buf.add_uint32(TAU_ACTION_ANNOUNCE);
|
buf.add_uint32(TAU_ACTION_ANNOUNCE);
|
||||||
buf.add_uint32(transaction_id);
|
buf.add_uint32(transaction_id);
|
||||||
buf.add(in.info_hash);
|
buf.add(in.info_hash);
|
||||||
@@ -374,7 +375,7 @@ struct tau_tracker
|
|||||||
this->connection_transaction_id = tau_transaction_new();
|
this->connection_transaction_id = tau_transaction_new();
|
||||||
logtrace(this->key, fmt::format("Trying to connect. Transaction ID is {}", this->connection_transaction_id));
|
logtrace(this->key, fmt::format("Trying to connect. Transaction ID is {}", this->connection_transaction_id));
|
||||||
|
|
||||||
auto buf = libtransmission::Buffer{};
|
auto buf = PayloadBuffer{};
|
||||||
buf.add_uint64(0x41727101980LL);
|
buf.add_uint64(0x41727101980LL);
|
||||||
buf.add_uint32(TAU_ACTION_CONNECT);
|
buf.add_uint32(TAU_ACTION_CONNECT);
|
||||||
buf.add_uint32(this->connection_transaction_id);
|
buf.add_uint32(this->connection_transaction_id);
|
||||||
@@ -462,7 +463,7 @@ private:
|
|||||||
{
|
{
|
||||||
if (this->connecting_at != 0 && this->connecting_at + ConnectionRequestTtl < now)
|
if (this->connecting_at != 0 && this->connecting_at + ConnectionRequestTtl < now)
|
||||||
{
|
{
|
||||||
auto empty_buf = libtransmission::Buffer{};
|
auto empty_buf = PayloadBuffer{};
|
||||||
on_connection_response(TAU_ACTION_ERROR, empty_buf);
|
on_connection_response(TAU_ACTION_ERROR, empty_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -535,7 +536,7 @@ private:
|
|||||||
{
|
{
|
||||||
logdbg(this->key, fmt::format("sending request w/connection id {}", this->connection_id));
|
logdbg(this->key, fmt::format("sending request w/connection id {}", this->connection_id));
|
||||||
|
|
||||||
auto buf = libtransmission::Buffer{};
|
auto buf = PayloadBuffer{};
|
||||||
buf.add_uint64(this->connection_id);
|
buf.add_uint64(this->connection_id);
|
||||||
buf.add(payload, payload_len);
|
buf.add(payload, payload_len);
|
||||||
|
|
||||||
@@ -622,7 +623,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// extract the action_id and see if it makes sense
|
// extract the action_id and see if it makes sense
|
||||||
auto buf = libtransmission::Buffer{};
|
auto buf = PayloadBuffer{};
|
||||||
buf.add(msg, msglen);
|
buf.add(msg, msglen);
|
||||||
auto const action_id = static_cast<tau_action_t>(buf.to_uint32());
|
auto const action_id = static_cast<tau_action_t>(buf.to_uint32());
|
||||||
|
|
||||||
|
|||||||
@@ -419,7 +419,7 @@ size_t tr_peerIo::try_read(size_t max)
|
|||||||
|
|
||||||
auto& buf = inbuf_;
|
auto& buf = inbuf_;
|
||||||
tr_error* error = nullptr;
|
tr_error* error = nullptr;
|
||||||
auto const n_read = socket_.try_read(buf, max, &error);
|
auto const n_read = socket_.try_read(buf, max, std::empty(buf), &error);
|
||||||
set_enabled(Dir, error == nullptr || canRetryFromError(error->code));
|
set_enabled(Dir, error == nullptr || canRetryFromError(error->code));
|
||||||
|
|
||||||
if (error != nullptr)
|
if (error != nullptr)
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility> // std::pair
|
#include <utility> // std::pair
|
||||||
|
|
||||||
|
#include <event2/util.h> // for evutil_socket_t
|
||||||
|
|
||||||
#include "transmission.h"
|
#include "transmission.h"
|
||||||
|
|
||||||
#include "bandwidth.h"
|
#include "bandwidth.h"
|
||||||
@@ -289,8 +291,11 @@ public:
|
|||||||
static void utp_init(struct_utp_context* ctx);
|
static void utp_init(struct_utp_context* ctx);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// our target socket receive buffer size
|
||||||
static constexpr auto RcvBuf = size_t{ 256 * 1024 };
|
static constexpr auto RcvBuf = size_t{ 256 * 1024 };
|
||||||
|
|
||||||
|
using PeerBuffer = libtransmission::Buffer;
|
||||||
|
|
||||||
friend class libtransmission::test::HandshakeTest;
|
friend class libtransmission::test::HandshakeTest;
|
||||||
|
|
||||||
[[nodiscard]] constexpr auto is_seed() const noexcept
|
[[nodiscard]] constexpr auto is_seed() const noexcept
|
||||||
@@ -342,8 +347,8 @@ private:
|
|||||||
|
|
||||||
tr_sha1_digest_t info_hash_;
|
tr_sha1_digest_t info_hash_;
|
||||||
|
|
||||||
libtransmission::Buffer inbuf_;
|
PeerBuffer inbuf_;
|
||||||
libtransmission::Buffer outbuf_;
|
PeerBuffer outbuf_;
|
||||||
|
|
||||||
tr_session* const session_;
|
tr_session* const session_;
|
||||||
|
|
||||||
|
|||||||
@@ -11,53 +11,16 @@
|
|||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <small/map.hpp>
|
||||||
|
|
||||||
#define LIBTRANSMISSION_PEER_MODULE
|
#define LIBTRANSMISSION_PEER_MODULE
|
||||||
|
|
||||||
#include "libtransmission/transmission.h"
|
#include "libtransmission/transmission.h"
|
||||||
|
|
||||||
#include "libtransmission/peer-mgr-active-requests.h"
|
#include "libtransmission/peer-mgr-active-requests.h"
|
||||||
|
#include "libtransmission/peer-mgr-wishlist.h"
|
||||||
#include "libtransmission/tr-assert.h"
|
#include "libtransmission/tr-assert.h"
|
||||||
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
|
|
||||||
struct peer_at
|
|
||||||
{
|
|
||||||
tr_peer* peer;
|
|
||||||
time_t when;
|
|
||||||
|
|
||||||
peer_at(tr_peer* p, time_t w)
|
|
||||||
: peer{ p }
|
|
||||||
, when{ w }
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] int compare(peer_at const& that) const // <=>
|
|
||||||
{
|
|
||||||
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
|
class ActiveRequests::Impl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -97,7 +60,7 @@ public:
|
|||||||
|
|
||||||
std::unordered_map<tr_peer const*, size_t> count_;
|
std::unordered_map<tr_peer const*, size_t> count_;
|
||||||
|
|
||||||
std::unordered_map<tr_block_index_t, std::unordered_set<peer_at, PeerAtHash>> blocks_;
|
std::unordered_map<tr_block_index_t, small::map<tr_peer const*, time_t, Wishlist::EndgameMaxPeers>> blocks_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t size_ = 0;
|
size_t size_ = 0;
|
||||||
@@ -126,8 +89,7 @@ bool ActiveRequests::add(tr_block_index_t block, tr_peer* peer, time_t when)
|
|||||||
bool ActiveRequests::remove(tr_block_index_t block, tr_peer const* peer)
|
bool ActiveRequests::remove(tr_block_index_t block, tr_peer const* peer)
|
||||||
{
|
{
|
||||||
auto const it = impl_->blocks_.find(block);
|
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(peer) != 0;
|
||||||
auto const removed = it != std::end(impl_->blocks_) && it->second.erase(key) != 0;
|
|
||||||
|
|
||||||
if (removed)
|
if (removed)
|
||||||
{
|
{
|
||||||
@@ -148,10 +110,9 @@ std::vector<tr_block_index_t> ActiveRequests::remove(tr_peer const* peer)
|
|||||||
auto removed = std::vector<tr_block_index_t>{};
|
auto removed = std::vector<tr_block_index_t>{};
|
||||||
removed.reserve(impl_->blocks_.size());
|
removed.reserve(impl_->blocks_.size());
|
||||||
|
|
||||||
auto const key = peer_at{ const_cast<tr_peer*>(peer), 0 };
|
|
||||||
for (auto const& [block, peers_at] : impl_->blocks_)
|
for (auto const& [block, peers_at] : impl_->blocks_)
|
||||||
{
|
{
|
||||||
if (peers_at.count(key) != 0U)
|
if (peers_at.count(peer) != 0U)
|
||||||
{
|
{
|
||||||
removed.push_back(block);
|
removed.push_back(block);
|
||||||
}
|
}
|
||||||
@@ -178,7 +139,7 @@ std::vector<tr_peer*> ActiveRequests::remove(tr_block_index_t block)
|
|||||||
std::begin(it->second),
|
std::begin(it->second),
|
||||||
std::end(it->second),
|
std::end(it->second),
|
||||||
std::begin(removed),
|
std::begin(removed),
|
||||||
[](auto const& sent) { return sent.peer; });
|
[](auto const& iter) { return const_cast<tr_peer*>(iter.first); });
|
||||||
impl_->blocks_.erase(block);
|
impl_->blocks_.erase(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,8 +154,8 @@ std::vector<tr_peer*> ActiveRequests::remove(tr_block_index_t block)
|
|||||||
// return true if there's an active request to `peer` for `block`
|
// 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
|
bool ActiveRequests::has(tr_block_index_t block, tr_peer const* peer) const
|
||||||
{
|
{
|
||||||
auto const it = impl_->blocks_.find(block);
|
auto const iter = impl_->blocks_.find(block);
|
||||||
return it != std::end(impl_->blocks_) && (it->second.count(peer_at{ const_cast<tr_peer*>(peer), 0 }) != 0U);
|
return iter != std::end(impl_->blocks_) && (iter->second.count(peer) != 0U);
|
||||||
}
|
}
|
||||||
|
|
||||||
// count how many peers we're asking for `block`
|
// count how many peers we're asking for `block`
|
||||||
@@ -225,11 +186,11 @@ std::vector<std::pair<tr_block_index_t, tr_peer*>> ActiveRequests::sentBefore(ti
|
|||||||
|
|
||||||
for (auto const& [block, peers_at] : impl_->blocks_)
|
for (auto const& [block, peers_at] : impl_->blocks_)
|
||||||
{
|
{
|
||||||
for (auto const& sent : peers_at)
|
for (auto const& [peer, sent_at] : peers_at)
|
||||||
{
|
{
|
||||||
if (sent.when < when)
|
if (sent_at < when)
|
||||||
{
|
{
|
||||||
sent_before.emplace_back(block, sent.peer);
|
sent_before.emplace_back(block, const_cast<tr_peer*>(peer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,8 @@
|
|||||||
class Wishlist
|
class Wishlist
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static auto constexpr EndgameMaxPeers = size_t{ 2U };
|
||||||
|
|
||||||
struct Mediator
|
struct Mediator
|
||||||
{
|
{
|
||||||
[[nodiscard]] virtual bool clientCanRequestBlock(tr_block_index_t block) const = 0;
|
[[nodiscard]] virtual bool clientCanRequestBlock(tr_block_index_t block) const = 0;
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ void tr_peer_socket::close()
|
|||||||
handle = {};
|
handle = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t tr_peer_socket::try_write(Buffer& buf, size_t max, tr_error** error) const
|
size_t tr_peer_socket::try_write(OutBuf& buf, size_t max, tr_error** error) const
|
||||||
{
|
{
|
||||||
if (max == size_t{})
|
if (max == size_t{})
|
||||||
{
|
{
|
||||||
@@ -107,7 +107,7 @@ size_t tr_peer_socket::try_write(Buffer& buf, size_t max, tr_error** error) cons
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t tr_peer_socket::try_read(Buffer& buf, size_t max, tr_error** error) const
|
size_t tr_peer_socket::try_read(InBuf& buf, size_t max, [[maybe_unused]] bool buf_is_empty, tr_error** error) const
|
||||||
{
|
{
|
||||||
if (max == size_t{})
|
if (max == size_t{})
|
||||||
{
|
{
|
||||||
@@ -123,7 +123,7 @@ size_t tr_peer_socket::try_read(Buffer& buf, size_t max, tr_error** error) const
|
|||||||
// utp_read_drained() notifies libutp that this read buffer is empty.
|
// utp_read_drained() notifies libutp that this read buffer is empty.
|
||||||
// It opens up the congestion window by sending an ACK (soonish) if
|
// It opens up the congestion window by sending an ACK (soonish) if
|
||||||
// one was not going to be sent.
|
// one was not going to be sent.
|
||||||
if (is_utp() && std::empty(buf))
|
if (is_utp() && buf_is_empty)
|
||||||
{
|
{
|
||||||
utp_read_drained(handle.utp);
|
utp_read_drained(handle.utp);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ struct tr_session;
|
|||||||
class tr_peer_socket
|
class tr_peer_socket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using Buffer = libtransmission::Buffer;
|
using InBuf = libtransmission::BufferWriter<std::byte>;
|
||||||
|
using OutBuf = libtransmission::BufferReader<std::byte>;
|
||||||
|
|
||||||
tr_peer_socket() = default;
|
tr_peer_socket() = default;
|
||||||
tr_peer_socket(tr_session const* session, tr_address const& address, tr_port port, tr_socket_t sock);
|
tr_peer_socket(tr_session const* session, tr_address const& address, tr_port port, tr_socket_t sock);
|
||||||
@@ -56,8 +57,8 @@ public:
|
|||||||
}
|
}
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
size_t try_write(Buffer& buf, size_t max, tr_error** error) const;
|
size_t try_read(InBuf& buf, size_t max, bool buf_is_empty, tr_error** error) const;
|
||||||
size_t try_read(Buffer& buf, size_t max, tr_error** error) const;
|
size_t try_write(OutBuf& buf, size_t max, tr_error** error) const;
|
||||||
|
|
||||||
[[nodiscard]] constexpr std::pair<tr_address, tr_port> socketAddress() const noexcept
|
[[nodiscard]] constexpr std::pair<tr_address, tr_port> socketAddress() const noexcept
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <event2/buffer.h>
|
||||||
|
|
||||||
struct evbuffer;
|
struct evbuffer;
|
||||||
|
|
||||||
class tr_web
|
class tr_web
|
||||||
|
|||||||
Reference in New Issue
Block a user