diff --git a/src/BtAnnounce.h b/src/BtAnnounce.h index ce29941e..1641e90a 100644 --- a/src/BtAnnounce.h +++ b/src/BtAnnounce.h @@ -109,6 +109,8 @@ public: virtual void overrideMinInterval(time_t interval) = 0; + virtual void setTcpPort(uint16_t port) = 0; + static const std::string FAILURE_REASON; static const std::string WARNING_MESSAGE; diff --git a/src/BtRegistry.cc b/src/BtRegistry.cc index a6f8ae64..883ff4bf 100644 --- a/src/BtRegistry.cc +++ b/src/BtRegistry.cc @@ -44,6 +44,12 @@ namespace aria2 { +BtRegistry::BtRegistry() + : tcpPort_(0) +{} + +BtRegistry::~BtRegistry() {} + SharedHandle BtRegistry::getDownloadContext(a2_gid_t gid) const { diff --git a/src/BtRegistry.h b/src/BtRegistry.h index c0d2a6a6..afc000b2 100644 --- a/src/BtRegistry.h +++ b/src/BtRegistry.h @@ -80,7 +80,11 @@ struct BtObject { class BtRegistry { private: std::map pool_; + uint16_t tcpPort_; public: + BtRegistry(); + ~BtRegistry(); + SharedHandle getDownloadContext(a2_gid_t gid) const; @@ -104,6 +108,15 @@ public: void removeAll(); bool remove(a2_gid_t gid); + + void setTcpPort(uint16_t port) + { + tcpPort_ = port; + } + uint16_t getTcpPort() const + { + return tcpPort_; + } }; } // namespace aria2 diff --git a/src/BtRuntime.cc b/src/BtRuntime.cc index a639a4f0..dca3e09d 100644 --- a/src/BtRuntime.cc +++ b/src/BtRuntime.cc @@ -39,7 +39,6 @@ namespace aria2 { BtRuntime::BtRuntime() : uploadLengthAtStartup_(0), - port_(0), halt_(false), connections_(0), ready_(false), diff --git a/src/BtRuntime.h b/src/BtRuntime.h index 30676d84..7ceeac65 100644 --- a/src/BtRuntime.h +++ b/src/BtRuntime.h @@ -43,7 +43,6 @@ namespace aria2 { class BtRuntime { private: uint64_t uploadLengthAtStartup_; - uint16_t port_; bool halt_; unsigned int connections_; bool ready_; @@ -69,12 +68,6 @@ public: uploadLengthAtStartup_ = length; } - void setListenPort(uint16_t port) { - port_ = port; - } - - uint16_t getListenPort() const { return port_; } - bool isHalt() const { return halt_; } void setHalt(bool halt) { diff --git a/src/BtSetup.cc b/src/BtSetup.cc index 2851c9a5..65ede7e4 100644 --- a/src/BtSetup.cc +++ b/src/BtSetup.cc @@ -100,7 +100,8 @@ void BtSetup::setup(std::vector& commands, SharedHandle torrentAttrs = bittorrent::getTorrentAttrs(requestGroup->getDownloadContext()); bool metadataGetMode = torrentAttrs->metadata.empty(); - BtObject btObject = e->getBtRegistry()->get(requestGroup->getGID()); + const SharedHandle& btReg = e->getBtRegistry(); + BtObject btObject = btReg->get(requestGroup->getGID()); SharedHandle pieceStorage = btObject.pieceStorage_; SharedHandle peerStorage = btObject.peerStorage_; SharedHandle btRuntime = btObject.btRuntime_; @@ -183,43 +184,38 @@ void BtSetup::setup(std::vector& commands, commands.push_back(c); } } - if(PeerListenCommand::getNumInstance() == 0) { + if(btReg->getTcpPort() == 0) { static int families[] = { AF_INET, AF_INET6 }; size_t familiesLength = e->getOption()->getAsBool(PREF_DISABLE_IPV6)?1:2; for(size_t i = 0; i < familiesLength; ++i) { - PeerListenCommand* listenCommand = - PeerListenCommand::getInstance(e, families[i]); + PeerListenCommand* command = + new PeerListenCommand(e->newCUID(), e, families[i]); bool ret; uint16_t port; - if(btRuntime->getListenPort()) { - IntSequence seq = - util::parseIntRange(util::uitos(btRuntime->getListenPort())); - ret = listenCommand->bindPort(port, seq); + if(btReg->getTcpPort()) { + IntSequence seq = util::parseIntRange(util::uitos(btReg->getTcpPort())); + ret = command->bindPort(port, seq); } else { IntSequence seq = util::parseIntRange(e->getOption()->get(PREF_LISTEN_PORT)); - ret = listenCommand->bindPort(port, seq); + ret = command->bindPort(port, seq); } if(ret) { - btRuntime->setListenPort(port); + btReg->setTcpPort(port); // Add command to DownloadEngine directly. - e->addCommand(listenCommand); + e->addCommand(command); } else { - delete listenCommand; + delete command; } } - if(PeerListenCommand::getNumInstance() == 0) { + if(btReg->getTcpPort() == 0) { throw DL_ABORT_EX(_("Errors occurred while binding port.\n")); } - } else { - PeerListenCommand* listenCommand = - PeerListenCommand::getInstance(e, AF_INET); - if(!listenCommand) { - listenCommand = PeerListenCommand::getInstance(e, AF_INET6); - } - btRuntime->setListenPort(listenCommand->getPort()); } + btAnnounce->setTcpPort(btReg->getTcpPort()); + if(option->getAsBool(PREF_BT_ENABLE_LPD) && + btReg->getTcpPort() && (metadataGetMode || !torrentAttrs->privateTorrent)) { if(LpdReceiveMessageCommand::getNumInstance() == 0) { A2_LOG_INFO("Initializing LpdMessageReceiver."); @@ -266,7 +262,7 @@ void BtSetup::setup(std::vector& commands, SharedHandle dispatcher (new LpdMessageDispatcher (std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]), - btRuntime->getListenPort(), + btReg->getTcpPort(), LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT)); if(dispatcher->init(receiver->getLocalAddress(), /*ttl*/1, /*loop*/0)) { A2_LOG_INFO("LpdMessageDispatcher initialized."); diff --git a/src/DHTGetPeersCommand.cc b/src/DHTGetPeersCommand.cc index 7c53df86..0582cef0 100644 --- a/src/DHTGetPeersCommand.cc +++ b/src/DHTGetPeersCommand.cc @@ -49,6 +49,7 @@ #include "DownloadContext.h" #include "wallclock.h" #include "fmt.h" +#include "BtRegistry.h" namespace aria2 { @@ -102,7 +103,9 @@ bool DHTGetPeersCommand::execute() bittorrent::getInfoHashString (requestGroup_->getDownloadContext()).c_str())); task_ = taskFactory_->createPeerLookupTask - (requestGroup_->getDownloadContext(), btRuntime_, peerStorage_); + (requestGroup_->getDownloadContext(), + e_->getBtRegistry()->getTcpPort(), + peerStorage_); taskQueue_->addPeriodicTask2(task_); } else if(task_ && task_->finished()) { A2_LOG_DEBUG("task finished detected"); diff --git a/src/DHTPeerLookupTask.cc b/src/DHTPeerLookupTask.cc index b3b080cb..0beaf998 100644 --- a/src/DHTPeerLookupTask.cc +++ b/src/DHTPeerLookupTask.cc @@ -43,7 +43,6 @@ #include "DHTMessageDispatcher.h" #include "DHTMessageCallback.h" #include "PeerStorage.h" -#include "BtRuntime.h" #include "util.h" #include "DHTBucket.h" #include "bittorrent_helper.h" @@ -54,9 +53,11 @@ namespace aria2 { DHTPeerLookupTask::DHTPeerLookupTask -(const SharedHandle& downloadContext) +(const SharedHandle& downloadContext, + uint16_t tcpPort) : DHTAbstractNodeLookupTask - (bittorrent::getInfoHash(downloadContext)) + (bittorrent::getInfoHash(downloadContext)), + tcpPort_(tcpPort) {} void @@ -115,18 +116,13 @@ void DHTPeerLookupTask::onFinish() getMessageFactory()->createAnnouncePeerMessage (node, getTargetID(), // this is infoHash - btRuntime_->getListenPort(), + tcpPort_, token); getMessageDispatcher()->addMessageToQueue(m); --num; } } -void DHTPeerLookupTask::setBtRuntime(const SharedHandle& btRuntime) -{ - btRuntime_ = btRuntime; -} - void DHTPeerLookupTask::setPeerStorage(const SharedHandle& ps) { peerStorage_ = ps; diff --git a/src/DHTPeerLookupTask.h b/src/DHTPeerLookupTask.h index e3c69c85..e80bbe59 100644 --- a/src/DHTPeerLookupTask.h +++ b/src/DHTPeerLookupTask.h @@ -43,7 +43,6 @@ namespace aria2 { class DownloadContext; class Peer; class PeerStorage; -class BtRuntime; class DHTGetPeersReplyMessage; class DHTPeerLookupTask: @@ -52,10 +51,11 @@ private: std::map tokenStorage_; SharedHandle peerStorage_; - - SharedHandle btRuntime_; + uint16_t tcpPort_; public: - DHTPeerLookupTask(const SharedHandle& downloadContext); + DHTPeerLookupTask + (const SharedHandle& downloadContext, + uint16_t tcpPort); virtual void getNodesFromMessage (std::vector >& nodes, @@ -69,8 +69,6 @@ public: virtual SharedHandle createCallback(); virtual void onFinish(); - - void setBtRuntime(const SharedHandle& btRuntime); void setPeerStorage(const SharedHandle& peerStorage); }; diff --git a/src/DHTTaskFactory.h b/src/DHTTaskFactory.h index 3e953b76..c59f705b 100644 --- a/src/DHTTaskFactory.h +++ b/src/DHTTaskFactory.h @@ -41,7 +41,6 @@ namespace aria2 { class DownloadContext; -class BtRuntime; class PeerStorage; class DHTTask; class DHTNode; @@ -62,7 +61,7 @@ public: virtual SharedHandle createPeerLookupTask(const SharedHandle& ctx, - const SharedHandle& btRuntime, + uint16_t tcpPort, const SharedHandle& peerStorage) = 0; virtual SharedHandle diff --git a/src/DHTTaskFactoryImpl.cc b/src/DHTTaskFactoryImpl.cc index d4488560..efc9afc3 100644 --- a/src/DHTTaskFactoryImpl.cc +++ b/src/DHTTaskFactoryImpl.cc @@ -46,7 +46,6 @@ #include "Peer.h" #include "DHTNodeLookupEntry.h" #include "PeerStorage.h" -#include "BtRuntime.h" #include "DHTMessageCallback.h" namespace aria2 { @@ -90,12 +89,11 @@ DHTTaskFactoryImpl::createBucketRefreshTask() SharedHandle DHTTaskFactoryImpl::createPeerLookupTask (const SharedHandle& ctx, - const SharedHandle& btRuntime, + uint16_t tcpPort, const SharedHandle& peerStorage) { - SharedHandle task(new DHTPeerLookupTask(ctx)); - // TODO these may be not freed by RequestGroup::releaseRuntimeResource() - task->setBtRuntime(btRuntime); + SharedHandle task(new DHTPeerLookupTask(ctx, tcpPort)); + // TODO this may be not freed by RequestGroup::releaseRuntimeResource() task->setPeerStorage(peerStorage); setCommonProperty(task); return task; diff --git a/src/DHTTaskFactoryImpl.h b/src/DHTTaskFactoryImpl.h index e90219aa..5adab80b 100644 --- a/src/DHTTaskFactoryImpl.h +++ b/src/DHTTaskFactoryImpl.h @@ -78,7 +78,7 @@ public: virtual SharedHandle createPeerLookupTask(const SharedHandle& ctx, - const SharedHandle& btRuntime, + uint16_t tcpPort, const SharedHandle& peerStorage); virtual SharedHandle diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index fa0ad96f..0f0efa86 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -68,7 +68,8 @@ DefaultBtAnnounce::DefaultBtAnnounce incomplete_(0), announceList_(bittorrent::getTorrentAttrs(downloadContext)->announceList), option_(option), - randomizer_(SimpleRandomizer::getInstance()) + randomizer_(SimpleRandomizer::getInstance()), + tcpPort_(0) {} DefaultBtAnnounce::~DefaultBtAnnounce() { @@ -166,9 +167,9 @@ std::string DefaultBtAnnounce::getAnnounceUrl() { uri += "&numwant="; uri += util::uitos(numWant); uri += "&no_peer_id=1"; - if(btRuntime_->getListenPort() > 0) { + if(tcpPort_) { uri += "&port="; - uri += util::uitos(btRuntime_->getListenPort()); + uri += util::uitos(tcpPort_); } std::string event = announceList_.getEventString(); if(!event.empty()) { diff --git a/src/DefaultBtAnnounce.h b/src/DefaultBtAnnounce.h index f2591339..c69e4214 100644 --- a/src/DefaultBtAnnounce.h +++ b/src/DefaultBtAnnounce.h @@ -65,6 +65,7 @@ private: SharedHandle btRuntime_; SharedHandle pieceStorage_; SharedHandle peerStorage_; + uint16_t tcpPort_; public: DefaultBtAnnounce(const SharedHandle& downloadContext, const Option* option); @@ -121,6 +122,11 @@ public: virtual void overrideMinInterval(time_t interval); + virtual void setTcpPort(uint16_t port) + { + tcpPort_ = port; + } + void setRandomizer(const SharedHandle& randomizer); time_t getInterval() const diff --git a/src/DefaultBtInteractive.cc b/src/DefaultBtInteractive.cc index 693187a8..a4017e8c 100644 --- a/src/DefaultBtInteractive.cc +++ b/src/DefaultBtInteractive.cc @@ -190,7 +190,7 @@ void DefaultBtInteractive::addHandshakeExtendedMessageToQueue() static const std::string CLIENT_ARIA2("aria2/"PACKAGE_VERSION); HandshakeExtensionMessageHandle m(new HandshakeExtensionMessage()); m->setClientVersion(CLIENT_ARIA2); - m->setTCPPort(btRuntime_->getListenPort()); + m->setTCPPort(tcpPort_); m->setExtensions(extensionMessageRegistry_->getExtensions()); SharedHandle attrs = bittorrent::getTorrentAttrs(downloadContext_); diff --git a/src/DefaultBtInteractive.h b/src/DefaultBtInteractive.h index fdbd892a..a34569d5 100644 --- a/src/DefaultBtInteractive.h +++ b/src/DefaultBtInteractive.h @@ -143,6 +143,8 @@ private: RequestGroupMan* requestGroupMan_; + uint16_t tcpPort_; + static const time_t FLOODING_CHECK_INTERVAL = 5; void addBitfieldMessageToQueue(); @@ -255,6 +257,11 @@ public: { metadataGetMode_ = true; } + + void setTcpPort(uint16_t port) + { + tcpPort_ = port; + } }; typedef SharedHandle DefaultBtInteractiveHandle; diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc index 0d7a3059..6c176999 100644 --- a/src/PeerInteractionCommand.cc +++ b/src/PeerInteractionCommand.cc @@ -75,6 +75,7 @@ #include "bittorrent_helper.h" #include "UTMetadataRequestFactory.h" #include "UTMetadataRequestTracker.h" +#include "BtRegistry.h" namespace aria2 { @@ -234,6 +235,7 @@ PeerInteractionCommand::PeerInteractionCommand } btInteractive->setUTMetadataRequestFactory(utMetadataRequestFactory); btInteractive->setUTMetadataRequestTracker(utMetadataRequestTracker); + btInteractive->setTcpPort(e->getBtRegistry()->getTcpPort()); if(metadataGetMode) { btInteractive->enableMetadataGetMode(); } diff --git a/src/PeerListenCommand.cc b/src/PeerListenCommand.cc index 28b7e2d4..526c9ca9 100644 --- a/src/PeerListenCommand.cc +++ b/src/PeerListenCommand.cc @@ -52,28 +52,16 @@ namespace aria2 { -unsigned int PeerListenCommand::numInstance_ = 0; - -PeerListenCommand* PeerListenCommand::instance_ = 0; - -PeerListenCommand* PeerListenCommand::instance6_ = 0; - PeerListenCommand::PeerListenCommand (cuid_t cuid, DownloadEngine* e, int family) : Command(cuid), e_(e), - family_(family), - lowestSpeedLimit_(20*1024) -{ - ++numInstance_; -} + family_(family) +{} -PeerListenCommand::~PeerListenCommand() -{ - --numInstance_; -} +PeerListenCommand::~PeerListenCommand() {} bool PeerListenCommand::bindPort(uint16_t& port, IntSequence& seq) { @@ -152,21 +140,4 @@ bool PeerListenCommand::execute() { return false; } -PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e, int family) -{ - if(family == AF_INET) { - if(!instance_) { - instance_ = new PeerListenCommand(e->newCUID(), e, family); - } - return instance_; - } else if(family == AF_INET6) { - if(!instance6_) { - instance6_ = new PeerListenCommand(e->newCUID(), e, family); - } - return instance6_; - } else { - return 0; - } -} - } // namespace aria2 diff --git a/src/PeerListenCommand.h b/src/PeerListenCommand.h index 7e0b1d4d..8d607c3d 100644 --- a/src/PeerListenCommand.h +++ b/src/PeerListenCommand.h @@ -49,13 +49,6 @@ private: DownloadEngine* e_; int family_; SharedHandle socket_; - unsigned int lowestSpeedLimit_; - - static unsigned int numInstance_; - - static PeerListenCommand* instance_; - - static PeerListenCommand* instance6_; public: PeerListenCommand(cuid_t cuid, DownloadEngine* e, int family); @@ -71,19 +64,6 @@ public: // Returns binded port uint16_t getPort() const; - - void setLowestSpeedLimit(unsigned int speed) - { - lowestSpeedLimit_ = speed; - } - - static PeerListenCommand* getInstance(DownloadEngine* e, int family); - - static unsigned int getNumInstance() - { - return numInstance_; - } - }; } // namespace aria2 diff --git a/test/DefaultBtAnnounceTest.cc b/test/DefaultBtAnnounceTest.cc index caccd370..a371fca2 100644 --- a/test/DefaultBtAnnounceTest.cc +++ b/test/DefaultBtAnnounceTest.cc @@ -72,7 +72,6 @@ public: peerStorage_->setStat(stat); btRuntime_.reset(new BtRuntime()); - btRuntime_->setListenPort(6989); } void tearDown() @@ -150,6 +149,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce() btAnnounce.setPeerStorage(peerStorage_); btAnnounce.setBtRuntime(btRuntime_); btAnnounce.setRandomizer(SharedHandle(new FixedNumberRandomizer())); + btAnnounce.setTcpPort(6989); CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl()); @@ -198,6 +198,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl() btAnnounce.setPeerStorage(peerStorage_); btAnnounce.setBtRuntime(btRuntime_); btAnnounce.setRandomizer(SharedHandle(new FixedNumberRandomizer())); + btAnnounce.setTcpPort(6989); CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl()); @@ -229,6 +230,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl_withQuery() btAnnounce.setPeerStorage(peerStorage_); btAnnounce.setBtRuntime(btRuntime_); btAnnounce.setRandomizer(SharedHandle(new FixedNumberRandomizer())); + btAnnounce.setTcpPort(6989); CPPUNIT_ASSERT_EQUAL (std::string("http://localhost/announce?k=v&" @@ -252,6 +254,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl_externalIP() btAnnounce.setPeerStorage(peerStorage_); btAnnounce.setBtRuntime(btRuntime_); btAnnounce.setRandomizer(SharedHandle(new FixedNumberRandomizer())); + btAnnounce.setTcpPort(6989); CPPUNIT_ASSERT_EQUAL (std::string("http://localhost/announce?" @@ -275,6 +278,7 @@ void DefaultBtAnnounceTest::testIsAllAnnounceFailed() btAnnounce.setPeerStorage(peerStorage_); btAnnounce.setBtRuntime(btRuntime_); btAnnounce.setRandomizer(SharedHandle(new FixedNumberRandomizer())); + btAnnounce.setTcpPort(6989); CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl()); @@ -307,6 +311,7 @@ void DefaultBtAnnounceTest::testURLOrderInStoppedEvent() btAnnounce.setPeerStorage(peerStorage_); btAnnounce.setBtRuntime(btRuntime_); btAnnounce.setRandomizer(SharedHandle(new FixedNumberRandomizer())); + btAnnounce.setTcpPort(6989); CPPUNIT_ASSERT_EQUAL(std::string("http://localhost1/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl()); @@ -337,6 +342,7 @@ void DefaultBtAnnounceTest::testURLOrderInCompletedEvent() btAnnounce.setPeerStorage(peerStorage_); btAnnounce.setBtRuntime(btRuntime_); btAnnounce.setRandomizer(SharedHandle(new FixedNumberRandomizer())); + btAnnounce.setTcpPort(6989); CPPUNIT_ASSERT_EQUAL(std::string("http://localhost1/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl()); diff --git a/test/MockBtAnnounce.h b/test/MockBtAnnounce.h index c5a6df09..b1493e32 100644 --- a/test/MockBtAnnounce.h +++ b/test/MockBtAnnounce.h @@ -58,6 +58,8 @@ public: virtual void overrideMinInterval(time_t interval) {} + virtual void setTcpPort(uint16_t port) {} + void setPeerId(const std::string& peerId) { this->peerId = peerId; } diff --git a/test/MockDHTTaskFactory.h b/test/MockDHTTaskFactory.h index 53ce2fed..fee36193 100644 --- a/test/MockDHTTaskFactory.h +++ b/test/MockDHTTaskFactory.h @@ -36,7 +36,7 @@ public: virtual SharedHandle createPeerLookupTask(const SharedHandle& ctx, - const SharedHandle& btRuntime, + uint16_t tcpPort, const SharedHandle& peerStorage) { return SharedHandle();