From d212a3bd8258bb2523280c7b081e4eb9a2194132 Mon Sep 17 00:00:00 2001 From: Hanjie Wu Date: Sat, 16 Dec 2023 04:50:31 -0800 Subject: add retry different protocols --- src/DefaultBtInteractive.cc | 3 ++ src/InitiatorMSEHandshakeCommand.cc | 74 ++++++++++++++++++++---------------- src/InitiatorMSEHandshakeCommand.h | 3 +- src/Peer.cc | 4 +- src/Peer.h | 25 +++++++++--- src/PeerAbstractCommand.cc | 5 +++ src/PeerAbstractCommand.h | 1 + src/PeerInitiateConnectionCommand.cc | 22 ++++++++++- src/PeerInitiateConnectionCommand.h | 1 + src/PeerInteractionCommand.cc | 17 +++++++++ src/PeerInteractionCommand.h | 1 + src/UTPexExtensionMessage.cc | 24 +++++++++--- src/uTPCore.cc | 8 ++-- 13 files changed, 135 insertions(+), 53 deletions(-) diff --git a/src/DefaultBtInteractive.cc b/src/DefaultBtInteractive.cc index da68aa95..519e53bd 100644 --- a/src/DefaultBtInteractive.cc +++ b/src/DefaultBtInteractive.cc @@ -171,6 +171,9 @@ DefaultBtInteractive::receiveAndSendHandshake() void DefaultBtInteractive::doPostHandshakeProcessing() { + if (peer_->isuTP() == true) { + peer_->uTPSupport(true); + } // Set time 0 to haveTimer to cache http/ftp download piece completion keepAliveTimer_ = global::wallclock(); floodingTimer_ = global::wallclock(); diff --git a/src/InitiatorMSEHandshakeCommand.cc b/src/InitiatorMSEHandshakeCommand.cc index ebcaa11b..9b539a04 100644 --- a/src/InitiatorMSEHandshakeCommand.cc +++ b/src/InitiatorMSEHandshakeCommand.cc @@ -40,6 +40,7 @@ #include "message.h" #include "prefs.h" #include "SocketCore.h" +#include "uTPCore.h" #include "Logger.h" #include "LogFactory.h" #include "Peer.h" @@ -197,7 +198,46 @@ bool InitiatorMSEHandshakeCommand::executeInternal() return false; } -void InitiatorMSEHandshakeCommand::tryNewPeer() +bool InitiatorMSEHandshakeCommand::retryDifferentProtocol() +{ + if (sequence_ != INITIATOR_SEND_KEY) { + // Try legacy handshake when connection can be established + if (!(getOption()->getAsBool(PREF_BT_FORCE_ENCRYPTION) || + getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO))) { + // Legacy handshake allowed by configuration + A2_LOG_INFO(fmt("CUID#%" PRId64 + " - Retry using legacy BitTorrent handshake.", + getCuid())); + auto command = make_unique( + getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_, + false); + command->setPeerStorage(peerStorage_); + command->setPieceStorage(pieceStorage_); + getDownloadEngine()->addCommand(std::move(command)); + return true; + } + else { + A2_LOG_INFO(fmt("CUID#%" PRId64 " - Establishing connection using legacy" + " BitTorrent handshake is disabled by preference.", + getCuid())); + } + } + // Try different protocol, start from using MSE handshake + int tried = getPeer()->triedProtocols(); + tried |= (getPeer()->isuTP() ? PEER_UTP : PEER_TCP); + if (tried == PEER_ALL) { + return false; + } + getPeer()->triedProtocols(tried); + auto command = make_unique( + getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_); + command->setPeerStorage(peerStorage_); + command->setPieceStorage(pieceStorage_); + getDownloadEngine()->addCommand(std::move(command)); + return true; +} + +bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait) { if (peerStorage_->isPeerAvailable() && btRuntime_->lessThanEqMinPeers()) { cuid_t ncuid = getDownloadEngine()->newCUID(); @@ -211,37 +251,7 @@ void InitiatorMSEHandshakeCommand::tryNewPeer() getDownloadEngine()->addCommand(std::move(command)); } } -} - -bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait) -{ - if (sequence_ == INITIATOR_SEND_KEY) { - // We don't try legacy handshake when connection did not - // established. - tryNewPeer(); - return true; - } - else if (getOption()->getAsBool(PREF_BT_FORCE_ENCRYPTION) || - getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)) { - A2_LOG_INFO(fmt("CUID#%" PRId64 " - Establishing connection using legacy" - " BitTorrent handshake is disabled by preference.", - getCuid())); - tryNewPeer(); - return true; - } - else { - // try legacy BitTorrent handshake - A2_LOG_INFO(fmt("CUID#%" PRId64 - " - Retry using legacy BitTorrent handshake.", - getCuid())); - auto command = make_unique( - getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_, - false); - command->setPeerStorage(peerStorage_); - command->setPieceStorage(pieceStorage_); - getDownloadEngine()->addCommand(std::move(command)); - return true; - } + return true; } void InitiatorMSEHandshakeCommand::onAbort() diff --git a/src/InitiatorMSEHandshakeCommand.h b/src/InitiatorMSEHandshakeCommand.h index 91f1a065..1ec94fb6 100644 --- a/src/InitiatorMSEHandshakeCommand.h +++ b/src/InitiatorMSEHandshakeCommand.h @@ -72,10 +72,9 @@ private: const std::shared_ptr