refactor: prefer std::unique_ptr over std::shared_ptr (#3741)
This commit is contained in:
parent
b989b72c0f
commit
d130f7d593
|
@ -115,7 +115,7 @@ enum handshake_state_t
|
|||
struct tr_handshake
|
||||
{
|
||||
tr_handshake(
|
||||
std::shared_ptr<tr_handshake_mediator> mediator_in,
|
||||
std::unique_ptr<tr_handshake_mediator> mediator_in,
|
||||
std::shared_ptr<tr_peerIo> io_in,
|
||||
tr_encryption_mode encryption_mode_in)
|
||||
: mediator{ std::move(mediator_in) }
|
||||
|
@ -136,7 +136,7 @@ struct tr_handshake
|
|||
return io->isIncoming();
|
||||
}
|
||||
|
||||
std::shared_ptr<tr_handshake_mediator> const mediator;
|
||||
std::unique_ptr<tr_handshake_mediator> const mediator;
|
||||
|
||||
bool haveReadAnythingFromPeer = false;
|
||||
bool haveSentBitTorrentHandshake = false;
|
||||
|
@ -1129,7 +1129,7 @@ static void gotError(tr_peerIo* io, short what, void* vhandshake)
|
|||
**/
|
||||
|
||||
tr_handshake* tr_handshakeNew(
|
||||
std::shared_ptr<tr_handshake_mediator> mediator,
|
||||
std::unique_ptr<tr_handshake_mediator> mediator,
|
||||
std::shared_ptr<tr_peerIo> io,
|
||||
tr_encryption_mode encryption_mode,
|
||||
tr_handshake_done_func done_func,
|
||||
|
|
|
@ -49,6 +49,8 @@ public:
|
|||
bool is_done;
|
||||
};
|
||||
|
||||
virtual ~tr_handshake_mediator() = default;
|
||||
|
||||
[[nodiscard]] virtual std::optional<torrent_info> torrentInfo(tr_sha1_digest_t const& info_hash) const = 0;
|
||||
|
||||
[[nodiscard]] virtual std::optional<torrent_info> torrentInfoFromObfuscated(tr_sha1_digest_t const& info_hash) const = 0;
|
||||
|
@ -76,7 +78,7 @@ using tr_handshake_done_func = bool (*)(tr_handshake_result const& result);
|
|||
|
||||
/** @brief create a new handshake */
|
||||
tr_handshake* tr_handshakeNew(
|
||||
std::shared_ptr<tr_handshake_mediator> mediator,
|
||||
std::unique_ptr<tr_handshake_mediator> mediator,
|
||||
std::shared_ptr<tr_peerIo> io,
|
||||
tr_encryption_mode encryption_mode,
|
||||
tr_handshake_done_func done_func,
|
||||
|
|
|
@ -90,13 +90,6 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
tr_handshake_mediator_impl(tr_handshake_mediator_impl&&) = delete;
|
||||
tr_handshake_mediator_impl(tr_handshake_mediator_impl const&) = delete;
|
||||
tr_handshake_mediator_impl& operator=(tr_handshake_mediator_impl&&) = delete;
|
||||
tr_handshake_mediator_impl& operator=(tr_handshake_mediator_impl const&) = delete;
|
||||
|
||||
virtual ~tr_handshake_mediator_impl() = default;
|
||||
|
||||
[[nodiscard]] std::optional<torrent_info> torrentInfo(tr_sha1_digest_t const& info_hash) const override
|
||||
{
|
||||
return torrentInfo(session_.torrents().get(info_hash));
|
||||
|
@ -1260,9 +1253,12 @@ void tr_peerMgrAddIncoming(tr_peerMgr* manager, tr_address const* addr, tr_port
|
|||
}
|
||||
else /* we don't have a connection to them yet... */
|
||||
{
|
||||
auto mediator = std::make_shared<tr_handshake_mediator_impl>(*session);
|
||||
auto io = tr_peerIo::newIncoming(session, &session->top_bandwidth_, addr, port, tr_time(), socket);
|
||||
auto* const handshake = tr_handshakeNew(mediator, std::move(io), session->encryptionMode(), on_handshake_done, manager);
|
||||
auto* const handshake = tr_handshakeNew(
|
||||
std::make_unique<tr_handshake_mediator_impl>(*session),
|
||||
tr_peerIo::newIncoming(session, &session->top_bandwidth_, addr, port, tr_time(), socket),
|
||||
session->encryptionMode(),
|
||||
on_handshake_done,
|
||||
manager);
|
||||
manager->incoming_handshakes.add(*addr, handshake);
|
||||
}
|
||||
}
|
||||
|
@ -2849,9 +2845,8 @@ void initiateConnection(tr_peerMgr* mgr, tr_swarm* s, peer_atom& atom)
|
|||
}
|
||||
else
|
||||
{
|
||||
auto mediator = std::make_shared<tr_handshake_mediator_impl>(*mgr->session);
|
||||
auto* const handshake = tr_handshakeNew(
|
||||
mediator,
|
||||
std::make_unique<tr_handshake_mediator_impl>(*mgr->session),
|
||||
std::move(io),
|
||||
mgr->session->encryptionMode(),
|
||||
on_handshake_done,
|
||||
|
|
|
@ -104,7 +104,7 @@ public:
|
|||
anti_brute_force_limit_ = limit;
|
||||
}
|
||||
|
||||
std::shared_ptr<libdeflate_compressor> compressor;
|
||||
std::unique_ptr<libdeflate_compressor, void (*)(libdeflate_compressor*)> compressor;
|
||||
|
||||
[[nodiscard]] constexpr auto const& url() const noexcept
|
||||
{
|
||||
|
|
|
@ -1395,8 +1395,10 @@ static void onBlocklistFetched(tr_web::FetchResponse const& web_response)
|
|||
content.resize(1024 * 128);
|
||||
for (;;)
|
||||
{
|
||||
auto decompressor = std::shared_ptr<libdeflate_decompressor>{ libdeflate_alloc_decompressor(),
|
||||
libdeflate_free_decompressor };
|
||||
auto decompressor = std::unique_ptr<libdeflate_decompressor, void (*)(libdeflate_decompressor*)>{
|
||||
libdeflate_alloc_decompressor(),
|
||||
libdeflate_free_decompressor
|
||||
};
|
||||
auto actual_size = size_t{};
|
||||
auto const decompress_result = libdeflate_gzip_decompress(
|
||||
decompressor.get(),
|
||||
|
|
|
@ -2913,7 +2913,7 @@ auto makeTorrentDir(std::string_view config_dir)
|
|||
auto makeEventBase()
|
||||
{
|
||||
tr_evthread_init();
|
||||
return std::shared_ptr<event_base>{ event_base_new(), event_base_free };
|
||||
return std::unique_ptr<event_base, void (*)(event_base*)>{ event_base_new(), event_base_free };
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -877,8 +877,8 @@ private:
|
|||
|
||||
LpdMediator lpd_mediator_{ *this };
|
||||
|
||||
std::shared_ptr<event_base> const event_base_;
|
||||
std::shared_ptr<evdns_base> const evdns_base_;
|
||||
std::unique_ptr<event_base, void (*)(event_base*)> const event_base_;
|
||||
std::unique_ptr<evdns_base, void (*)(evdns_base*)> const evdns_base_;
|
||||
std::unique_ptr<libtransmission::TimerMaker> const timer_maker_;
|
||||
|
||||
void onNowTimer();
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
#include "crypto-utils.h"
|
||||
#include "log.h"
|
||||
#include "peer-io.h"
|
||||
#include "tr-assert.h"
|
||||
#include "utils.h"
|
||||
#include "web.h"
|
||||
|
@ -179,8 +180,8 @@ public:
|
|||
class Task
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<evbuffer> const privbuf{ evbuffer_new(), evbuffer_free };
|
||||
std::shared_ptr<CURL> const easy_handle{ curl_easy_init(), curl_easy_cleanup };
|
||||
tr_evbuffer_ptr const privbuf = tr_evbuffer_ptr{ evbuffer_new() };
|
||||
std::unique_ptr<CURL, void (*)(CURL*)> const easy_handle{ curl_easy_init(), curl_easy_cleanup };
|
||||
tr_web::FetchOptions options;
|
||||
|
||||
public:
|
||||
|
@ -465,7 +466,7 @@ public:
|
|||
// the thread started by Impl.curl_thread runs this function
|
||||
static void curlThreadFunc(Impl* impl)
|
||||
{
|
||||
auto const multi = std::shared_ptr<CURLM>(curl_multi_init(), curl_multi_cleanup);
|
||||
auto const multi = std::unique_ptr<CURLM, CURLMcode (*)(CURLM*)>(curl_multi_init(), curl_multi_cleanup);
|
||||
|
||||
auto running_tasks = int{ 0 };
|
||||
auto repeats = unsigned{};
|
||||
|
@ -562,7 +563,7 @@ public:
|
|||
impl->is_closed_ = true;
|
||||
}
|
||||
|
||||
std::shared_ptr<CURLSH> const curlsh_{ curl_share_init(), curl_share_cleanup };
|
||||
std::unique_ptr<CURLSH, CURLSHcode (*)(CURLSH*)> const curlsh_{ curl_share_init(), curl_share_cleanup };
|
||||
|
||||
std::mutex queued_tasks_mutex;
|
||||
std::condition_variable queued_tasks_cv;
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include "bandwidth.h"
|
||||
#include "cache.h"
|
||||
#include "peer-io.h"
|
||||
#include "peer-mgr.h"
|
||||
#include "torrent.h"
|
||||
#include "trevent.h" /* tr_runInEventThread() */
|
||||
|
@ -42,7 +43,7 @@ void on_idle(tr_webseed* w);
|
|||
class tr_webseed_task
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<evbuffer> const content_{ evbuffer_new(), evbuffer_free };
|
||||
tr_evbuffer_ptr const content_{ evbuffer_new() };
|
||||
|
||||
public:
|
||||
tr_webseed_task(tr_torrent* tor, tr_webseed* webseed_in, tr_block_span_t blocks_in)
|
||||
|
@ -351,7 +352,7 @@ private:
|
|||
struct write_block_data
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<evbuffer> const content_{ evbuffer_new(), evbuffer_free };
|
||||
tr_evbuffer_ptr const content_{ evbuffer_new() };
|
||||
|
||||
public:
|
||||
write_block_data(
|
||||
|
|
|
@ -44,8 +44,6 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
virtual ~MediatorMock() = default;
|
||||
|
||||
[[nodiscard]] std::optional<torrent_info> torrentInfo(tr_sha1_digest_t const& info_hash) const override
|
||||
{
|
||||
if (auto const iter = torrents.find(info_hash); iter != std::end(torrents))
|
||||
|
@ -207,7 +205,7 @@ public:
|
|||
}
|
||||
|
||||
static auto runHandshake(
|
||||
std::shared_ptr<tr_handshake_mediator> mediator,
|
||||
std::unique_ptr<tr_handshake_mediator> mediator,
|
||||
std::shared_ptr<tr_peerIo> io,
|
||||
tr_encryption_mode encryption_mode = TR_CLEAR_PREFERRED)
|
||||
{
|
||||
|
@ -230,7 +228,7 @@ public:
|
|||
TEST_F(HandshakeTest, incomingPlaintext)
|
||||
{
|
||||
auto const peer_id = makeRandomPeerId();
|
||||
auto mediator = std::make_shared<MediatorMock>(session_);
|
||||
auto mediator = std::make_unique<MediatorMock>(session_);
|
||||
mediator->torrents.emplace(TorrentWeAreSeeding.info_hash, TorrentWeAreSeeding);
|
||||
|
||||
// The simplest handshake there is. "The handshake starts with character
|
||||
|
@ -247,7 +245,7 @@ TEST_F(HandshakeTest, incomingPlaintext)
|
|||
sendToClient(sock, TorrentWeAreSeeding.info_hash);
|
||||
sendToClient(sock, peer_id);
|
||||
|
||||
auto const res = runHandshake(mediator, io);
|
||||
auto const res = runHandshake(std::move(mediator), io);
|
||||
|
||||
// check the results
|
||||
EXPECT_TRUE(res);
|
||||
|
@ -266,7 +264,7 @@ TEST_F(HandshakeTest, incomingPlaintext)
|
|||
// but this time we don't recognize the infohash sent by the peer.
|
||||
TEST_F(HandshakeTest, incomingPlaintextUnknownInfoHash)
|
||||
{
|
||||
auto mediator = std::make_shared<MediatorMock>(session_);
|
||||
auto mediator = std::make_unique<MediatorMock>(session_);
|
||||
mediator->torrents.emplace(TorrentWeAreSeeding.info_hash, TorrentWeAreSeeding);
|
||||
|
||||
auto [io, sock] = createIncomingIo(session_);
|
||||
|
@ -275,7 +273,7 @@ TEST_F(HandshakeTest, incomingPlaintextUnknownInfoHash)
|
|||
sendToClient(sock, tr_sha1::digest("some other torrent unknown to us"sv));
|
||||
sendToClient(sock, makeRandomPeerId());
|
||||
|
||||
auto const res = runHandshake(mediator, io);
|
||||
auto const res = runHandshake(std::move(mediator), io);
|
||||
|
||||
// check the results
|
||||
EXPECT_TRUE(res);
|
||||
|
@ -291,7 +289,7 @@ TEST_F(HandshakeTest, incomingPlaintextUnknownInfoHash)
|
|||
TEST_F(HandshakeTest, outgoingPlaintext)
|
||||
{
|
||||
auto const peer_id = makeRandomPeerId();
|
||||
auto mediator = std::make_shared<MediatorMock>(session_);
|
||||
auto mediator = std::make_unique<MediatorMock>(session_);
|
||||
mediator->torrents.emplace(UbuntuTorrent.info_hash, TorrentWeAreSeeding);
|
||||
|
||||
auto [io, sock] = createOutgoingIo(session_, UbuntuTorrent.info_hash);
|
||||
|
@ -300,7 +298,7 @@ TEST_F(HandshakeTest, outgoingPlaintext)
|
|||
sendToClient(sock, UbuntuTorrent.info_hash);
|
||||
sendToClient(sock, peer_id);
|
||||
|
||||
auto const res = runHandshake(mediator, io);
|
||||
auto const res = runHandshake(std::move(mediator), io);
|
||||
|
||||
// check the results
|
||||
EXPECT_TRUE(res);
|
||||
|
@ -320,7 +318,7 @@ TEST_F(HandshakeTest, incomingEncrypted)
|
|||
{
|
||||
static auto constexpr ExpectedPeerId = makePeerId("-TR300Z-w4bd4mkebkbi"sv);
|
||||
|
||||
auto mediator = std::make_shared<MediatorMock>(session_);
|
||||
auto mediator = std::make_unique<MediatorMock>(session_);
|
||||
mediator->torrents.emplace(UbuntuTorrent.info_hash, UbuntuTorrent);
|
||||
mediator->setPrivateKeyFromBase64("0EYKCwBWQ4Dg9kX3c5xxjVtBDKw="sv);
|
||||
|
||||
|
@ -339,7 +337,7 @@ TEST_F(HandshakeTest, incomingEncrypted)
|
|||
"VGwrTPstEPu3V5lmzjtMGVLaL5EErlpJ93Xrz+ea6EIQEUZA+D4jKaV/to9NVi"
|
||||
"04/1W1A2PHgg+I9puac/i9BsFPcjdQeoVtU73lNCbTDQgTieyjDWmwo="sv);
|
||||
|
||||
auto const res = runHandshake(mediator, io);
|
||||
auto const res = runHandshake(std::move(mediator), io);
|
||||
|
||||
// check the results
|
||||
EXPECT_TRUE(res);
|
||||
|
@ -359,7 +357,7 @@ TEST_F(HandshakeTest, incomingEncrypted)
|
|||
// but this time we don't recognize the infohash sent by the peer.
|
||||
TEST_F(HandshakeTest, incomingEncryptedUnknownInfoHash)
|
||||
{
|
||||
auto mediator = std::make_shared<MediatorMock>(session_);
|
||||
auto mediator = std::make_unique<MediatorMock>(session_);
|
||||
mediator->setPrivateKeyFromBase64("0EYKCwBWQ4Dg9kX3c5xxjVtBDKw="sv);
|
||||
|
||||
auto [io, sock] = createIncomingIo(session_);
|
||||
|
@ -377,7 +375,7 @@ TEST_F(HandshakeTest, incomingEncryptedUnknownInfoHash)
|
|||
"VGwrTPstEPu3V5lmzjtMGVLaL5EErlpJ93Xrz+ea6EIQEUZA+D4jKaV/to9NVi"
|
||||
"04/1W1A2PHgg+I9puac/i9BsFPcjdQeoVtU73lNCbTDQgTieyjDWmwo="sv);
|
||||
|
||||
auto const res = runHandshake(mediator, io);
|
||||
auto const res = runHandshake(std::move(mediator), io);
|
||||
|
||||
// check the results
|
||||
EXPECT_TRUE(res);
|
||||
|
@ -392,7 +390,7 @@ TEST_F(HandshakeTest, outgoingEncrypted)
|
|||
{
|
||||
static auto constexpr ExpectedPeerId = makePeerId("-qB4250-scysDI_JuVN3"sv);
|
||||
|
||||
auto mediator = std::make_shared<MediatorMock>(session_);
|
||||
auto mediator = std::make_unique<MediatorMock>(session_);
|
||||
mediator->torrents.emplace(UbuntuTorrent.info_hash, UbuntuTorrent);
|
||||
mediator->setPrivateKeyFromBase64("0EYKCwBWQ4Dg9kX3c5xxjVtBDKw="sv);
|
||||
|
||||
|
@ -416,7 +414,7 @@ TEST_F(HandshakeTest, outgoingEncrypted)
|
|||
"3+o/RdiKQJAsGxMIU08scBc5VOmrAmjeYrLNpFnpXVuavH5if7490zMCu3DEn"
|
||||
"G9hpbYbiX95T+EUcRbM6pSCvr3Twq1Q="sv);
|
||||
|
||||
auto const res = runHandshake(mediator, io, TR_ENCRYPTION_PREFERRED);
|
||||
auto const res = runHandshake(std::move(mediator), io, TR_ENCRYPTION_PREFERRED);
|
||||
|
||||
// check the results
|
||||
EXPECT_TRUE(res);
|
||||
|
|
Loading…
Reference in New Issue