refactor: tr_peerIo ref, unref (#3735)

* refactor: replace manual peerIo refcounting with std::shared_ptr
This commit is contained in:
Charles Kerr
2022-08-30 12:38:30 -05:00
committed by GitHub
parent 7c014e3256
commit b7ea4d9f04
11 changed files with 280 additions and 361 deletions

View File

@@ -66,14 +66,23 @@ struct evbuffer_deleter
using tr_evbuffer_ptr = std::unique_ptr<evbuffer, evbuffer_deleter>;
class tr_peerIo
namespace libtransmission::test
{
class HandshakeTest;
} // namespace libtransmission::test
class tr_peerIo final : public std::enable_shared_from_this<tr_peerIo>
{
using DH = tr_message_stream_encryption::DH;
using Filter = tr_message_stream_encryption::Filter;
public:
~tr_peerIo();
// TODO: 8 constructor args is too many; maybe a builder object?
static tr_peerIo* newOutgoing(
static std::shared_ptr<tr_peerIo> newOutgoing(
tr_session* session,
tr_bandwidth* parent,
struct tr_address const* addr,
@@ -83,7 +92,7 @@ public:
bool is_seed,
bool utp);
static tr_peerIo* newIncoming(
static std::shared_ptr<tr_peerIo> newIncoming(
tr_session* session,
tr_bandwidth* parent,
struct tr_address const* addr,
@@ -91,19 +100,6 @@ public:
time_t current_time,
struct tr_peer_socket const socket);
// this is only public for testing purposes.
// production code should use newOutgoing() or newIncoming()
static tr_peerIo* create(
tr_session* session,
tr_bandwidth* parent,
tr_address const* addr,
tr_port port,
time_t current_time,
tr_sha1_digest_t const* torrent_hash,
bool is_incoming,
bool is_seed,
struct tr_peer_socket const socket);
void clear();
void readBytes(void* bytes, size_t byte_count);
@@ -218,7 +214,7 @@ public:
bandwidth_.setParent(parent);
}
[[nodiscard]] constexpr auto isIncoming() noexcept
[[nodiscard]] constexpr auto isIncoming() const noexcept
{
return is_incoming_;
}
@@ -235,12 +231,6 @@ public:
void setCallbacks(tr_can_read_cb readcb, tr_did_write_cb writecb, tr_net_error_cb errcb, void* user_data);
// TODO(ckerr): yikes, unlike other class' magic_numbers it looks
// like this one isn't being used just for assertions, but also in
// didWriteWrapper() to see if the tr_peerIo got freed during the
// notify-consumed events. Fix this before removing this field.
int magic_number = PEER_IO_MAGIC_NUMBER;
struct tr_peer_socket socket = {};
tr_session* const session;
@@ -260,9 +250,6 @@ public:
struct event* event_read = nullptr;
struct event* event_write = nullptr;
// TODO: use std::shared_ptr instead of manual refcounting?
int refCount = 1;
short int pendingEvents = 0;
tr_priority_t priority = TR_PRI_NORMAL;
@@ -297,6 +284,21 @@ public:
static void utpInit(struct_utp_context* ctx);
private:
friend class libtransmission::test::HandshakeTest;
// this is only public for testing purposes.
// production code should use newOutgoing() or newIncoming()
static std::shared_ptr<tr_peerIo> create(
tr_session* session,
tr_bandwidth* parent,
tr_address const* addr,
tr_port port,
time_t current_time,
tr_sha1_digest_t const* torrent_hash,
bool is_incoming,
bool is_seed,
struct tr_peer_socket const socket);
tr_peerIo(
tr_session* session_in,
tr_sha1_digest_t const* torrent_hash,
@@ -347,24 +349,11 @@ private:
bool fast_extension_supported_ = false;
};
void tr_peerIoRefImpl(char const* file, int line, tr_peerIo* io);
#define tr_peerIoRef(io) tr_peerIoRefImpl(__FILE__, __LINE__, (io))
void tr_peerIoUnrefImpl(char const* file, int line, tr_peerIo* io);
#define tr_peerIoUnref(io) tr_peerIoUnrefImpl(__FILE__, __LINE__, (io))
constexpr bool tr_isPeerIo(tr_peerIo const* io)
{
return io != nullptr && io->magic_number == PEER_IO_MAGIC_NUMBER && io->refCount >= 0 &&
tr_address_is_valid(&io->address());
return io != nullptr && tr_address_is_valid(&io->address());
}
/**
***
**/
void evbuffer_add_uint8(struct evbuffer* outbuf, uint8_t addme);
void evbuffer_add_uint16(struct evbuffer* outbuf, uint16_t hs);
void evbuffer_add_uint32(struct evbuffer* outbuf, uint32_t hl);
@@ -373,5 +362,3 @@ void evbuffer_add_uint64(struct evbuffer* outbuf, uint64_t hll);
void evbuffer_add_hton_16(struct evbuffer* buf, uint16_t val);
void evbuffer_add_hton_32(struct evbuffer* buf, uint32_t val);
void evbuffer_add_hton_64(struct evbuffer* buf, uint64_t val);
/* @} */