refactor: remove utils dep from peer-io (#2719)

This commit is contained in:
Charles Kerr 2022-02-28 14:26:26 -08:00 committed by GitHub
parent e3d1a251d8
commit d115ee6dfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 27 deletions

View File

@ -597,9 +597,10 @@ static tr_peerIo* tr_peerIoNew(
Bandwidth* parent,
tr_address const* addr,
tr_port port,
time_t current_time,
tr_sha1_digest_t const* torrent_hash,
bool is_incoming,
bool isSeed,
bool is_seed,
struct tr_peer_socket const socket)
{
TR_ASSERT(session != nullptr);
@ -618,7 +619,7 @@ static tr_peerIo* tr_peerIoNew(
maybeSetCongestionAlgorithm(socket.handle.tcp, session->peerCongestionAlgorithm());
}
auto* io = new tr_peerIo{ session, torrent_hash, is_incoming, *addr, port, isSeed };
auto* io = new tr_peerIo{ session, torrent_hash, is_incoming, *addr, port, is_seed, current_time };
io->socket = socket;
io->bandwidth = new Bandwidth(parent);
io->bandwidth->setPeer(io);
@ -662,12 +663,13 @@ tr_peerIo* tr_peerIoNewIncoming(
Bandwidth* parent,
tr_address const* addr,
tr_port port,
time_t current_time,
struct tr_peer_socket const socket)
{
TR_ASSERT(session != nullptr);
TR_ASSERT(tr_address_is_valid(addr));
return tr_peerIoNew(session, parent, addr, port, nullptr, true, false, socket);
return tr_peerIoNew(session, parent, addr, port, current_time, nullptr, true, false, socket);
}
tr_peerIo* tr_peerIoNewOutgoing(
@ -675,8 +677,9 @@ tr_peerIo* tr_peerIoNewOutgoing(
Bandwidth* parent,
tr_address const* addr,
tr_port port,
time_t current_time,
tr_sha1_digest_t const& torrent_hash,
bool isSeed,
bool is_seed,
bool utp)
{
TR_ASSERT(session != nullptr);
@ -686,12 +689,12 @@ tr_peerIo* tr_peerIoNewOutgoing(
if (utp)
{
socket = tr_netOpenPeerUTPSocket(session, addr, port, isSeed);
socket = tr_netOpenPeerUTPSocket(session, addr, port, is_seed);
}
if (socket.type == TR_PEER_SOCKET_TYPE_NONE)
{
socket = tr_netOpenPeerSocket(session, addr, port, isSeed);
socket = tr_netOpenPeerSocket(session, addr, port, is_seed);
dbgmsg(
nullptr,
"tr_netOpenPeerSocket returned fd %" PRIdMAX,
@ -703,7 +706,7 @@ tr_peerIo* tr_peerIoNewOutgoing(
return nullptr;
}
return tr_peerIoNew(session, parent, addr, port, &torrent_hash, false, isSeed, socket);
return tr_peerIoNew(session, parent, addr, port, current_time, &torrent_hash, false, is_seed, socket);
}
/***
@ -958,7 +961,7 @@ int tr_peerIoReconnect(tr_peerIo* io)
io_close_socket(io);
io->socket = tr_netOpenPeerSocket(session, &io->addr, io->port, io->isSeed);
io->socket = tr_netOpenPeerSocket(session, &io->addr, io->port, io->is_seed);
if (io->socket.type != TR_PEER_SOCKET_TYPE_TCP)
{

View File

@ -27,7 +27,6 @@
#include "net.h" /* tr_address */
#include "peer-socket.h"
#include "tr-assert.h"
#include "utils.h" // tr_time()
class tr_peerIo;
struct Bandwidth;
@ -70,14 +69,16 @@ public:
bool is_incoming,
tr_address const& addr_in,
tr_port port_in,
bool is_seed_in)
bool is_seed_in,
time_t current_time)
: crypto{ torrent_hash, is_incoming }
, addr{ addr_in }
, session{ session_in }
, time_created{ current_time }
, inbuf{ evbuffer_new() }
, outbuf{ evbuffer_new() }
, port{ port_in }
, isSeed{ is_seed_in }
, is_seed{ is_seed_in }
{
}
@ -99,10 +100,10 @@ public:
struct tr_peer_socket socket = {};
time_t const timeCreated = tr_time();
tr_session* const session;
time_t const time_created;
tr_can_read_cb canRead = nullptr;
tr_did_write_cb didWrite = nullptr;
tr_net_error_cb gotError = nullptr;
@ -131,7 +132,7 @@ public:
tr_priority_t priority = TR_PRI_NORMAL;
bool const isSeed;
bool const is_seed;
bool dhtSupported = false;
bool extendedProtocolSupported = false;
bool fastExtensionSupported = false;
@ -142,13 +143,15 @@ public:
***
**/
// TODO: 8 constructor args is too many; maybe a builder object?
tr_peerIo* tr_peerIoNewOutgoing(
tr_session* session,
Bandwidth* parent,
struct tr_address const* addr,
tr_port port,
time_t current_time,
tr_sha1_digest_t const& torrent_hash,
bool isSeed,
bool is_seed,
bool utp);
tr_peerIo* tr_peerIoNewIncoming(
@ -156,6 +159,7 @@ tr_peerIo* tr_peerIoNewIncoming(
Bandwidth* parent,
struct tr_address const* addr,
tr_port port,
time_t current_time,
struct tr_peer_socket const socket);
void tr_peerIoRefImpl(char const* file, int line, tr_peerIo* io);
@ -237,12 +241,6 @@ constexpr bool tr_peerIoIsIncoming(tr_peerIo const* io)
return io->crypto.is_incoming;
}
// TODO: remove this func; let caller get the current time instead
static inline int tr_peerIoGetAge(tr_peerIo const* io)
{
return tr_time() - io->timeCreated;
}
/**
***
**/

View File

@ -1096,7 +1096,7 @@ void tr_peerMgrAddIncoming(tr_peerMgr* manager, tr_address const* addr, tr_port
}
else /* we don't have a connection to them yet... */
{
tr_peerIo* const io = tr_peerIoNewIncoming(session, session->bandwidth, addr, port, socket);
tr_peerIo* const io = tr_peerIoNewIncoming(session, session->bandwidth, addr, port, tr_time(), socket);
tr_handshake* const handshake = tr_handshakeNew(io, session->encryptionMode, on_handshake_done, manager);
tr_peerIoUnref(io); /* balanced by the implicit ref in tr_peerIoNewIncoming() */
@ -2054,7 +2054,8 @@ static int compareChoke(void const* va, void const* vb)
/* is this a new connection? */
static bool isNew(tr_peerMsgs const* msgs)
{
return msgs != nullptr && msgs->get_connection_age() < 45;
auto constexpr CutoffSecs = time_t{ 45 };
return msgs != nullptr && !msgs->is_connection_older_than(tr_time() - CutoffSecs);
}
/* get a rate for deciding which peers to choke and unchoke. */
@ -3011,6 +3012,7 @@ static void initiateConnection(tr_peerMgr* mgr, tr_swarm* s, struct peer_atom* a
mgr->session->bandwidth,
&atom->addr,
atom->port,
tr_time(),
s->tor->infoHash(),
s->tor->completeness == TR_SEED,
utp);

View File

@ -357,9 +357,9 @@ public:
set_active(direction, calculate_active(direction));
}
[[nodiscard]] time_t get_connection_age() const override
[[nodiscard]] bool is_connection_older_than(time_t timestamp) const override
{
return tr_peerIoGetAge(io);
return io->time_created < timestamp;
}
void cancel_block_request(tr_block_index_t block) override

View File

@ -47,7 +47,7 @@ public:
virtual bool is_active(tr_direction direction) const = 0;
virtual void update_active(tr_direction direction) = 0;
virtual time_t get_connection_age() const = 0;
virtual bool is_connection_older_than(time_t time) const = 0;
virtual void cancel_block_request(tr_block_index_t block) = 0;

View File

@ -8,6 +8,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include "tr-macros.h" // tr_sha1_digest_t

View File

@ -25,7 +25,11 @@ TEST_F(BencTest, MalformedBenc)
auto handler = TestHandler{};
tr_error* error = nullptr;
EXPECT_FALSE(transmission::benc::parse(Benc, stack, handler, nullptr, &error));
EXPECT_NE(nullptr, error->message);
EXPECT_NE(nullptr, error);
if (error != nullptr)
{
EXPECT_NE(nullptr, error->message);
}
tr_error_clear(&error);
}