diff options
Diffstat (limited to 'src')
103 files changed, 555 insertions, 713 deletions
diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index 5736180a..e3f0a2f5 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -48,7 +48,6 @@ #include "DownloadFailureException.h" #include "CreateRequestCommand.h" #include "InitiateConnectionCommandFactory.h" -#include "SleepCommand.h" #include "StreamCheckIntegrityEntry.h" #include "PieceStorage.h" #include "SocketCore.h" @@ -133,11 +132,10 @@ void AbstractCommand::useFasterRequest fasterRequest->getPort())); // Cancel current Request object and use faster one. fileEntry_->removeRequest(req_); - Command* command = - InitiateConnectionCommandFactory::createInitiateConnectionCommand - (getCuid(), fasterRequest, fileEntry_, requestGroup_, e_); e_->setNoWait(true); - e_->addCommand(command); + e_->addCommand(InitiateConnectionCommandFactory:: + createInitiateConnectionCommand + (getCuid(), fasterRequest, fileEntry_, requestGroup_, e_)); } bool AbstractCommand::execute() { @@ -301,7 +299,7 @@ bool AbstractCommand::execute() { } throw DL_RETRY_EX2(EX_TIME_OUT, error_code::TIME_OUT); } - e_->addCommand(this); + addCommandSelf(); return false; } } catch(DlAbortEx& err) { @@ -390,10 +388,10 @@ void AbstractCommand::tryReserved() { } A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Trying reserved/pooled request.", getCuid())); - std::vector<Command*> commands; + std::vector<std::unique_ptr<Command>> commands; requestGroup_->createNextCommand(commands, e_, 1); e_->setNoWait(true); - e_->addCommand(commands); + e_->addCommand(std::move(commands)); } bool AbstractCommand::prepareForRetry(time_t wait) { @@ -415,16 +413,16 @@ bool AbstractCommand::prepareForRetry(time_t wait) { } } - Command* command = new CreateRequestCommand(getCuid(), requestGroup_, e_); + auto command = make_unique<CreateRequestCommand>(getCuid(), + requestGroup_, e_); if(wait == 0) { e_->setNoWait(true); - e_->addCommand(command); } else { // We don't use wait so that Command can be executed by // DownloadEngine::setRefreshInterval(0). command->setStatus(Command::STATUS_INACTIVE); - e_->addCommand(command); } + e_->addCommand(std::move(command)); return true; } @@ -774,11 +772,9 @@ std::string AbstractCommand::resolveHostname void AbstractCommand::prepareForNextAction (const std::shared_ptr<CheckIntegrityEntry>& checkEntry) { - std::vector<Command*>* commands = new std::vector<Command*>(); - auto_delete_container<std::vector<Command*> > commandsDel(commands); - requestGroup_->processCheckIntegrityEntry(*commands, checkEntry, e_); - e_->addCommand(*commands); - commands->clear(); + std::vector<std::unique_ptr<Command>> commands; + requestGroup_->processCheckIntegrityEntry(commands, checkEntry, e_); + e_->addCommand(std::move(commands)); e_->setNoWait(true); } @@ -796,11 +792,10 @@ bool AbstractCommand::checkIfConnectionEstablished A2_LOG_INFO(fmt(MSG_CONNECT_FAILED_AND_RETRY, getCuid(), connectedAddr.c_str(), connectedPort)); - Command* command = - InitiateConnectionCommandFactory::createInitiateConnectionCommand - (getCuid(), req_, fileEntry_, requestGroup_, e_); e_->setNoWait(true); - e_->addCommand(command); + e_->addCommand(InitiateConnectionCommandFactory:: + createInitiateConnectionCommand + (getCuid(), req_, fileEntry_, requestGroup_, e_)); return false; } e_->removeCachedIPAddress(connectedHostname, connectedPort); @@ -889,4 +884,9 @@ void AbstractCommand::checkSocketRecvBuffer() } } +void AbstractCommand::addCommandSelf() +{ + e_->addCommand(std::unique_ptr<Command>(this)); +} + } // namespace aria2 diff --git a/src/AbstractCommand.h b/src/AbstractCommand.h index a8204b75..2a34502f 100644 --- a/src/AbstractCommand.h +++ b/src/AbstractCommand.h @@ -222,6 +222,7 @@ public: void checkSocketRecvBuffer(); + void addCommandSelf(); protected: virtual bool prepareForRetry(time_t wait); virtual void onAbort(); diff --git a/src/AbstractHttpServerResponseCommand.cc b/src/AbstractHttpServerResponseCommand.cc index e83c4c1e..fe3fbb34 100644 --- a/src/AbstractHttpServerResponseCommand.cc +++ b/src/AbstractHttpServerResponseCommand.cc @@ -126,7 +126,7 @@ bool AbstractHttpServerResponseCommand::execute() return true; } else { updateReadWriteCheck(); - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } } diff --git a/src/AbstractProxyRequestCommand.cc b/src/AbstractProxyRequestCommand.cc index 4d511883..4e8d83ac 100644 --- a/src/AbstractProxyRequestCommand.cc +++ b/src/AbstractProxyRequestCommand.cc @@ -88,7 +88,7 @@ bool AbstractProxyRequestCommand::executeInternal() { return true; } else { setWriteCheckSocket(getSocket()); - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } diff --git a/src/AbstractProxyRequestCommand.h b/src/AbstractProxyRequestCommand.h index b54b7d31..68fd8af1 100644 --- a/src/AbstractProxyRequestCommand.h +++ b/src/AbstractProxyRequestCommand.h @@ -70,7 +70,7 @@ public: virtual ~AbstractProxyRequestCommand(); - virtual Command* getNextCommand() = 0; + virtual std::unique_ptr<Command> getNextCommand() = 0; }; } // namespace aria2 diff --git a/src/AbstractProxyResponseCommand.cc b/src/AbstractProxyResponseCommand.cc index c490f020..14461ea7 100644 --- a/src/AbstractProxyResponseCommand.cc +++ b/src/AbstractProxyResponseCommand.cc @@ -67,7 +67,7 @@ bool AbstractProxyResponseCommand::executeInternal() { std::shared_ptr<HttpResponse> httpResponse = httpConnection_->receiveResponse(); if(!httpResponse) { // the server has not responded our request yet. - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } if(httpResponse->getStatusCode() != 200) { diff --git a/src/AbstractProxyResponseCommand.h b/src/AbstractProxyResponseCommand.h index 4c85fbcc..589d85fb 100644 --- a/src/AbstractProxyResponseCommand.h +++ b/src/AbstractProxyResponseCommand.h @@ -64,7 +64,7 @@ public: virtual ~AbstractProxyResponseCommand(); - virtual Command* getNextCommand() = 0; + virtual std::unique_ptr<Command> getNextCommand() = 0; }; } // namespace aria2 diff --git a/src/ActivePeerConnectionCommand.cc b/src/ActivePeerConnectionCommand.cc index 07ae4d5f..311daf1a 100644 --- a/src/ActivePeerConnectionCommand.cc +++ b/src/ActivePeerConnectionCommand.cc @@ -123,7 +123,7 @@ bool ActivePeerConnectionCommand::execute() { } } } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } @@ -136,12 +136,11 @@ void ActivePeerConnectionCommand::makeNewConnections(int num) if(!peer) { break; } - PeerInitiateConnectionCommand* command; - command = new PeerInitiateConnectionCommand(ncuid, requestGroup_, peer, e_, - btRuntime_); + auto command = make_unique<PeerInitiateConnectionCommand> + (ncuid, requestGroup_, peer, e_, btRuntime_); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); - e_->addCommand(command); + e_->addCommand(std::move(command)); A2_LOG_INFO(fmt(MSG_CONNECTING_TO_PEER, getCuid(), peer->getIPAddress().c_str())); } diff --git a/src/BackupIPv4ConnectCommand.cc b/src/BackupIPv4ConnectCommand.cc index 5c61434b..04aa4e22 100644 --- a/src/BackupIPv4ConnectCommand.cc +++ b/src/BackupIPv4ConnectCommand.cc @@ -133,7 +133,7 @@ bool BackupIPv4ConnectCommand::execute() retval = true; } if(!retval) { - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); } return retval; } diff --git a/src/BtCheckIntegrityEntry.cc b/src/BtCheckIntegrityEntry.cc index 8121d869..cc28bfdb 100644 --- a/src/BtCheckIntegrityEntry.cc +++ b/src/BtCheckIntegrityEntry.cc @@ -49,7 +49,7 @@ BtCheckIntegrityEntry::BtCheckIntegrityEntry(RequestGroup* requestGroup): BtCheckIntegrityEntry::~BtCheckIntegrityEntry() {} void BtCheckIntegrityEntry::onDownloadIncomplete -(std::vector<Command*>& commands, DownloadEngine* e) +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) { const std::shared_ptr<PieceStorage>& ps = getRequestGroup()->getPieceStorage(); ps->onDownloadIncomplete(); @@ -69,7 +69,7 @@ void BtCheckIntegrityEntry::onDownloadIncomplete } void BtCheckIntegrityEntry::onDownloadFinished -(std::vector<Command*>& commands, DownloadEngine* e) +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) { // TODO Currently,when all the checksums // are valid, then aira2 goes to seeding mode. Sometimes it is better diff --git a/src/BtCheckIntegrityEntry.h b/src/BtCheckIntegrityEntry.h index 91b140ac..f04a21d9 100644 --- a/src/BtCheckIntegrityEntry.h +++ b/src/BtCheckIntegrityEntry.h @@ -45,11 +45,11 @@ public: virtual ~BtCheckIntegrityEntry(); - virtual void onDownloadFinished(std::vector<Command*>& commands, - DownloadEngine* e); + virtual void onDownloadFinished + (std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e); - virtual void onDownloadIncomplete(std::vector<Command*>& commands, - DownloadEngine* e); + virtual void onDownloadIncomplete + (std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e); }; } // namespace aria2 diff --git a/src/BtFileAllocationEntry.cc b/src/BtFileAllocationEntry.cc index 7b96f1c4..74cc826e 100644 --- a/src/BtFileAllocationEntry.cc +++ b/src/BtFileAllocationEntry.cc @@ -56,7 +56,7 @@ BtFileAllocationEntry::BtFileAllocationEntry(RequestGroup* requestGroup): BtFileAllocationEntry::~BtFileAllocationEntry() {} void BtFileAllocationEntry::prepareForNextAction -(std::vector<Command*>& commands, DownloadEngine* e) +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) { BtSetup().setup(commands, getRequestGroup(), e, getRequestGroup()->getOption().get()); diff --git a/src/BtFileAllocationEntry.h b/src/BtFileAllocationEntry.h index a263a8ed..a4355058 100644 --- a/src/BtFileAllocationEntry.h +++ b/src/BtFileAllocationEntry.h @@ -45,8 +45,8 @@ public: virtual ~BtFileAllocationEntry(); - virtual void prepareForNextAction(std::vector<Command*>& commands, - DownloadEngine* e); + virtual void prepareForNextAction + (std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e); }; } // namespace aria2 diff --git a/src/BtSetup.cc b/src/BtSetup.cc index b3f446bd..f5a1467b 100644 --- a/src/BtSetup.cc +++ b/src/BtSetup.cc @@ -90,7 +90,7 @@ namespace aria2 { BtSetup::BtSetup() {} -void BtSetup::setup(std::vector<Command*>& commands, +void BtSetup::setup(std::vector<std::unique_ptr<Command>>& commands, RequestGroup* requestGroup, DownloadEngine* e, const Option* option) @@ -109,53 +109,50 @@ void BtSetup::setup(std::vector<Command*>& commands, const std::shared_ptr<BtAnnounce>& btAnnounce = btObject->btAnnounce; // commands { - TrackerWatcherCommand* c = - new TrackerWatcherCommand(e->newCUID(), requestGroup, e); + auto c = make_unique<TrackerWatcherCommand>(e->newCUID(), requestGroup, e); c->setPeerStorage(peerStorage); c->setPieceStorage(pieceStorage); c->setBtRuntime(btRuntime); c->setBtAnnounce(btAnnounce); - commands.push_back(c); + commands.push_back(std::move(c)); } if(!metadataGetMode) { - PeerChokeCommand* c = - new PeerChokeCommand(e->newCUID(), e); + auto c = make_unique<PeerChokeCommand>(e->newCUID(), e); c->setPeerStorage(peerStorage); c->setBtRuntime(btRuntime); - commands.push_back(c); + commands.push_back(std::move(c)); } { - ActivePeerConnectionCommand* c = - new ActivePeerConnectionCommand(e->newCUID(), requestGroup, e, - metadataGetMode?2:10); + auto c = make_unique<ActivePeerConnectionCommand> + (e->newCUID(), requestGroup, e, metadataGetMode?2:10); c->setBtRuntime(btRuntime); c->setPieceStorage(pieceStorage); c->setPeerStorage(peerStorage); c->setBtAnnounce(btAnnounce); - commands.push_back(c); + commands.push_back(std::move(c)); } if(metadataGetMode || !torrentAttrs->privateTorrent) { if(DHTRegistry::isInitialized()) { - DHTGetPeersCommand* command = - new DHTGetPeersCommand(e->newCUID(), requestGroup, e); + auto command = make_unique<DHTGetPeersCommand> + (e->newCUID(), requestGroup, e); command->setTaskQueue(DHTRegistry::getData().taskQueue); command->setTaskFactory(DHTRegistry::getData().taskFactory); command->setBtRuntime(btRuntime); command->setPeerStorage(peerStorage); - commands.push_back(command); + commands.push_back(std::move(command)); } if(DHTRegistry::isInitialized6()) { - DHTGetPeersCommand* command = - new DHTGetPeersCommand(e->newCUID(), requestGroup, e); + auto command = make_unique<DHTGetPeersCommand> + (e->newCUID(), requestGroup, e); command->setTaskQueue(DHTRegistry::getData6().taskQueue); command->setTaskFactory(DHTRegistry::getData6().taskFactory); command->setBtRuntime(btRuntime); command->setPeerStorage(peerStorage); - commands.push_back(command); + commands.push_back(std::move(command)); } } if(!metadataGetMode) { @@ -178,19 +175,19 @@ void BtSetup::setup(std::vector<Command*>& commands, } } if(!unionCri->getSeedCriterion().empty()) { - SeedCheckCommand* c = - new SeedCheckCommand(e->newCUID(), requestGroup, e, unionCri); + auto c = make_unique<SeedCheckCommand> + (e->newCUID(), requestGroup, e, unionCri); c->setPieceStorage(pieceStorage); c->setBtRuntime(btRuntime); - commands.push_back(c); + commands.push_back(std::move(c)); } } 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* command = - new PeerListenCommand(e->newCUID(), e, families[i]); + auto command = make_unique<PeerListenCommand> + (e->newCUID(), e, families[i]); bool ret; uint16_t port; if(btReg->getTcpPort()) { @@ -207,9 +204,7 @@ void BtSetup::setup(std::vector<Command*>& commands, if(ret) { btReg->setTcpPort(port); // Add command to DownloadEngine directly. - e->addCommand(command); - } else { - delete command; + e->addCommand(std::move(command)); } } if(btReg->getTcpPort() == 0) { @@ -252,9 +247,8 @@ void BtSetup::setup(std::vector<Command*>& commands, " localAddr=%s", LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT, receiver->getLocalAddress().c_str())); - LpdReceiveMessageCommand* cmd = - new LpdReceiveMessageCommand(e->newCUID(), receiver, e); - e->addCommand(cmd); + e->addCommand(make_unique<LpdReceiveMessageCommand> + (e->newCUID(), receiver, e)); } else { A2_LOG_INFO("LpdMessageReceiver not initialized."); } @@ -271,10 +265,10 @@ void BtSetup::setup(std::vector<Command*>& commands, if(dispatcher->init(btReg->getLpdMessageReceiver()->getLocalAddress(), /*ttl*/1, /*loop*/1)) { A2_LOG_INFO("LpdMessageDispatcher initialized."); - LpdDispatchMessageCommand* cmd = - new LpdDispatchMessageCommand(e->newCUID(), dispatcher, e); + auto cmd = make_unique<LpdDispatchMessageCommand> + (e->newCUID(), dispatcher, e); cmd->setBtRuntime(btRuntime); - e->addCommand(cmd); + e->addCommand(std::move(cmd)); } else { A2_LOG_INFO("LpdMessageDispatcher not initialized."); } @@ -282,11 +276,11 @@ void BtSetup::setup(std::vector<Command*>& commands, } time_t btStopTimeout = option->getAsInt(PREF_BT_STOP_TIMEOUT); if(btStopTimeout > 0) { - BtStopDownloadCommand* stopDownloadCommand = - new BtStopDownloadCommand(e->newCUID(), requestGroup, e, btStopTimeout); + auto stopDownloadCommand = make_unique<BtStopDownloadCommand> + (e->newCUID(), requestGroup, e, btStopTimeout); stopDownloadCommand->setBtRuntime(btRuntime); stopDownloadCommand->setPieceStorage(pieceStorage); - commands.push_back(stopDownloadCommand); + commands.push_back(std::move(stopDownloadCommand)); } btRuntime->setReady(true); } diff --git a/src/BtSetup.h b/src/BtSetup.h index e44dea69..865545ce 100644 --- a/src/BtSetup.h +++ b/src/BtSetup.h @@ -36,7 +36,9 @@ #define D_BT_SETUP_H #include "common.h" + #include <vector> +#include <memory> namespace aria2 { @@ -49,7 +51,7 @@ class BtSetup { public: BtSetup(); - void setup(std::vector<Command*>& commands, + void setup(std::vector<std::unique_ptr<Command>>& commands, RequestGroup* requestGroup, DownloadEngine* e, const Option* option); diff --git a/src/CheckIntegrityCommand.cc b/src/CheckIntegrityCommand.cc index e2472496..ddc9b59f 100644 --- a/src/CheckIntegrityCommand.cc +++ b/src/CheckIntegrityCommand.cc @@ -74,25 +74,21 @@ bool CheckIntegrityCommand::executeInternal() A2_LOG_NOTICE (fmt(MSG_VERIFICATION_SUCCESSFUL, getRequestGroup()->getDownloadContext()->getBasePath().c_str())); - std::vector<Command*>* commands = new std::vector<Command*>(); - auto_delete_container<std::vector<Command*> > commandsDel(commands); - entry_->onDownloadFinished(*commands, getDownloadEngine()); - getDownloadEngine()->addCommand(*commands); - commands->clear(); + std::vector<std::unique_ptr<Command>> commands; + entry_->onDownloadFinished(commands, getDownloadEngine()); + getDownloadEngine()->addCommand(std::move(commands)); } else { A2_LOG_ERROR (fmt(MSG_VERIFICATION_FAILED, getRequestGroup()->getDownloadContext()->getBasePath().c_str())); - std::vector<Command*>* commands = new std::vector<Command*>(); - auto_delete_container<std::vector<Command*> > commandsDel(commands); - entry_->onDownloadIncomplete(*commands, getDownloadEngine()); - getDownloadEngine()->addCommand(*commands); - commands->clear(); + std::vector<std::unique_ptr<Command>> commands; + entry_->onDownloadIncomplete(commands, getDownloadEngine()); + getDownloadEngine()->addCommand(std::move(commands)); } getDownloadEngine()->setNoWait(true); return true; } else { - getDownloadEngine()->addCommand(this); + getDownloadEngine()->addCommand(std::unique_ptr<Command>(this)); return false; } } diff --git a/src/CheckIntegrityDispatcherCommand.cc b/src/CheckIntegrityDispatcherCommand.cc index ed176cf6..7a5517e9 100644 --- a/src/CheckIntegrityDispatcherCommand.cc +++ b/src/CheckIntegrityDispatcherCommand.cc @@ -52,13 +52,13 @@ CheckIntegrityDispatcherCommand::CheckIntegrityDispatcherCommand setStatusRealtime(); } -Command* CheckIntegrityDispatcherCommand::createCommand +std::unique_ptr<Command> CheckIntegrityDispatcherCommand::createCommand (const std::shared_ptr<CheckIntegrityEntry>& entry) { cuid_t newCUID = getDownloadEngine()->newCUID(); A2_LOG_INFO(fmt("CUID#%" PRId64 " - Dispatching CheckIntegrityCommand CUID#%" PRId64 ".", getCuid(), newCUID)); - return new CheckIntegrityCommand + return make_unique<CheckIntegrityCommand> (newCUID, entry->getRequestGroup(), getDownloadEngine(), entry); } diff --git a/src/CheckIntegrityDispatcherCommand.h b/src/CheckIntegrityDispatcherCommand.h index 4072bba1..40753452 100644 --- a/src/CheckIntegrityDispatcherCommand.h +++ b/src/CheckIntegrityDispatcherCommand.h @@ -50,7 +50,7 @@ public: const std::shared_ptr<CheckIntegrityMan>& checkMan, DownloadEngine* e); protected: - virtual Command* createCommand + virtual std::unique_ptr<Command> createCommand (const std::shared_ptr<CheckIntegrityEntry>& entry); }; diff --git a/src/CheckIntegrityEntry.cc b/src/CheckIntegrityEntry.cc index 57252118..93a3fc20 100644 --- a/src/CheckIntegrityEntry.cc +++ b/src/CheckIntegrityEntry.cc @@ -45,8 +45,8 @@ namespace aria2 { CheckIntegrityEntry::CheckIntegrityEntry(RequestGroup* requestGroup, - Command* nextCommand): - RequestGroupEntry(requestGroup, nextCommand) + std::unique_ptr<Command> nextCommand): + RequestGroupEntry(requestGroup, std::move(nextCommand)) {} CheckIntegrityEntry::~CheckIntegrityEntry() {} @@ -85,7 +85,7 @@ void CheckIntegrityEntry::cutTrailingGarbage() } void CheckIntegrityEntry::proceedFileAllocation -(std::vector<Command*>& commands, +(std::vector<std::unique_ptr<Command>>& commands, const std::shared_ptr<FileAllocationEntry>& entry, DownloadEngine* e) { diff --git a/src/CheckIntegrityEntry.h b/src/CheckIntegrityEntry.h index dc60bfbc..5d6b2419 100644 --- a/src/CheckIntegrityEntry.h +++ b/src/CheckIntegrityEntry.h @@ -55,11 +55,13 @@ private: protected: void setValidator(const std::shared_ptr<IteratableValidator>& validator); - void proceedFileAllocation(std::vector<Command*>& commands, + void proceedFileAllocation(std::vector<std::unique_ptr<Command>>& commands, const std::shared_ptr<FileAllocationEntry>& entry, DownloadEngine* e); public: - CheckIntegrityEntry(RequestGroup* requestGroup, Command* nextCommand = 0); + CheckIntegrityEntry(RequestGroup* requestGroup, + std::unique_ptr<Command> nextCommand = + std::unique_ptr<Command>()); virtual ~CheckIntegrityEntry(); @@ -75,11 +77,13 @@ public: virtual void initValidator() = 0; - virtual void onDownloadFinished(std::vector<Command*>& commands, - DownloadEngine* e) = 0; + virtual void onDownloadFinished + (std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e) = 0; - virtual void onDownloadIncomplete(std::vector<Command*>& commands, - DownloadEngine* e) = 0; + virtual void onDownloadIncomplete + (std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e) = 0; void cutTrailingGarbage(); }; diff --git a/src/ChecksumCheckIntegrityEntry.cc b/src/ChecksumCheckIntegrityEntry.cc index 34e72635..cb6b33fb 100644 --- a/src/ChecksumCheckIntegrityEntry.cc +++ b/src/ChecksumCheckIntegrityEntry.cc @@ -44,8 +44,9 @@ namespace aria2 { -ChecksumCheckIntegrityEntry::ChecksumCheckIntegrityEntry(RequestGroup* requestGroup, Command* nextCommand): - CheckIntegrityEntry(requestGroup, nextCommand), +ChecksumCheckIntegrityEntry::ChecksumCheckIntegrityEntry +(RequestGroup* requestGroup, std::unique_ptr<Command> nextCommand): + CheckIntegrityEntry(requestGroup, std::move(nextCommand)), redownload_(false) {} ChecksumCheckIntegrityEntry::~ChecksumCheckIntegrityEntry() {} @@ -68,12 +69,12 @@ void ChecksumCheckIntegrityEntry::initValidator() void ChecksumCheckIntegrityEntry::onDownloadFinished -(std::vector<Command*>& commands, DownloadEngine* e) +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) {} void ChecksumCheckIntegrityEntry::onDownloadIncomplete -(std::vector<Command*>& commands, DownloadEngine* e) +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) { if(redownload_) { std::shared_ptr<FileAllocationEntry> entry diff --git a/src/ChecksumCheckIntegrityEntry.h b/src/ChecksumCheckIntegrityEntry.h index cb1fcbe0..2c8ce041 100644 --- a/src/ChecksumCheckIntegrityEntry.h +++ b/src/ChecksumCheckIntegrityEntry.h @@ -44,7 +44,9 @@ class ChecksumCheckIntegrityEntry:public CheckIntegrityEntry private: bool redownload_; public: - ChecksumCheckIntegrityEntry(RequestGroup* requestGroup, Command* nextCommand = 0); + ChecksumCheckIntegrityEntry(RequestGroup* requestGroup, + std::unique_ptr<Command> nextCommand = + std::unique_ptr<Command>()); virtual ~ChecksumCheckIntegrityEntry(); @@ -52,11 +54,13 @@ public: virtual void initValidator(); - virtual void onDownloadFinished(std::vector<Command*>& commands, - DownloadEngine* e); + virtual void onDownloadFinished + (std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e); - virtual void onDownloadIncomplete(std::vector<Command*>& commands, - DownloadEngine* e); + virtual void onDownloadIncomplete + (std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e); void setRedownload(bool redownload) { diff --git a/src/CreateRequestCommand.cc b/src/CreateRequestCommand.cc index 1845af7c..90cbc556 100644 --- a/src/CreateRequestCommand.cc +++ b/src/CreateRequestCommand.cc @@ -105,16 +105,15 @@ bool CreateRequestCommand::executeInternal() // counted (1 for pooled and another one in this command) and // AbstractCommand::execute() will behave badly. resetRequest(); - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } - Command* command = - InitiateConnectionCommandFactory::createInitiateConnectionCommand - (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), - getDownloadEngine()); getDownloadEngine()->setNoWait(true); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand + (InitiateConnectionCommandFactory::createInitiateConnectionCommand + (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), + getDownloadEngine())); return true; } diff --git a/src/DHTEntryPointNameResolveCommand.cc b/src/DHTEntryPointNameResolveCommand.cc index 05e68a11..4b020602 100644 --- a/src/DHTEntryPointNameResolveCommand.cc +++ b/src/DHTEntryPointNameResolveCommand.cc @@ -99,7 +99,7 @@ bool DHTEntryPointNameResolveCommand::execute() std::vector<std::string> res; int rv = resolveHostname(res, hostname); if(rv == 0) { - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } else { if(rv == 1) { diff --git a/src/DHTGetPeersCommand.cc b/src/DHTGetPeersCommand.cc index 90e95853..52686be9 100644 --- a/src/DHTGetPeersCommand.cc +++ b/src/DHTGetPeersCommand.cc @@ -126,7 +126,7 @@ bool DHTGetPeersCommand::execute() task_.reset(); } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/DHTInteractionCommand.cc b/src/DHTInteractionCommand.cc index 2e6f77ea..31f84d74 100644 --- a/src/DHTInteractionCommand.cc +++ b/src/DHTInteractionCommand.cc @@ -138,7 +138,7 @@ bool DHTInteractionCommand::execute() udpTrackerClient_->requestFail(UDPT_ERR_NETWORK); } } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/DHTSetup.cc b/src/DHTSetup.cc index f2842535..5dbb8ec3 100644 --- a/src/DHTSetup.cc +++ b/src/DHTSetup.cc @@ -78,21 +78,17 @@ DHTSetup::DHTSetup() {} DHTSetup::~DHTSetup() {} -void DHTSetup::setup -(std::vector<Command*>& commands, DownloadEngine* e, int family) +std::vector<std::unique_ptr<Command>> DHTSetup::setup +(DownloadEngine* e, int family) { - if(family != AF_INET && family != AF_INET6) { - return; - } - if((family == AF_INET && DHTRegistry::isInitialized()) || + std::vector<std::unique_ptr<Command> > tempCommands; + if((family != AF_INET && family != AF_INET6) || + (family == AF_INET && DHTRegistry::isInitialized()) || (family == AF_INET6 && DHTRegistry::isInitialized6())) { - return; + return tempCommands; } try { - std::vector<Command*>* tempCommands = new std::vector<Command*>(); - auto_delete_container<std::vector<Command*> > commandsDel(tempCommands); // load routing table and localnode id here - std::shared_ptr<DHTNode> localNode; DHTRoutingTableDeserializer deserializer(family); @@ -240,65 +236,60 @@ void DHTSetup::setup e->getOption()->getAsInt(prefEntryPointPort)); std::vector<std::pair<std::string, uint16_t> > entryPoints; entryPoints.push_back(addr); - DHTEntryPointNameResolveCommand* command = - new DHTEntryPointNameResolveCommand(e->newCUID(), e, entryPoints); + auto command = make_unique<DHTEntryPointNameResolveCommand> + (e->newCUID(), e, entryPoints); command->setBootstrapEnabled(true); command->setTaskQueue(taskQueue); command->setTaskFactory(taskFactory); command->setRoutingTable(routingTable); command->setLocalNode(localNode); - tempCommands->push_back(command); + tempCommands.push_back(std::move(command)); } } else { A2_LOG_INFO("No DHT entry point specified."); } { - DHTInteractionCommand* command = - new DHTInteractionCommand(e->newCUID(), e); + auto command = make_unique<DHTInteractionCommand>(e->newCUID(), e); command->setMessageDispatcher(dispatcher); command->setMessageReceiver(receiver); command->setTaskQueue(taskQueue); command->setReadCheckSocket(connection->getSocket()); command->setConnection(connection); command->setUDPTrackerClient(udpTrackerClient); - tempCommands->push_back(command); + tempCommands.push_back(std::move(command)); } { - DHTTokenUpdateCommand* command = - new DHTTokenUpdateCommand(e->newCUID(), e, DHT_TOKEN_UPDATE_INTERVAL); + auto command = make_unique<DHTTokenUpdateCommand> + (e->newCUID(), e, DHT_TOKEN_UPDATE_INTERVAL); command->setTokenTracker(tokenTracker); - tempCommands->push_back(command); + tempCommands.push_back(std::move(command)); } { - DHTBucketRefreshCommand* command = - new DHTBucketRefreshCommand(e->newCUID(), e, - DHT_BUCKET_REFRESH_CHECK_INTERVAL); + auto command = make_unique<DHTBucketRefreshCommand> + (e->newCUID(), e, DHT_BUCKET_REFRESH_CHECK_INTERVAL); command->setTaskQueue(taskQueue); command->setRoutingTable(routingTable); command->setTaskFactory(taskFactory); - tempCommands->push_back(command); + tempCommands.push_back(std::move(command)); } { - DHTPeerAnnounceCommand* command = - new DHTPeerAnnounceCommand(e->newCUID(), e, - DHT_PEER_ANNOUNCE_CHECK_INTERVAL); + auto command = make_unique<DHTPeerAnnounceCommand> + (e->newCUID(), e, DHT_PEER_ANNOUNCE_CHECK_INTERVAL); command->setPeerAnnounceStorage(peerAnnounceStorage); - tempCommands->push_back(command); + tempCommands.push_back(std::move(command)); } { - DHTAutoSaveCommand* command = - new DHTAutoSaveCommand(e->newCUID(), e, family, 30*60); + auto command = make_unique<DHTAutoSaveCommand> + (e->newCUID(), e, family, 30*60); command->setLocalNode(localNode); command->setRoutingTable(routingTable); - tempCommands->push_back(command); + tempCommands.push_back(std::move(command)); } if(family == AF_INET) { DHTRegistry::setInitialized(true); } else { DHTRegistry::setInitialized6(true); } - commands.insert(commands.end(), tempCommands->begin(), tempCommands->end()); - tempCommands->clear(); if(e->getBtRegistry()->getUdpPort() == 0) { // We assign port last so that no exception gets in the way e->getBtRegistry()->setUdpPort(port); @@ -307,6 +298,7 @@ void DHTSetup::setup A2_LOG_ERROR_EX(fmt("Exception caught while initializing DHT functionality." " DHT is disabled."), ex); + tempCommands.clear(); if(family == AF_INET) { DHTRegistry::clearData(); e->getBtRegistry()->setUDPTrackerClient @@ -315,6 +307,7 @@ void DHTSetup::setup DHTRegistry::clearData6(); } } + return tempCommands; } } // namespace aria2 diff --git a/src/DHTSetup.h b/src/DHTSetup.h index 1d111fa0..93384590 100644 --- a/src/DHTSetup.h +++ b/src/DHTSetup.h @@ -36,7 +36,9 @@ #define D_DHT_SETUP_H #include "common.h" + #include <vector> +#include <memory> namespace aria2 { @@ -49,7 +51,7 @@ public: ~DHTSetup(); - void setup(std::vector<Command*>& commands, DownloadEngine* e, int family); + std::vector<std::unique_ptr<Command>> setup(DownloadEngine* e, int family); }; } // namespace aria2 diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index b6d7e1cc..c7cc5f8c 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -143,7 +143,7 @@ void flushWrDiskCacheEntry(WrDiskCache* wrDiskCache, bool DownloadCommand::executeInternal() { if(getDownloadEngine()->getRequestGroupMan()->doesOverallDownloadSpeedExceed() || getRequestGroup()->doesDownloadSpeedExceed()) { - getDownloadEngine()->addCommand(this); + addCommandSelf(); disableReadCheckSocket(); return false; } @@ -293,7 +293,7 @@ bool DownloadCommand::executeInternal() { checkLowestDownloadSpeed(); setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite()); checkSocketRecvBuffer(); - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } @@ -368,7 +368,7 @@ bool DownloadCommand::prepareForNextSegment() { return prepareForRetry(0); } else { checkSocketRecvBuffer(); - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } else { diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 071d2340..bc315b24 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -40,6 +40,7 @@ #include <cerrno> #include <algorithm> #include <numeric> +#include <iterator> #include "StatCalc.h" #include "RequestGroup.h" @@ -107,40 +108,32 @@ DownloadEngine::DownloadEngine(const std::shared_ptr<EventPoll>& eventPoll) sessionId_.assign(&sessionId[0], & sessionId[sizeof(sessionId)]); } -namespace { -void cleanQueue(std::deque<Command*>& commands) { - std::for_each(commands.begin(), commands.end(), Deleter()); - commands.clear(); -} -} // namespace - -DownloadEngine::~DownloadEngine() { - cleanQueue(commands_); - cleanQueue(routineCommands_); +DownloadEngine::~DownloadEngine() +{ #ifdef HAVE_ARES_ADDR_NODE setAsyncDNSServers(0); #endif // HAVE_ARES_ADDR_NODE } namespace { -void executeCommand(std::deque<Command*>& commands, +void executeCommand(std::deque<std::unique_ptr<Command>>& commands, Command::STATUS statusFilter) { size_t max = commands.size(); for(size_t i = 0; i < max; ++i) { - Command* com = commands.front(); + std::unique_ptr<Command> com = std::move(commands.front()); commands.pop_front(); if(com->statusMatch(statusFilter)) { com->transitStatus(); if(com->execute()) { - delete com; - com = 0; + com.reset(); + } else { + com->clearIOEvents(); + com.release(); } } else { - commands.push_back(com); - } - if(com) { com->clearIOEvents(); + commands.push_back(std::move(com)); } } } @@ -282,9 +275,9 @@ void DownloadEngine::setNoWait(bool b) noWait_ = b; } -void DownloadEngine::addRoutineCommand(Command* command) +void DownloadEngine::addRoutineCommand(std::unique_ptr<Command> command) { - routineCommands_.push_back(command); + routineCommands_.push_back(std::move(command)); } void DownloadEngine::poolSocket(const std::string& key, @@ -561,14 +554,17 @@ void DownloadEngine::setRefreshInterval(int64_t interval) refreshInterval_ = std::min(static_cast<int64_t>(999), interval); } -void DownloadEngine::addCommand(const std::vector<Command*>& commands) +void DownloadEngine::addCommand +(std::vector<std::unique_ptr<Command>> commands) { - commands_.insert(commands_.end(), commands.begin(), commands.end()); + commands_.insert(commands_.end(), + std::make_move_iterator(std::begin(commands)), + std::make_move_iterator(std::end(commands))); } -void DownloadEngine::addCommand(Command* command) +void DownloadEngine::addCommand(std::unique_ptr<Command> command) { - commands_.push_back(command); + commands_.push_back(std::move(command)); } void DownloadEngine::setRequestGroupMan diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index 242769fe..5784b0d1 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -131,7 +131,7 @@ private: int64_t refreshInterval_; Timer lastRefresh_; - std::deque<Command*> routineCommands_; + std::deque<std::unique_ptr<Command>> routineCommands_; std::shared_ptr<CookieStorage> cookieStorage_; @@ -167,7 +167,7 @@ private: std::multimap<std::string, SocketPoolEntry>::iterator findSocketPoolEntry(const std::string& key); - std::deque<Command*> commands_; + std::deque<std::unique_ptr<Command>> commands_; std::shared_ptr<RequestGroupMan> requestGroupMan_; std::shared_ptr<FileAllocationMan> fileAllocationMan_; std::shared_ptr<CheckIntegrityMan> checkIntegrityMan_; @@ -200,9 +200,9 @@ public: Command* command); #endif // ENABLE_ASYNC_DNS - void addCommand(const std::vector<Command*>& commands); + void addCommand(std::vector<std::unique_ptr<Command>> commands); - void addCommand(Command* command); + void addCommand(std::unique_ptr<Command> command); const std::shared_ptr<RequestGroupMan>& getRequestGroupMan() const { @@ -253,7 +253,7 @@ public: void setNoWait(bool b); - void addRoutineCommand(Command* command); + void addRoutineCommand(std::unique_ptr<Command> command); void poolSocket(const std::string& ipaddr, uint16_t port, const std::string& username, diff --git a/src/DownloadEngineFactory.cc b/src/DownloadEngineFactory.cc index 3f3bad54..9d3fb85d 100644 --- a/src/DownloadEngineFactory.cc +++ b/src/DownloadEngineFactory.cc @@ -159,35 +159,38 @@ DownloadEngineFactory::newDownloadEngine e->setCheckIntegrityMan (std::shared_ptr<CheckIntegrityMan>(new CheckIntegrityMan())); #endif // ENABLE_MESSAGE_DIGEST - e->addRoutineCommand(new FillRequestGroupCommand(e->newCUID(), e.get())); - e->addRoutineCommand(new FileAllocationDispatcherCommand + e->addRoutineCommand(make_unique<FillRequestGroupCommand> + (e->newCUID(), e.get())); + e->addRoutineCommand(make_unique<FileAllocationDispatcherCommand> (e->newCUID(), e->getFileAllocationMan(), e.get())); #ifdef ENABLE_MESSAGE_DIGEST - e->addRoutineCommand(new CheckIntegrityDispatcherCommand + e->addRoutineCommand(make_unique<CheckIntegrityDispatcherCommand> (e->newCUID(), e->getCheckIntegrityMan(), e.get())); #endif // ENABLE_MESSAGE_DIGEST if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) { e->addRoutineCommand - (new AutoSaveCommand(e->newCUID(), e.get(), - op->getAsInt(PREF_AUTO_SAVE_INTERVAL))); + (make_unique<AutoSaveCommand>(e->newCUID(), e.get(), + op->getAsInt(PREF_AUTO_SAVE_INTERVAL))); } if(op->getAsInt(PREF_SAVE_SESSION_INTERVAL) > 0) { - e->addRoutineCommand - (new SaveSessionCommand(e->newCUID(), e.get(), - op->getAsInt(PREF_SAVE_SESSION_INTERVAL))); + e->addRoutineCommand(make_unique<SaveSessionCommand> + (e->newCUID(), e.get(), + op->getAsInt(PREF_SAVE_SESSION_INTERVAL))); } - e->addRoutineCommand(new HaveEraseCommand(e->newCUID(), e.get(), 10)); + e->addRoutineCommand(make_unique<HaveEraseCommand> + (e->newCUID(), e.get(), 10)); { time_t stopSec = op->getAsInt(PREF_STOP); if(stopSec > 0) { - e->addRoutineCommand(new TimedHaltCommand(e->newCUID(), e.get(), - stopSec)); + e->addRoutineCommand(make_unique<TimedHaltCommand>(e->newCUID(), e.get(), + stopSec)); } } if(op->defined(PREF_STOP_WITH_PROCESS)) { unsigned int pid = op->getAsInt(PREF_STOP_WITH_PROCESS); - e->addRoutineCommand(new WatchProcessCommand(e->newCUID(), e.get(), pid)); + e->addRoutineCommand(make_unique<WatchProcessCommand>(e->newCUID(), + e.get(), pid)); } if(op->getAsBool(PREF_ENABLE_RPC)) { bool ok = false; @@ -198,13 +201,11 @@ DownloadEngineFactory::newDownloadEngine static int families[] = { AF_INET, AF_INET6 }; size_t familiesLength = op->getAsBool(PREF_DISABLE_IPV6)?1:2; for(size_t i = 0; i < familiesLength; ++i) { - HttpListenCommand* httpListenCommand = - new HttpListenCommand(e->newCUID(), e.get(), families[i], secure); + auto httpListenCommand = make_unique<HttpListenCommand> + (e->newCUID(), e.get(), families[i], secure); if(httpListenCommand->bindPort(op->getAsInt(PREF_RPC_LISTEN_PORT))){ - e->addCommand(httpListenCommand); + e->addCommand(std::move(httpListenCommand)); ok = true; - } else { - delete httpListenCommand; } } if(!ok) { diff --git a/src/FileAllocationCommand.cc b/src/FileAllocationCommand.cc index 54c4e7a5..b76357cc 100644 --- a/src/FileAllocationCommand.cc +++ b/src/FileAllocationCommand.cc @@ -74,15 +74,13 @@ bool FileAllocationCommand::executeInternal() getRequestGroup()->getTotalLength())); getDownloadEngine()->getFileAllocationMan()->dropPickedEntry(); - std::vector<Command*>* commands = new std::vector<Command*>(); - auto_delete_container<std::vector<Command*> > commandsDel(commands); - fileAllocationEntry_->prepareForNextAction(*commands, getDownloadEngine()); - getDownloadEngine()->addCommand(*commands); - commands->clear(); + std::vector<std::unique_ptr<Command>> commands; + fileAllocationEntry_->prepareForNextAction(commands, getDownloadEngine()); + getDownloadEngine()->addCommand(std::move(commands)); getDownloadEngine()->setNoWait(true); return true; } else { - getDownloadEngine()->addCommand(this); + getDownloadEngine()->addCommand(std::unique_ptr<Command>(this)); return false; } } diff --git a/src/FileAllocationDispatcherCommand.cc b/src/FileAllocationDispatcherCommand.cc index 70e6573a..4a56b569 100644 --- a/src/FileAllocationDispatcherCommand.cc +++ b/src/FileAllocationDispatcherCommand.cc @@ -50,15 +50,13 @@ FileAllocationDispatcherCommand::FileAllocationDispatcherCommand : SequentialDispatcherCommand<FileAllocationEntry>(cuid, fileAllocMan, e) {} -Command* FileAllocationDispatcherCommand::createCommand +std::unique_ptr<Command> FileAllocationDispatcherCommand::createCommand (const std::shared_ptr<FileAllocationEntry>& entry) { cuid_t newCUID = getDownloadEngine()->newCUID(); A2_LOG_INFO(fmt(MSG_FILE_ALLOCATION_DISPATCH, newCUID)); - FileAllocationCommand* command = - new FileAllocationCommand(newCUID, entry->getRequestGroup(), - getDownloadEngine(), entry); - return command; + return make_unique<FileAllocationCommand> + (newCUID, entry->getRequestGroup(), getDownloadEngine(), entry); } } // namespace aria2 diff --git a/src/FileAllocationDispatcherCommand.h b/src/FileAllocationDispatcherCommand.h index f41fb1b7..6d374433 100644 --- a/src/FileAllocationDispatcherCommand.h +++ b/src/FileAllocationDispatcherCommand.h @@ -50,7 +50,7 @@ public: const std::shared_ptr<FileAllocationMan>& fileAllocMan, DownloadEngine* e); protected: - virtual Command* createCommand + virtual std::unique_ptr<Command> createCommand (const std::shared_ptr<FileAllocationEntry>& entry); }; diff --git a/src/FileAllocationEntry.cc b/src/FileAllocationEntry.cc index 143b0b33..4a580398 100644 --- a/src/FileAllocationEntry.cc +++ b/src/FileAllocationEntry.cc @@ -41,8 +41,9 @@ namespace aria2 { -FileAllocationEntry::FileAllocationEntry(RequestGroup* requestGroup, Command* nextCommand): - RequestGroupEntry(requestGroup, nextCommand), +FileAllocationEntry::FileAllocationEntry(RequestGroup* requestGroup, + std::unique_ptr<Command> nextCommand): + RequestGroupEntry(requestGroup, std::move(nextCommand)), fileAllocationIterator_(requestGroup->getPieceStorage()->getDiskAdaptor()->fileAllocationIterator()) {} diff --git a/src/FileAllocationEntry.h b/src/FileAllocationEntry.h index 74a455d5..6811f56f 100644 --- a/src/FileAllocationEntry.h +++ b/src/FileAllocationEntry.h @@ -52,7 +52,9 @@ class FileAllocationEntry : public RequestGroupEntry, public ProgressAwareEntry private: std::shared_ptr<FileAllocationIterator> fileAllocationIterator_; public: - FileAllocationEntry(RequestGroup* requestGroup, Command* nextCommand = 0); + FileAllocationEntry(RequestGroup* requestGroup, + std::unique_ptr<Command> nextCommand = + std::unique_ptr<Command>()); ~FileAllocationEntry(); @@ -64,8 +66,9 @@ public: void allocateChunk(); - virtual void prepareForNextAction(std::vector<Command*>& commands, - DownloadEngine* e) = 0; + virtual void prepareForNextAction + (std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e) = 0; }; } // namespace aria2 diff --git a/src/FillRequestGroupCommand.cc b/src/FillRequestGroupCommand.cc index ddfbb8b6..3aa12d0d 100644 --- a/src/FillRequestGroupCommand.cc +++ b/src/FillRequestGroupCommand.cc @@ -80,7 +80,7 @@ bool FillRequestGroupCommand::execute() return true; } } - e_->addRoutineCommand(this); + e_->addRoutineCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/FtpDownloadCommand.cc b/src/FtpDownloadCommand.cc index 81f55541..fbd3191f 100644 --- a/src/FtpDownloadCommand.cc +++ b/src/FtpDownloadCommand.cc @@ -70,10 +70,10 @@ bool FtpDownloadCommand::prepareForNextSegment() if(getOption()->getAsBool(PREF_FTP_REUSE_CONNECTION) && getFileEntry()->gtoloff(getSegments().front()->getPositionToWrite()) == getFileEntry()->getLength()) { - Command* command = new FtpFinishDownloadCommand - (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), - ftpConnection_, getDownloadEngine(), ctrlSocket_); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand + (make_unique<FtpFinishDownloadCommand> + (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), + ftpConnection_, getDownloadEngine(), ctrlSocket_)); if(getRequestGroup()->downloadFinished()) { // To run checksum checking, we had to call following function here. diff --git a/src/FtpFinishDownloadCommand.cc b/src/FtpFinishDownloadCommand.cc index 3002f631..c4599348 100644 --- a/src/FtpFinishDownloadCommand.cc +++ b/src/FtpFinishDownloadCommand.cc @@ -80,7 +80,7 @@ bool FtpFinishDownloadCommand::execute() getCheckPoint() = global::wallclock(); int status = ftpConnection_->receiveResponse(); if(status == 0) { - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } if(status == 226) { @@ -97,7 +97,7 @@ bool FtpFinishDownloadCommand::execute() A2_LOG_INFO(fmt("CUID#%" PRId64 " - Timeout before receiving transfer complete.", getCuid())); } else { - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } catch(RecoverableException& e) { diff --git a/src/FtpInitiateConnectionCommand.cc b/src/FtpInitiateConnectionCommand.cc index 9b026a89..4520d91e 100644 --- a/src/FtpInitiateConnectionCommand.cc +++ b/src/FtpInitiateConnectionCommand.cc @@ -73,12 +73,11 @@ FtpInitiateConnectionCommand::FtpInitiateConnectionCommand FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() {} -Command* FtpInitiateConnectionCommand::createNextCommand +std::unique_ptr<Command> FtpInitiateConnectionCommand::createNextCommand (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector<std::string>& resolvedAddresses, const std::shared_ptr<Request>& proxyRequest) { - Command* command; if(proxyRequest) { std::string options; std::shared_ptr<SocketCore> pooledSocket; @@ -102,13 +101,13 @@ Command* FtpInitiateConnectionCommand::createNextCommand getRequest()->setConnectedAddrInfo(hostname, addr, port); - ConnectCommand* c = new ConnectCommand(getCuid(), - getRequest(), - proxyRequest, - getFileEntry(), - getRequestGroup(), - getDownloadEngine(), - getSocket()); + auto c = make_unique<ConnectCommand>(getCuid(), + getRequest(), + proxyRequest, + getFileEntry(), + getRequestGroup(), + getDownloadEngine(), + getSocket()); if(proxyMethod == V_GET) { // Use GET for FTP via HTTP proxy. getRequest()->setMethod(Request::METHOD_GET); @@ -123,13 +122,13 @@ Command* FtpInitiateConnectionCommand::createNextCommand // Unreachable assert(0); } - setupBackupConnection(hostname, addr, port, c); - command = c; + setupBackupConnection(hostname, addr, port, c.get()); + return std::move(c); } else { setConnectedAddrInfo(getRequest(), hostname, pooledSocket); if(proxyMethod == V_TUNNEL) { // options contains "baseWorkingDir" - command = new FtpNegotiationCommand + return make_unique<FtpNegotiationCommand> (getCuid(), getRequest(), getFileEntry(), @@ -146,15 +145,15 @@ Command* FtpInitiateConnectionCommand::createNextCommand std::shared_ptr<HttpConnection> hc (new HttpConnection(getCuid(), pooledSocket, socketRecvBuffer)); - HttpRequestCommand* c = new HttpRequestCommand(getCuid(), - getRequest(), - getFileEntry(), - getRequestGroup(), - hc, - getDownloadEngine(), - pooledSocket); + auto c = make_unique<HttpRequestCommand>(getCuid(), + getRequest(), + getFileEntry(), + getRequestGroup(), + hc, + getDownloadEngine(), + pooledSocket); c->setProxyRequest(proxyRequest); - command = c; + return std::move(c); } else { // Unreachable assert(0); @@ -174,21 +173,21 @@ Command* FtpInitiateConnectionCommand::createNextCommand createSocket(); getSocket()->establishConnection(addr, port); getRequest()->setConnectedAddrInfo(hostname, addr, port); - ConnectCommand* c = new ConnectCommand(getCuid(), - getRequest(), - proxyRequest, // must be null - getFileEntry(), - getRequestGroup(), - getDownloadEngine(), - getSocket()); + auto c = make_unique<ConnectCommand>(getCuid(), + getRequest(), + proxyRequest, // must be null + getFileEntry(), + getRequestGroup(), + getDownloadEngine(), + getSocket()); std::shared_ptr<FtpNegotiationConnectChain> chain (new FtpNegotiationConnectChain()); c->setControlChain(chain); - setupBackupConnection(hostname, addr, port, c); - command = c; + setupBackupConnection(hostname, addr, port, c.get()); + return std::move(c); } else { // options contains "baseWorkingDir" - command = new FtpNegotiationCommand + auto command = make_unique<FtpNegotiationCommand> (getCuid(), getRequest(), getFileEntry(), @@ -198,9 +197,9 @@ Command* FtpInitiateConnectionCommand::createNextCommand FtpNegotiationCommand::SEQ_SEND_CWD_PREP, options); setConnectedAddrInfo(getRequest(), hostname, pooledSocket); + return std::move(command); } } - return command; } } // namespace aria2 diff --git a/src/FtpInitiateConnectionCommand.h b/src/FtpInitiateConnectionCommand.h index 46cead0d..d8c6070e 100644 --- a/src/FtpInitiateConnectionCommand.h +++ b/src/FtpInitiateConnectionCommand.h @@ -41,7 +41,7 @@ namespace aria2 { class FtpInitiateConnectionCommand : public InitiateConnectionCommand { protected: - virtual Command* createNextCommand + virtual std::unique_ptr<Command> createNextCommand (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector<std::string>& resolvedAddresses, const std::shared_ptr<Request>& proxyRequest); diff --git a/src/FtpNegotiationCommand.cc b/src/FtpNegotiationCommand.cc index 1845a116..af0f80e1 100644 --- a/src/FtpNegotiationCommand.cc +++ b/src/FtpNegotiationCommand.cc @@ -110,8 +110,7 @@ bool FtpNegotiationCommand::executeInternal() { if(sequence_ == SEQ_RETRY) { return prepareForRetry(0); } else if(sequence_ == SEQ_NEGOTIATION_COMPLETED) { - FtpDownloadCommand* command = - new FtpDownloadCommand + auto command = make_unique<FtpDownloadCommand> (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), ftp_, getDownloadEngine(), dataSocket_, getSocket()); command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME)); @@ -121,8 +120,8 @@ bool FtpNegotiationCommand::executeInternal() { getFileEntry()->removeURIWhoseHostnameIs(getRequest()->getHost()); } getRequestGroup()->getURISelector()->tuneDownloadCommand - (getFileEntry()->getRemainingUris(), command); - getDownloadEngine()->addCommand(command); + (getFileEntry()->getRemainingUris(), command.get()); + getDownloadEngine()->addCommand(std::move(command)); return true; } else if(sequence_ == SEQ_HEAD_OK || sequence_ == SEQ_DOWNLOAD_ALREADY_COMPLETED) { @@ -137,7 +136,7 @@ bool FtpNegotiationCommand::executeInternal() { } else if(sequence_ == SEQ_EXIT) { return true; } else { - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } @@ -473,7 +472,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength) poolConnection(); return false; } - checkIntegrityEntry->pushNextCommand(this); + checkIntegrityEntry->pushNextCommand(std::unique_ptr<Command>(this)); // We have to make sure that command that has Request object must // have segment after PieceStorage is initialized. See // AbstractCommand::execute() diff --git a/src/FtpNegotiationConnectChain.h b/src/FtpNegotiationConnectChain.h index 1e06a2df..65aaead7 100644 --- a/src/FtpNegotiationConnectChain.h +++ b/src/FtpNegotiationConnectChain.h @@ -47,7 +47,7 @@ struct FtpNegotiationConnectChain : public ControlChain<ConnectCommand*> { virtual ~FtpNegotiationConnectChain() {} virtual int run(ConnectCommand* t, DownloadEngine* e) { - FtpNegotiationCommand* c = new FtpNegotiationCommand + auto c = make_unique<FtpNegotiationCommand> (t->getCuid(), t->getRequest(), t->getFileEntry(), @@ -56,7 +56,7 @@ struct FtpNegotiationConnectChain : public ControlChain<ConnectCommand*> { t->getSocket()); c->setStatus(Command::STATUS_ONESHOT_REALTIME); e->setNoWait(true); - e->addCommand(c); + e->addCommand(std::move(c)); return 0; } }; diff --git a/src/FtpTunnelRequestCommand.cc b/src/FtpTunnelRequestCommand.cc index 72ea099b..6f2b2684 100644 --- a/src/FtpTunnelRequestCommand.cc +++ b/src/FtpTunnelRequestCommand.cc @@ -38,6 +38,7 @@ #include "SocketCore.h" #include "DownloadContext.h" #include "SocketRecvBuffer.h" +#include "a2functional.h" namespace aria2 { @@ -56,9 +57,9 @@ FtpTunnelRequestCommand::FtpTunnelRequestCommand FtpTunnelRequestCommand::~FtpTunnelRequestCommand() {} -Command* FtpTunnelRequestCommand::getNextCommand() +std::unique_ptr<Command> FtpTunnelRequestCommand::getNextCommand() { - return new FtpTunnelResponseCommand + return make_unique<FtpTunnelResponseCommand> (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getHttpConnection(), getDownloadEngine(), getSocket()); } diff --git a/src/FtpTunnelRequestCommand.h b/src/FtpTunnelRequestCommand.h index 2e2bb6f5..664abf99 100644 --- a/src/FtpTunnelRequestCommand.h +++ b/src/FtpTunnelRequestCommand.h @@ -52,7 +52,7 @@ public: const std::shared_ptr<SocketCore>& s); virtual ~FtpTunnelRequestCommand(); - virtual Command* getNextCommand(); + virtual std::unique_ptr<Command> getNextCommand(); }; } // namespace aria2 diff --git a/src/FtpTunnelRequestConnectChain.h b/src/FtpTunnelRequestConnectChain.h index 77b3f86e..ef38a753 100644 --- a/src/FtpTunnelRequestConnectChain.h +++ b/src/FtpTunnelRequestConnectChain.h @@ -47,7 +47,7 @@ struct FtpTunnelRequestConnectChain : public ControlChain<ConnectCommand*> { virtual ~FtpTunnelRequestConnectChain() {} virtual int run(ConnectCommand* t, DownloadEngine* e) { - FtpTunnelRequestCommand* c = new FtpTunnelRequestCommand + auto c = make_unique<FtpTunnelRequestCommand> (t->getCuid(), t->getRequest(), t->getFileEntry(), @@ -57,7 +57,7 @@ struct FtpTunnelRequestConnectChain : public ControlChain<ConnectCommand*> { t->getSocket()); c->setStatus(Command::STATUS_ONESHOT_REALTIME); e->setNoWait(true); - e->addCommand(c); + e->addCommand(std::move(c)); return 0; } }; diff --git a/src/FtpTunnelResponseCommand.cc b/src/FtpTunnelResponseCommand.cc index bc218400..ed626f0d 100644 --- a/src/FtpTunnelResponseCommand.cc +++ b/src/FtpTunnelResponseCommand.cc @@ -57,11 +57,12 @@ FtpTunnelResponseCommand::FtpTunnelResponseCommand FtpTunnelResponseCommand::~FtpTunnelResponseCommand() {} -Command* FtpTunnelResponseCommand::getNextCommand() +std::unique_ptr<Command> FtpTunnelResponseCommand::getNextCommand() { - return new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(), - getRequestGroup(), getDownloadEngine(), - getSocket()); + return make_unique<FtpNegotiationCommand> + (getCuid(), getRequest(), getFileEntry(), + getRequestGroup(), getDownloadEngine(), + getSocket()); } } // namespace aria2 diff --git a/src/FtpTunnelResponseCommand.h b/src/FtpTunnelResponseCommand.h index b80ef8ff..6b5fdbef 100644 --- a/src/FtpTunnelResponseCommand.h +++ b/src/FtpTunnelResponseCommand.h @@ -52,7 +52,7 @@ public: const std::shared_ptr<SocketCore>& s); virtual ~FtpTunnelResponseCommand(); - virtual Command* getNextCommand(); + virtual std::unique_ptr<Command> getNextCommand(); }; } // namespace aria2 diff --git a/src/HttpDownloadCommand.cc b/src/HttpDownloadCommand.cc index 3436bb58..93cca2d0 100644 --- a/src/HttpDownloadCommand.cc +++ b/src/HttpDownloadCommand.cc @@ -75,16 +75,16 @@ HttpDownloadCommand::~HttpDownloadCommand() {} bool HttpDownloadCommand::prepareForNextSegment() { bool downloadFinished = getRequestGroup()->downloadFinished(); if(getRequest()->isPipeliningEnabled() && !downloadFinished) { - HttpRequestCommand* command = - new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(), - getRequestGroup(), httpConnection_, - getDownloadEngine(), getSocket()); + auto command = make_unique<HttpRequestCommand> + (getCuid(), getRequest(), getFileEntry(), + getRequestGroup(), httpConnection_, + getDownloadEngine(), getSocket()); // Set proxy request here. aria2 sends the HTTP request specialized for // proxy. if(resolveProxyMethod(getRequest()->getProtocol()) == V_GET) { command->setProxyRequest(createProxyRequest()); } - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(std::move(command)); return true; } else { const std::string& streamFilterName = getStreamFilter()->getName(); diff --git a/src/HttpInitiateConnectionCommand.cc b/src/HttpInitiateConnectionCommand.cc index cc8b2ef1..a0572fb4 100644 --- a/src/HttpInitiateConnectionCommand.cc +++ b/src/HttpInitiateConnectionCommand.cc @@ -69,12 +69,11 @@ HttpInitiateConnectionCommand::HttpInitiateConnectionCommand HttpInitiateConnectionCommand::~HttpInitiateConnectionCommand() {} -Command* HttpInitiateConnectionCommand::createNextCommand +std::unique_ptr<Command> HttpInitiateConnectionCommand::createNextCommand (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector<std::string>& resolvedAddresses, const std::shared_ptr<Request>& proxyRequest) { - Command* command; if(proxyRequest) { std::shared_ptr<SocketCore> pooledSocket = getDownloadEngine()->popPooledSocket @@ -88,13 +87,13 @@ Command* HttpInitiateConnectionCommand::createNextCommand getSocket()->establishConnection(addr, port); getRequest()->setConnectedAddrInfo(hostname, addr, port); - ConnectCommand* c = new ConnectCommand(getCuid(), - getRequest(), - proxyRequest, - getFileEntry(), - getRequestGroup(), - getDownloadEngine(), - getSocket()); + auto c = make_unique<ConnectCommand>(getCuid(), + getRequest(), + proxyRequest, + getFileEntry(), + getRequestGroup(), + getDownloadEngine(), + getSocket()); if(proxyMethod == V_TUNNEL) { std::shared_ptr<HttpProxyRequestConnectChain> chain (new HttpProxyRequestConnectChain()); @@ -107,25 +106,25 @@ Command* HttpInitiateConnectionCommand::createNextCommand // Unreachable assert(0); } - setupBackupConnection(hostname, addr, port, c); - command = c; + setupBackupConnection(hostname, addr, port, c.get()); + return std::move(c); } else { setConnectedAddrInfo(getRequest(), hostname, pooledSocket); std::shared_ptr<SocketRecvBuffer> socketRecvBuffer (new SocketRecvBuffer(pooledSocket)); std::shared_ptr<HttpConnection> httpConnection (new HttpConnection(getCuid(), pooledSocket, socketRecvBuffer)); - HttpRequestCommand* c = new HttpRequestCommand(getCuid(), - getRequest(), - getFileEntry(), - getRequestGroup(), - httpConnection, - getDownloadEngine(), - pooledSocket); + auto c = make_unique<HttpRequestCommand>(getCuid(), + getRequest(), + getFileEntry(), + getRequestGroup(), + httpConnection, + getDownloadEngine(), + pooledSocket); if(proxyMethod == V_GET) { c->setProxyRequest(proxyRequest); } - command = c; + return std::move(c); } } else { std::shared_ptr<SocketCore> pooledSocket = @@ -138,18 +137,18 @@ Command* HttpInitiateConnectionCommand::createNextCommand getSocket()->establishConnection(addr, port); getRequest()->setConnectedAddrInfo(hostname, addr, port); - ConnectCommand* c = new ConnectCommand(getCuid(), - getRequest(), - proxyRequest, // must be null - getFileEntry(), - getRequestGroup(), - getDownloadEngine(), - getSocket()); + auto c = make_unique<ConnectCommand>(getCuid(), + getRequest(), + proxyRequest, // must be null + getFileEntry(), + getRequestGroup(), + getDownloadEngine(), + getSocket()); std::shared_ptr<HttpRequestConnectChain> chain (new HttpRequestConnectChain()); c->setControlChain(chain); - setupBackupConnection(hostname, addr, port, c); - command = c; + setupBackupConnection(hostname, addr, port, c.get()); + return std::move(c); } else { setSocket(pooledSocket); setConnectedAddrInfo(getRequest(), hostname, pooledSocket); @@ -158,16 +157,15 @@ Command* HttpInitiateConnectionCommand::createNextCommand (new SocketRecvBuffer(getSocket())); std::shared_ptr<HttpConnection> httpConnection (new HttpConnection(getCuid(), getSocket(), socketRecvBuffer)); - command = new HttpRequestCommand(getCuid(), - getRequest(), - getFileEntry(), - getRequestGroup(), - httpConnection, - getDownloadEngine(), - getSocket()); + return make_unique<HttpRequestCommand>(getCuid(), + getRequest(), + getFileEntry(), + getRequestGroup(), + httpConnection, + getDownloadEngine(), + getSocket()); } } - return command; } } // namespace aria2 diff --git a/src/HttpInitiateConnectionCommand.h b/src/HttpInitiateConnectionCommand.h index a913dc78..176ae4b4 100644 --- a/src/HttpInitiateConnectionCommand.h +++ b/src/HttpInitiateConnectionCommand.h @@ -68,7 +68,7 @@ namespace aria2 { // calling execute() returns true. class HttpInitiateConnectionCommand : public InitiateConnectionCommand { protected: - virtual Command* createNextCommand + virtual std::unique_ptr<Command> createNextCommand (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector<std::string>& resolvedAddresses, const std::shared_ptr<Request>& proxyRequest); diff --git a/src/HttpListenCommand.cc b/src/HttpListenCommand.cc index 728faa57..a81929b8 100644 --- a/src/HttpListenCommand.cc +++ b/src/HttpListenCommand.cc @@ -80,15 +80,14 @@ bool HttpListenCommand::execute() A2_LOG_INFO(fmt("RPC: Accepted the connection from %s:%u.", peerInfo.first.c_str(), peerInfo.second)); - HttpServerCommand* c = - new HttpServerCommand(e_->newCUID(), e_, socket, secure_); e_->setNoWait(true); - e_->addCommand(c); + e_->addCommand(make_unique<HttpServerCommand> + (e_->newCUID(), e_, socket, secure_)); } } catch(RecoverableException& e) { A2_LOG_DEBUG_EX(fmt(MSG_ACCEPT_FAILURE, getCuid()), e); } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/HttpProxyRequestCommand.cc b/src/HttpProxyRequestCommand.cc index 2143f398..f639ac70 100644 --- a/src/HttpProxyRequestCommand.cc +++ b/src/HttpProxyRequestCommand.cc @@ -37,6 +37,7 @@ #include "Request.h" #include "SocketCore.h" #include "SocketRecvBuffer.h" +#include "a2functional.h" namespace aria2 { @@ -55,9 +56,9 @@ HttpProxyRequestCommand::HttpProxyRequestCommand HttpProxyRequestCommand::~HttpProxyRequestCommand() {} -Command* HttpProxyRequestCommand::getNextCommand() +std::unique_ptr<Command> HttpProxyRequestCommand::getNextCommand() { - return new HttpProxyResponseCommand + return make_unique<HttpProxyResponseCommand> (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getHttpConnection(), getDownloadEngine(), getSocket()); } diff --git a/src/HttpProxyRequestCommand.h b/src/HttpProxyRequestCommand.h index ef99ddb2..682684f2 100644 --- a/src/HttpProxyRequestCommand.h +++ b/src/HttpProxyRequestCommand.h @@ -52,7 +52,7 @@ public: const std::shared_ptr<SocketCore>& s); virtual ~HttpProxyRequestCommand(); - virtual Command* getNextCommand(); + virtual std::unique_ptr<Command> getNextCommand(); }; } // namespace aria2 diff --git a/src/HttpProxyRequestConnectChain.h b/src/HttpProxyRequestConnectChain.h index 9e6e804d..59cbc4c0 100644 --- a/src/HttpProxyRequestConnectChain.h +++ b/src/HttpProxyRequestConnectChain.h @@ -47,7 +47,7 @@ struct HttpProxyRequestConnectChain : public ControlChain<ConnectCommand*> { virtual ~HttpProxyRequestConnectChain() {} virtual int run(ConnectCommand* t, DownloadEngine* e) { - HttpProxyRequestCommand* c = new HttpProxyRequestCommand + auto c = make_unique<HttpProxyRequestCommand> (t->getCuid(), t->getRequest(), t->getFileEntry(), @@ -57,7 +57,7 @@ struct HttpProxyRequestConnectChain : public ControlChain<ConnectCommand*> { t->getSocket()); c->setStatus(Command::STATUS_ONESHOT_REALTIME); e->setNoWait(true); - e->addCommand(c); + e->addCommand(std::move(c)); return 0; } }; diff --git a/src/HttpProxyResponseCommand.cc b/src/HttpProxyResponseCommand.cc index cfadc609..630d7eba 100644 --- a/src/HttpProxyResponseCommand.cc +++ b/src/HttpProxyResponseCommand.cc @@ -57,11 +57,12 @@ HttpProxyResponseCommand::HttpProxyResponseCommand HttpProxyResponseCommand::~HttpProxyResponseCommand() {} -Command* HttpProxyResponseCommand::getNextCommand() +std::unique_ptr<Command> HttpProxyResponseCommand::getNextCommand() { - return new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(), - getRequestGroup(), getHttpConnection(), - getDownloadEngine(), getSocket()); + return make_unique<HttpRequestCommand> + (getCuid(), getRequest(), getFileEntry(), + getRequestGroup(), getHttpConnection(), + getDownloadEngine(), getSocket()); } } // namespace aria2 diff --git a/src/HttpProxyResponseCommand.h b/src/HttpProxyResponseCommand.h index 008f7a8b..14bd681f 100644 --- a/src/HttpProxyResponseCommand.h +++ b/src/HttpProxyResponseCommand.h @@ -52,7 +52,7 @@ public: const std::shared_ptr<SocketCore>& s); virtual ~HttpProxyResponseCommand(); - virtual Command* getNextCommand(); + virtual std::unique_ptr<Command> getNextCommand(); }; } // namespace aria2 diff --git a/src/HttpRequestCommand.cc b/src/HttpRequestCommand.cc index 620dbf7d..53f73007 100644 --- a/src/HttpRequestCommand.cc +++ b/src/HttpRequestCommand.cc @@ -130,7 +130,7 @@ bool HttpRequestCommand::executeInternal() { if(!getSocket()->tlsConnect(getRequest()->getHost())) { setReadCheckSocketIf(getSocket(), getSocket()->wantRead()); setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite()); - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } @@ -207,19 +207,19 @@ bool HttpRequestCommand::executeInternal() { httpConnection_->sendPendingData(); } if(httpConnection_->sendBufferIsEmpty()) { - Command* command = new HttpResponseCommand(getCuid(), - getRequest(), - getFileEntry(), - getRequestGroup(), - httpConnection_, - getDownloadEngine(), - getSocket()); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(make_unique<HttpResponseCommand> + (getCuid(), + getRequest(), + getFileEntry(), + getRequestGroup(), + httpConnection_, + getDownloadEngine(), + getSocket())); return true; } else { setReadCheckSocketIf(getSocket(), getSocket()->wantRead()); setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite()); - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } diff --git a/src/HttpRequestConnectChain.h b/src/HttpRequestConnectChain.h index ab6ef98e..4344860e 100644 --- a/src/HttpRequestConnectChain.h +++ b/src/HttpRequestConnectChain.h @@ -53,17 +53,17 @@ struct HttpRequestConnectChain : public ControlChain<ConnectCommand*> { (new SocketRecvBuffer(t->getSocket())); std::shared_ptr<HttpConnection> httpConnection (new HttpConnection(t->getCuid(), t->getSocket(), socketRecvBuffer)); - HttpRequestCommand* c = new HttpRequestCommand(t->getCuid(), - t->getRequest(), - t->getFileEntry(), - t->getRequestGroup(), - httpConnection, - e, - t->getSocket()); + auto c = make_unique<HttpRequestCommand>(t->getCuid(), + t->getRequest(), + t->getFileEntry(), + t->getRequestGroup(), + httpConnection, + e, + t->getSocket()); c->setProxyRequest(t->getProxyRequest()); c->setStatus(Command::STATUS_ONESHOT_REALTIME); e->setNoWait(true); - e->addCommand(c); + e->addCommand(std::move(c)); return 0; } }; diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index 872f401a..79e703b2 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -158,7 +158,7 @@ bool HttpResponseCommand::executeInternal() // For socket->wantRead() == true, setReadCheckSocket(socket) is already // done in the constructor. setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite()); - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } // check HTTP status number @@ -377,21 +377,17 @@ bool HttpResponseCommand::handleDefaultEncoding // we can't continue to use this socket because server sends all entity // body instead of a segment. // Therefore, we shutdown the socket here if pipelining is enabled. - DownloadCommand* command = 0; if(getRequest()->getMethod() == Request::METHOD_GET && segment && segment->getPositionToWrite() == 0 && !getRequest()->isPipeliningEnabled()) { - command = createHttpDownloadCommand - (httpResponse, - getTransferEncodingStreamFilter(httpResponse)); + checkEntry->pushNextCommand + (createHttpDownloadCommand + (httpResponse, + getTransferEncodingStreamFilter(httpResponse))); } else { getSegmentMan()->cancelSegment(getCuid()); getFileEntry()->poolRequest(getRequest()); } - // After command is passed to prepareForNextAction(), it is managed - // by CheckIntegrityEntry. - checkEntry->pushNextCommand(command); - command = 0; prepareForNextAction(checkEntry); @@ -505,7 +501,7 @@ bool HttpResponseCommand::skipResponseBody // We don't use Content-Encoding here because this response body is just // thrown away. - HttpSkipResponseCommand* command = new HttpSkipResponseCommand + auto command = make_unique<HttpSkipResponseCommand> (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), httpConnection_, httpResponse, getDownloadEngine(), getSocket()); @@ -522,7 +518,7 @@ bool HttpResponseCommand::skipResponseBody getDownloadEngine()->setNoWait(true); } - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(std::move(command)); return true; } @@ -544,16 +540,17 @@ bool decideFileAllocation } } // namespace -HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand +std::unique_ptr<HttpDownloadCommand> +HttpResponseCommand::createHttpDownloadCommand (const std::shared_ptr<HttpResponse>& httpResponse, const std::shared_ptr<StreamFilter>& filter) { - HttpDownloadCommand* command = - new HttpDownloadCommand(getCuid(), getRequest(), getFileEntry(), - getRequestGroup(), - httpResponse, httpConnection_, - getDownloadEngine(), getSocket()); + auto command = make_unique<HttpDownloadCommand> + (getCuid(), getRequest(), getFileEntry(), + getRequestGroup(), + httpResponse, httpConnection_, + getDownloadEngine(), getSocket()); command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME)); command->setLowestDownloadSpeedLimit (getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT)); @@ -563,9 +560,9 @@ HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand getRequestGroup()->setFileAllocationEnabled(false); } getRequestGroup()->getURISelector()->tuneDownloadCommand - (getFileEntry()->getRemainingUris(), command); + (getFileEntry()->getRemainingUris(), command.get()); - return command; + return std::move(command); } void HttpResponseCommand::poolConnection() diff --git a/src/HttpResponseCommand.h b/src/HttpResponseCommand.h index 8a947d5d..15b6bb87 100644 --- a/src/HttpResponseCommand.h +++ b/src/HttpResponseCommand.h @@ -67,7 +67,7 @@ private: bool handleOtherEncoding(const std::shared_ptr<HttpResponse>& httpResponse); bool skipResponseBody(const std::shared_ptr<HttpResponse>& httpResponse); - HttpDownloadCommand* + std::unique_ptr<HttpDownloadCommand> createHttpDownloadCommand (const std::shared_ptr<HttpResponse>& httpResponse, const std::shared_ptr<StreamFilter>& streamFilter); diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc index a860e90d..b05b6766 100644 --- a/src/HttpServerBodyCommand.cc +++ b/src/HttpServerBodyCommand.cc @@ -142,9 +142,8 @@ void HttpServerBodyCommand::sendJsonRpcBatchResponse void HttpServerBodyCommand::addHttpServerResponseCommand() { - Command* command = - new HttpServerResponseCommand(getCuid(), httpServer_, e_, socket_); - e_->addCommand(command); + e_->addCommand(make_unique<HttpServerResponseCommand> + (getCuid(), httpServer_, e_, socket_)); e_->setNoWait(true); } @@ -309,7 +308,7 @@ bool HttpServerBodyCommand::execute() } } else { updateWriteCheck(); - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } } else { @@ -317,7 +316,7 @@ bool HttpServerBodyCommand::execute() A2_LOG_INFO("HTTP request body timeout."); return true; } else { - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } } diff --git a/src/HttpServerCommand.cc b/src/HttpServerCommand.cc index 257ab474..1784611b 100644 --- a/src/HttpServerCommand.cc +++ b/src/HttpServerCommand.cc @@ -184,7 +184,7 @@ bool HttpServerCommand::execute() // finished. if(!socket_->tlsAccept()) { updateWriteCheck(); - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } } @@ -194,7 +194,7 @@ bool HttpServerCommand::execute() header = httpServer_->receiveRequest(); if(!header) { updateWriteCheck(); - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } // CORS preflight request uses OPTIONS method. It is not @@ -204,9 +204,8 @@ bool HttpServerCommand::execute() httpServer_->disableKeepAlive(); httpServer_->feedResponse (401, "WWW-Authenticate: Basic realm=\"aria2\"\r\n"); - Command* command = - new HttpServerResponseCommand(getCuid(), httpServer_, e_, socket_); - e_->addCommand(command); + e_->addCommand(make_unique<HttpServerResponseCommand> + (getCuid(), httpServer_, e_, socket_)); e_->setNoWait(true); return true; } @@ -214,7 +213,6 @@ bool HttpServerCommand::execute() header->fieldContains(HttpHeader::CONNECTION, "upgrade")) { #ifdef ENABLE_WEBSOCKET int status = websocketHandshake(header); - Command* command; if(status == 101) { std::string serverKey = createWebSocketServerKey @@ -222,26 +220,23 @@ bool HttpServerCommand::execute() httpServer_->feedUpgradeResponse("websocket", fmt("Sec-WebSocket-Accept: %s\r\n", serverKey.c_str())); - command = new rpc::WebSocketResponseCommand(getCuid(), httpServer_, - e_, socket_); + e_->addCommand(make_unique<rpc::WebSocketResponseCommand> + (getCuid(), httpServer_, e_, socket_)); } else { if(status == 426) { httpServer_->feedResponse(426, "Sec-WebSocket-Version: 13\r\n"); } else { httpServer_->feedResponse(status); } - command = new HttpServerResponseCommand(getCuid(), httpServer_, e_, - socket_); + e_->addCommand(make_unique<HttpServerResponseCommand> + (getCuid(), httpServer_, e_, socket_)); } - e_->addCommand(command); e_->setNoWait(true); return true; #else // !ENABLE_WEBSOCKET httpServer_->feedResponse(400); - Command* command = new HttpServerResponseCommand(getCuid(), - httpServer_, e_, - socket_); - e_->addCommand(command); + e_->addCommand(make_unique<HttpServerResponseCommand> + (getCuid(), httpServer_, e_, socket_)); e_->setNoWait(true); return true; #endif // !ENABLE_WEBSOCKET @@ -255,9 +250,8 @@ bool HttpServerCommand::execute() httpServer_->getContentLength())); return true; } - Command* command = new HttpServerBodyCommand(getCuid(), httpServer_, e_, - socket_); - e_->addCommand(command); + e_->addCommand(make_unique<HttpServerBodyCommand> + (getCuid(), httpServer_, e_, socket_)); e_->setNoWait(true); return true; } @@ -266,7 +260,7 @@ bool HttpServerCommand::execute() A2_LOG_INFO("HTTP request timeout."); return true; } else { - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } } diff --git a/src/HttpServerResponseCommand.cc b/src/HttpServerResponseCommand.cc index a2aa1555..727166e3 100644 --- a/src/HttpServerResponseCommand.cc +++ b/src/HttpServerResponseCommand.cc @@ -63,8 +63,8 @@ void HttpServerResponseCommand::afterSend A2_LOG_INFO(fmt("CUID#%" PRId64 " - Persist connection.", getCuid())); e->addCommand - (new HttpServerCommand(getCuid(), httpServer, e, - httpServer->getSocket())); + (make_unique<HttpServerCommand>(getCuid(), httpServer, e, + httpServer->getSocket())); } } diff --git a/src/HttpSkipResponseCommand.cc b/src/HttpSkipResponseCommand.cc index 0308c0ab..dc37f4ee 100644 --- a/src/HttpSkipResponseCommand.cc +++ b/src/HttpSkipResponseCommand.cc @@ -173,7 +173,7 @@ bool HttpSkipResponseCommand::executeInternal() return processResponse(); } else { setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite()); - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } diff --git a/src/InitiateConnectionCommand.cc b/src/InitiateConnectionCommand.cc index 069a5a8f..1d49d7fb 100644 --- a/src/InitiateConnectionCommand.cc +++ b/src/InitiateConnectionCommand.cc @@ -87,13 +87,12 @@ bool InitiateConnectionCommand::executeInternal() { std::vector<std::string> addrs; std::string ipaddr = resolveHostname(addrs, hostname, port); if(ipaddr.empty()) { - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } try { - Command* command = createNextCommand(hostname, ipaddr, port, - addrs, proxyRequest); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(createNextCommand(hostname, ipaddr, port, + addrs, proxyRequest)); return true; } catch(RecoverableException& ex) { // Catch exception and retry another address. @@ -106,12 +105,12 @@ bool InitiateConnectionCommand::executeInternal() { A2_LOG_INFO(fmt(MSG_CONNECT_FAILED_AND_RETRY, getCuid(), ipaddr.c_str(), port)); - Command* command = + auto command = InitiateConnectionCommandFactory::createInitiateConnectionCommand (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine()); getDownloadEngine()->setNoWait(true); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(std::move(command)); return true; } getDownloadEngine()->removeCachedIPAddress(hostname, port); @@ -149,12 +148,12 @@ InitiateConnectionCommand::createBackupIPv4ConnectCommand eoi = addrs.end(); i != eoi; ++i) { if(inetPton(AF_INET, (*i).c_str(), &buf) == 0) { info.reset(new BackupConnectInfo()); - BackupIPv4ConnectCommand* command = new BackupIPv4ConnectCommand + auto command = make_unique<BackupIPv4ConnectCommand> (getDownloadEngine()->newCUID(), *i, port, info, mainCommand, getRequestGroup(), getDownloadEngine()); A2_LOG_INFO(fmt("Issue backup connection command CUID#%" PRId64 ", addr=%s", command->getCuid(), (*i).c_str())); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(std::move(command)); return info; } } diff --git a/src/InitiateConnectionCommand.h b/src/InitiateConnectionCommand.h index c5e476a3..0dcaca4a 100644 --- a/src/InitiateConnectionCommand.h +++ b/src/InitiateConnectionCommand.h @@ -58,7 +58,7 @@ protected: // use this address this time. resolvedAddresses are all addresses // resolved. proxyRequest is set if we are going to use proxy // server. - virtual Command* createNextCommand + virtual std::unique_ptr<Command> createNextCommand (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector<std::string>& resolvedAddresses, const std::shared_ptr<Request>& proxyRequest) = 0; diff --git a/src/InitiateConnectionCommandFactory.cc b/src/InitiateConnectionCommandFactory.cc index 2d4e6141..2856b244 100644 --- a/src/InitiateConnectionCommandFactory.cc +++ b/src/InitiateConnectionCommandFactory.cc @@ -47,7 +47,7 @@ namespace aria2 { -Command* +std::unique_ptr<Command> InitiateConnectionCommandFactory::createInitiateConnectionCommand (cuid_t cuid, const std::shared_ptr<Request>& req, @@ -69,16 +69,16 @@ InitiateConnectionCommandFactory::createInitiateConnectionCommand req->setPipeliningHint(true); } - return - new HttpInitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e); + return make_unique<HttpInitiateConnectionCommand>(cuid, req, fileEntry, + requestGroup, e); } else if(req->getProtocol() == "ftp") { if(req->getFile().empty()) { throw DL_ABORT_EX (fmt("FTP URI %s doesn't contain file path.", req->getUri().c_str())); } - return - new FtpInitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e); + return make_unique<FtpInitiateConnectionCommand>(cuid, req, fileEntry, + requestGroup, e); } else { // these protocols are not supported yet throw DL_ABORT_EX diff --git a/src/InitiateConnectionCommandFactory.h b/src/InitiateConnectionCommandFactory.h index 1cedf465..e4d6bf1e 100644 --- a/src/InitiateConnectionCommandFactory.h +++ b/src/InitiateConnectionCommandFactory.h @@ -50,7 +50,7 @@ class FileEntry; class InitiateConnectionCommandFactory { public: - static Command* + static std::unique_ptr<Command> createInitiateConnectionCommand(cuid_t cuid, const std::shared_ptr<Request>& req, const std::shared_ptr<FileEntry>& fileEntry, diff --git a/src/InitiatorMSEHandshakeCommand.cc b/src/InitiatorMSEHandshakeCommand.cc index e5022921..4fd9b565 100644 --- a/src/InitiatorMSEHandshakeCommand.cc +++ b/src/InitiatorMSEHandshakeCommand.cc @@ -97,7 +97,7 @@ bool InitiatorMSEHandshakeCommand::executeInternal() { switch(sequence_) { case INITIATOR_SEND_KEY: { if(!getSocket()->isWritable(0)) { - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } setTimeout(getOption()->getAsInt(PREF_BT_TIMEOUT)); @@ -164,15 +164,15 @@ bool InitiatorMSEHandshakeCommand::executeInternal() { peerConnection->presetBuffer(mseHandshake_->getBuffer(), mseHandshake_->getBufferLength()); } - PeerInteractionCommand* c = - new PeerInteractionCommand - (getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_, - pieceStorage_, - peerStorage_, - getSocket(), - PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE, - peerConnection); - getDownloadEngine()->addCommand(c); + getDownloadEngine()->addCommand + (make_unique<PeerInteractionCommand> + (getCuid(), requestGroup_, getPeer(), + getDownloadEngine(), btRuntime_, + pieceStorage_, + peerStorage_, + getSocket(), + PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE, + peerConnection)); return true; } else { done = true; @@ -191,7 +191,7 @@ bool InitiatorMSEHandshakeCommand::executeInternal() { } else { disableWriteCheckSocket(); } - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } @@ -202,13 +202,11 @@ void InitiatorMSEHandshakeCommand::tryNewPeer() std::shared_ptr<Peer> peer = peerStorage_->checkoutPeer(ncuid); // sanity check if(peer) { - PeerInitiateConnectionCommand* command; - command = new PeerInitiateConnectionCommand(ncuid, requestGroup_, peer, - getDownloadEngine(), - btRuntime_); + auto command = make_unique<PeerInitiateConnectionCommand> + (ncuid, requestGroup_, peer, getDownloadEngine(), btRuntime_); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(std::move(command)); } } } @@ -230,12 +228,12 @@ bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait) // try legacy BitTorrent handshake A2_LOG_INFO(fmt("CUID#%" PRId64 " - Retry using legacy BitTorrent handshake.", getCuid())); - PeerInitiateConnectionCommand* command = - new PeerInitiateConnectionCommand(getCuid(), requestGroup_, getPeer(), - getDownloadEngine(), btRuntime_, false); + auto command = make_unique<PeerInitiateConnectionCommand> + (getCuid(), requestGroup_, getPeer(), + getDownloadEngine(), btRuntime_, false); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(std::move(command)); return true; } } diff --git a/src/LpdDispatchMessageCommand.cc b/src/LpdDispatchMessageCommand.cc index d2a54fbf..f1b4f152 100644 --- a/src/LpdDispatchMessageCommand.cc +++ b/src/LpdDispatchMessageCommand.cc @@ -87,7 +87,7 @@ bool LpdDispatchMessageCommand::execute() tryCount_ = 0; } } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/LpdReceiveMessageCommand.cc b/src/LpdReceiveMessageCommand.cc index 65641715..e0f11715 100644 --- a/src/LpdReceiveMessageCommand.cc +++ b/src/LpdReceiveMessageCommand.cc @@ -105,7 +105,7 @@ bool LpdReceiveMessageCommand::execute() peer->isLocalPeer()?1:0)); } } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/Makefile.am b/src/Makefile.am index 8dfe3daf..7b76f9c9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,7 +24,6 @@ SRCS = option_processing.cc\ FtpDownloadCommand.cc FtpDownloadCommand.h\ FtpTunnelRequestCommand.cc FtpTunnelRequestCommand.h\ FtpTunnelResponseCommand.cc FtpTunnelResponseCommand.h\ - SleepCommand.cc SleepCommand.h\ DownloadEngine.cc DownloadEngine.h\ Segment.h\ GrowSegment.cc GrowSegment.h\ diff --git a/src/NameResolveCommand.cc b/src/NameResolveCommand.cc index fab43395..8cfff500 100644 --- a/src/NameResolveCommand.cc +++ b/src/NameResolveCommand.cc @@ -95,7 +95,7 @@ bool NameResolveCommand::execute() #ifdef ENABLE_ASYNC_DNS if(e_->getOption()->getAsBool(PREF_ASYNC_DNS)) { if(resolveHostname(res, hostname) == 0) { - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } } else diff --git a/src/PeerAbstractCommand.cc b/src/PeerAbstractCommand.cc index df10c9ee..0fd13817 100644 --- a/src/PeerAbstractCommand.cc +++ b/src/PeerAbstractCommand.cc @@ -198,4 +198,9 @@ void PeerAbstractCommand::createSocket() socket_.reset(new SocketCore()); } +void PeerAbstractCommand::addCommandSelf() +{ + e_->addCommand(std::unique_ptr<Command>(this)); +} + } // namespace aria2 diff --git a/src/PeerAbstractCommand.h b/src/PeerAbstractCommand.h index 147237c1..af006751 100644 --- a/src/PeerAbstractCommand.h +++ b/src/PeerAbstractCommand.h @@ -93,6 +93,7 @@ protected: void disableWriteCheckSocket(); void setNoCheck(bool check); void updateKeepAlive(); + void addCommandSelf(); public: PeerAbstractCommand(cuid_t cuid, const std::shared_ptr<Peer>& peer, diff --git a/src/PeerChokeCommand.cc b/src/PeerChokeCommand.cc index 8568e7d8..8c886a9f 100644 --- a/src/PeerChokeCommand.cc +++ b/src/PeerChokeCommand.cc @@ -53,7 +53,7 @@ bool PeerChokeCommand::execute() { if(peerStorage_->chokeRoundIntervalElapsed()) { peerStorage_->executeChoke(); } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/PeerInitiateConnectionCommand.cc b/src/PeerInitiateConnectionCommand.cc index 118163ce..384af239 100644 --- a/src/PeerInitiateConnectionCommand.cc +++ b/src/PeerInitiateConnectionCommand.cc @@ -84,20 +84,20 @@ bool PeerInitiateConnectionCommand::executeInternal() { getSocket()->establishConnection(getPeer()->getIPAddress(), getPeer()->getPort(), false); if(mseHandshakeEnabled_) { - InitiatorMSEHandshakeCommand* c = - new InitiatorMSEHandshakeCommand(getCuid(), requestGroup_, getPeer(), - getDownloadEngine(), - btRuntime_, getSocket()); + auto c = make_unique<InitiatorMSEHandshakeCommand> + (getCuid(), requestGroup_, getPeer(), + getDownloadEngine(), btRuntime_, getSocket()); c->setPeerStorage(peerStorage_); c->setPieceStorage(pieceStorage_); - getDownloadEngine()->addCommand(c); + getDownloadEngine()->addCommand(std::move(c)); } else { - PeerInteractionCommand* command = - new PeerInteractionCommand - (getCuid(), requestGroup_, getPeer(), getDownloadEngine(), - btRuntime_, pieceStorage_, peerStorage_, - getSocket(), PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand + (make_unique<PeerInteractionCommand> + (getCuid(), requestGroup_, getPeer(), + getDownloadEngine(), + btRuntime_, pieceStorage_, peerStorage_, + getSocket(), + PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE)); } return true; } @@ -109,13 +109,11 @@ bool PeerInitiateConnectionCommand::prepareForNextPeer(time_t wait) { std::shared_ptr<Peer> peer = peerStorage_->checkoutPeer(ncuid); // sanity check if(peer) { - PeerInitiateConnectionCommand* command; - command = new PeerInitiateConnectionCommand(ncuid, requestGroup_, peer, - getDownloadEngine(), - btRuntime_); + auto command = make_unique<PeerInitiateConnectionCommand> + (ncuid, requestGroup_, peer, getDownloadEngine(), btRuntime_); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(std::move(command)); } } return true; diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc index 8aba2f85..f4940435 100644 --- a/src/PeerInteractionCommand.cc +++ b/src/PeerInteractionCommand.cc @@ -384,7 +384,7 @@ bool PeerInteractionCommand::executeInternal() { if(btInteractive_->countPendingMessage() > 0) { setNoCheck(true); } - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } @@ -395,13 +395,11 @@ bool PeerInteractionCommand::prepareForNextPeer(time_t wait) { std::shared_ptr<Peer> peer = peerStorage_->checkoutPeer(ncuid); // sanity check if(peer) { - PeerInitiateConnectionCommand* command; - command = new PeerInitiateConnectionCommand(ncuid, requestGroup_, peer, - getDownloadEngine(), - btRuntime_); + auto command = make_unique<PeerInitiateConnectionCommand> + (ncuid, requestGroup_, peer, getDownloadEngine(), btRuntime_); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); - getDownloadEngine()->addCommand(command); + getDownloadEngine()->addCommand(std::move(command)); } } return true; diff --git a/src/PeerListenCommand.cc b/src/PeerListenCommand.cc index 365e4c96..a7e3a147 100644 --- a/src/PeerListenCommand.cc +++ b/src/PeerListenCommand.cc @@ -115,9 +115,8 @@ bool PeerListenCommand::execute() { std::shared_ptr<Peer> peer(new Peer(peerInfo.first, peerInfo.second, true)); cuid_t cuid = e_->newCUID(); - Command* command = - new ReceiverMSEHandshakeCommand(cuid, peer, e_, peerSocket); - e_->addCommand(command); + e_->addCommand(make_unique<ReceiverMSEHandshakeCommand> + (cuid, peer, e_, peerSocket)); A2_LOG_DEBUG(fmt("Accepted the connection from %s:%u.", peer->getIPAddress().c_str(), peer->getPort())); @@ -129,7 +128,7 @@ bool PeerListenCommand::execute() { ex); } } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/PeerReceiveHandshakeCommand.cc b/src/PeerReceiveHandshakeCommand.cc index 5cdd51c3..283ca4d7 100644 --- a/src/PeerReceiveHandshakeCommand.cc +++ b/src/PeerReceiveHandshakeCommand.cc @@ -142,8 +142,7 @@ bool PeerReceiveHandshakeCommand::executeInternal() // that the added peer must be checked out. if(peerStorage->addPeer(getPeer()) && peerStorage->checkoutPeer(getCuid())) { - PeerInteractionCommand* command = - new PeerInteractionCommand + getDownloadEngine()->addCommand(make_unique<PeerInteractionCommand> (getCuid(), downloadContext->getOwnerRequestGroup(), getPeer(), @@ -153,8 +152,7 @@ bool PeerReceiveHandshakeCommand::executeInternal() peerStorage, getSocket(), PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE, - peerConnection_); - getDownloadEngine()->addCommand(command); + peerConnection_)); A2_LOG_DEBUG(fmt(MSG_INCOMING_PEER_CONNECTION, getCuid(), getPeer()->usedBy())); @@ -162,7 +160,7 @@ bool PeerReceiveHandshakeCommand::executeInternal() } return true; } else { - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } } diff --git a/src/PieceHashCheckIntegrityEntry.cc b/src/PieceHashCheckIntegrityEntry.cc index f4e12010..41d2eec2 100644 --- a/src/PieceHashCheckIntegrityEntry.cc +++ b/src/PieceHashCheckIntegrityEntry.cc @@ -42,8 +42,8 @@ namespace aria2 { PieceHashCheckIntegrityEntry::PieceHashCheckIntegrityEntry (RequestGroup* requestGroup, - Command* nextCommand): - CheckIntegrityEntry(requestGroup, nextCommand) {} + std::unique_ptr<Command> nextCommand): + CheckIntegrityEntry(requestGroup, std::move(nextCommand)) {} PieceHashCheckIntegrityEntry::~PieceHashCheckIntegrityEntry() {} diff --git a/src/PieceHashCheckIntegrityEntry.h b/src/PieceHashCheckIntegrityEntry.h index 821af5f7..e380e79c 100644 --- a/src/PieceHashCheckIntegrityEntry.h +++ b/src/PieceHashCheckIntegrityEntry.h @@ -42,7 +42,9 @@ namespace aria2 { class PieceHashCheckIntegrityEntry : public CheckIntegrityEntry { public: - PieceHashCheckIntegrityEntry(RequestGroup* requestGroup, Command* nextCommand = 0); + PieceHashCheckIntegrityEntry(RequestGroup* requestGroup, + std::unique_ptr<Command> nextCommand = + std::unique_ptr<Command>()); virtual ~PieceHashCheckIntegrityEntry(); diff --git a/src/ReceiverMSEHandshakeCommand.cc b/src/ReceiverMSEHandshakeCommand.cc index c458a924..f79e19f6 100644 --- a/src/ReceiverMSEHandshakeCommand.cc +++ b/src/ReceiverMSEHandshakeCommand.cc @@ -107,12 +107,12 @@ bool ReceiverMSEHandshakeCommand::executeInternal() (new PeerConnection(getCuid(), getPeer(), getSocket())); peerConnection->presetBuffer(mseHandshake_->getBuffer(), mseHandshake_->getBufferLength()); - Command* c = new PeerReceiveHandshakeCommand(getCuid(), - getPeer(), - getDownloadEngine(), - getSocket(), - peerConnection); - getDownloadEngine()->addCommand(c); + getDownloadEngine()->addCommand + (make_unique<PeerReceiveHandshakeCommand>(getCuid(), + getPeer(), + getDownloadEngine(), + getSocket(), + peerConnection)); return true; } default: @@ -200,7 +200,7 @@ bool ReceiverMSEHandshakeCommand::executeInternal() } else { disableWriteCheckSocket(); } - getDownloadEngine()->addCommand(this); + addCommandSelf(); return false; } @@ -219,10 +219,9 @@ void ReceiverMSEHandshakeCommand::createCommand() // TODO add mseHandshake_->getInfoHash() to PeerReceiveHandshakeCommand // as a hint. If this info hash and one in BitTorrent Handshake does not // match, then drop connection. - Command* c = - new PeerReceiveHandshakeCommand(getCuid(), getPeer(), getDownloadEngine(), - getSocket(), peerConnection); - getDownloadEngine()->addCommand(c); + getDownloadEngine()->addCommand(make_unique<PeerReceiveHandshakeCommand> + (getCuid(), getPeer(), getDownloadEngine(), + getSocket(), peerConnection)); } } // namespace aria2 diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 51db87ea..d35e4967 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -277,7 +277,7 @@ std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry() } void RequestGroup::createInitialCommand -(std::vector<Command*>& commands, DownloadEngine* e) +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) { // Start session timer here. When file size becomes known, it will // be reset again in *FileAllocationEntry, because hash check and @@ -369,15 +369,11 @@ void RequestGroup::createInitialCommand (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && option_->getAsBool(PREF_ENABLE_DHT6))) { if(option_->getAsBool(PREF_ENABLE_DHT)) { - std::vector<Command*> dhtCommands; - DHTSetup().setup(dhtCommands, e, AF_INET); - e->addCommand(dhtCommands); + e->addCommand(DHTSetup().setup(e, AF_INET)); } if(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && option_->getAsBool(PREF_ENABLE_DHT6)) { - std::vector<Command*> dhtCommands; - DHTSetup().setup(dhtCommands, e, AF_INET6); - e->addCommand(dhtCommands); + e->addCommand(DHTSetup().setup(e, AF_INET6)); } } else { A2_LOG_NOTICE(_("For BitTorrent Magnet URI, enabling DHT is strongly" @@ -439,27 +435,23 @@ void RequestGroup::createInitialCommand (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && option_->getAsBool(PREF_ENABLE_DHT6)))) { if(option_->getAsBool(PREF_ENABLE_DHT)) { - std::vector<Command*> dhtCommands; - DHTSetup().setup(dhtCommands, e, AF_INET); - e->addCommand(dhtCommands); + e->addCommand(DHTSetup().setup(e, AF_INET)); } if(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && option_->getAsBool(PREF_ENABLE_DHT6)) { - std::vector<Command*> dhtCommands; - DHTSetup().setup(dhtCommands, e, AF_INET6); - e->addCommand(dhtCommands); + e->addCommand(DHTSetup().setup(e, AF_INET6)); } const std::vector<std::pair<std::string, uint16_t> >& nodes = torrentAttrs->nodes; // TODO Are nodes in torrent IPv4 only? if(!nodes.empty() && DHTRegistry::isInitialized()) { - DHTEntryPointNameResolveCommand* command = - new DHTEntryPointNameResolveCommand(e->newCUID(), e, nodes); + auto command = make_unique<DHTEntryPointNameResolveCommand> + (e->newCUID(), e, nodes); command->setTaskQueue(DHTRegistry::getData().taskQueue); command->setTaskFactory(DHTRegistry::getData().taskFactory); command->setRoutingTable(DHTRegistry::getData().routingTable); command->setLocalNode(DHTRegistry::getData().localNode); - e->addCommand(command); + e->addCommand(std::move(command)); } } std::shared_ptr<CheckIntegrityEntry> entry(new BtCheckIntegrityEntry(this)); @@ -553,7 +545,7 @@ void RequestGroup::createInitialCommand } void RequestGroup::processCheckIntegrityEntry -(std::vector<Command*>& commands, +(std::vector<std::unique_ptr<Command>>& commands, const std::shared_ptr<CheckIntegrityEntry>& entry, DownloadEngine* e) { @@ -804,8 +796,9 @@ bool RequestGroup::tryAutoFileRenaming() return false; } -void RequestGroup::createNextCommandWithAdj(std::vector<Command*>& commands, - DownloadEngine* e, int numAdj) +void RequestGroup::createNextCommandWithAdj +(std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e, int numAdj) { int numCommand; if(getTotalLength() == 0) { @@ -820,8 +813,9 @@ void RequestGroup::createNextCommandWithAdj(std::vector<Command*>& commands, } } -void RequestGroup::createNextCommand(std::vector<Command*>& commands, - DownloadEngine* e) +void RequestGroup::createNextCommand +(std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e) { int numCommand; if(getTotalLength() == 0) { @@ -844,13 +838,14 @@ void RequestGroup::createNextCommand(std::vector<Command*>& commands, } } -void RequestGroup::createNextCommand(std::vector<Command*>& commands, - DownloadEngine* e, - int numCommand) +void RequestGroup::createNextCommand +(std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e, + int numCommand) { for(; numCommand > 0; --numCommand) { - Command* command = new CreateRequestCommand(e->newCUID(), this, e); - commands.push_back(command); + commands.push_back(make_unique<CreateRequestCommand> + (e->newCUID(), this, e)); } if(!commands.empty()) { e->setNoWait(true); diff --git a/src/RequestGroup.h b/src/RequestGroup.h index 3fe0ce33..6ba18145 100644 --- a/src/RequestGroup.h +++ b/src/RequestGroup.h @@ -215,16 +215,18 @@ public: // Returns first bootstrap commands to initiate a download. // If this is HTTP/FTP download and file size is unknown, only 1 command // (usually, HttpInitiateConnection or FtpInitiateConnection) will be created. - void createInitialCommand(std::vector<Command*>& commands, + void createInitialCommand(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e); - void createNextCommandWithAdj(std::vector<Command*>& commands, - DownloadEngine* e, int numAdj); + void createNextCommandWithAdj + (std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e, int numAdj); - void createNextCommand(std::vector<Command*>& commands, + void createNextCommand(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e, int numCommand); - void createNextCommand(std::vector<Command*>& commands, DownloadEngine* e); + void createNextCommand(std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e); bool downloadFinished() const; @@ -383,9 +385,10 @@ public: void clearPreDownloadHandler(); - void processCheckIntegrityEntry(std::vector<Command*>& commands, - const std::shared_ptr<CheckIntegrityEntry>& entry, - DownloadEngine* e); + void processCheckIntegrityEntry + (std::vector<std::unique_ptr<Command>>& commands, + const std::shared_ptr<CheckIntegrityEntry>& entry, + DownloadEngine* e); // Initializes pieceStorage_ and segmentMan_. We guarantee that // either both of pieceStorage_ and segmentMan_ are initialized or diff --git a/src/RequestGroupEntry.cc b/src/RequestGroupEntry.cc index c5aef0bf..8241e52a 100644 --- a/src/RequestGroupEntry.cc +++ b/src/RequestGroupEntry.cc @@ -40,9 +40,9 @@ namespace aria2 { RequestGroupEntry::RequestGroupEntry(RequestGroup* requestGroup, - Command* nextCommand): + std::unique_ptr<Command> nextCommand): requestGroup_(requestGroup), - nextCommand_(nextCommand) + nextCommand_(std::move(nextCommand)) { requestGroup_->increaseNumCommand(); } @@ -50,20 +50,16 @@ RequestGroupEntry::RequestGroupEntry(RequestGroup* requestGroup, RequestGroupEntry::~RequestGroupEntry() { requestGroup_->decreaseNumCommand(); - delete nextCommand_; } -Command* RequestGroupEntry::popNextCommand() +std::unique_ptr<Command> RequestGroupEntry::popNextCommand() { - Command* temp = nextCommand_; - nextCommand_ = 0; - return temp; + return std::move(nextCommand_); } -void RequestGroupEntry::pushNextCommand(Command* nextCommand) +void RequestGroupEntry::pushNextCommand(std::unique_ptr<Command> nextCommand) { - delete nextCommand_; - nextCommand_ = nextCommand; + nextCommand_ = std::move(nextCommand); } } // namespace aria2 diff --git a/src/RequestGroupEntry.h b/src/RequestGroupEntry.h index fedd41d5..eb151461 100644 --- a/src/RequestGroupEntry.h +++ b/src/RequestGroupEntry.h @@ -37,6 +37,8 @@ #include "common.h" +#include <memory> + namespace aria2 { class RequestGroup; @@ -45,10 +47,11 @@ class Command; class RequestGroupEntry { private: RequestGroup* requestGroup_; - Command* nextCommand_; + std::unique_ptr<Command> nextCommand_; public: RequestGroupEntry(RequestGroup* requestGroup, - Command* nextCommand = 0); + std::unique_ptr<Command> nextCommand = + std::unique_ptr<Command>()); virtual ~RequestGroupEntry(); @@ -59,12 +62,12 @@ public: Command* getNextCommand() const { - return nextCommand_; + return nextCommand_.get(); } - Command* popNextCommand(); + std::unique_ptr<Command> popNextCommand(); - void pushNextCommand(Command* nextCommand); + void pushNextCommand(std::unique_ptr<Command> nextCommand); bool operator==(const RequestGroupEntry& entry) const { diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 106b783d..11135f26 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -438,11 +438,12 @@ void RequestGroupMan::configureRequestGroup } namespace { -void createInitialCommand(const std::shared_ptr<RequestGroup>& requestGroup, - std::vector<Command*>& commands, - DownloadEngine* e) +std::vector<std::unique_ptr<Command>> createInitialCommand +(const std::shared_ptr<RequestGroup>& requestGroup, DownloadEngine* e) { - requestGroup->createInitialCommand(commands, e); + std::vector<std::unique_ptr<Command>> res; + requestGroup->createInitialCommand(res, e); + return res; } } // namespace @@ -483,24 +484,21 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e) groupToAdd->dropPieceStorage(); configureRequestGroup(groupToAdd); groupToAdd->setRequestGroupMan(this); - std::vector<Command*> commands; try { - createInitialCommand(groupToAdd, commands, e); + auto res = createInitialCommand(groupToAdd, e); ++count; + if(res.empty()) { + requestQueueCheck(); + } else { + e->addCommand(std::move(res)); + } } catch(RecoverableException& ex) { A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex); A2_LOG_DEBUG("Deleting temporal commands."); - std::for_each(commands.begin(), commands.end(), Deleter()); - commands.clear(); - A2_LOG_DEBUG("Commands deleted"); groupToAdd->setLastErrorCode(ex.getErrorCode()); // We add groupToAdd to e later in order to it is processed in // removeStoppedGroup(). - } - if(commands.empty()) { requestQueueCheck(); - } else { - e->addCommand(commands); } groupToAdd->setState(RequestGroup::STATE_ACTIVE); requestGroups_.push_back(groupToAdd->getGID(), groupToAdd); diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index ddbb8435..d64c6473 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -1312,9 +1312,9 @@ std::shared_ptr<ValueBase> ChangeUriRpcMethod::process } } if(addcount && group->getPieceStorage()) { - std::vector<Command*> commands; + std::vector<std::unique_ptr<Command>> commands; group->createNextCommand(commands, e); - e->addCommand(commands); + e->addCommand(std::move(commands)); group->getSegmentMan()->recognizeSegmentFor(s); } std::shared_ptr<List> res = List::g(); @@ -1329,7 +1329,8 @@ std::shared_ptr<ValueBase> goingShutdown { // Schedule shutdown after 3seconds to give time to client to // receive RPC response. - e->addRoutineCommand(new TimedHaltCommand(e->newCUID(), e, 3, forceHalt)); + e->addRoutineCommand(make_unique<TimedHaltCommand> + (e->newCUID(), e, 3, forceHalt)); A2_LOG_INFO("Scheduled shutdown in 3 seconds."); return VLB_OK; } diff --git a/src/SeedCheckCommand.cc b/src/SeedCheckCommand.cc index fc536427..59801526 100644 --- a/src/SeedCheckCommand.cc +++ b/src/SeedCheckCommand.cc @@ -84,7 +84,7 @@ bool SeedCheckCommand::execute() { btRuntime_->setHalt(true); } } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/SequentialDispatcherCommand.h b/src/SequentialDispatcherCommand.h index 9e0f1e36..0b5a1580 100644 --- a/src/SequentialDispatcherCommand.h +++ b/src/SequentialDispatcherCommand.h @@ -78,12 +78,13 @@ public: e_->setNoWait(true); } - e_->addRoutineCommand(this); + e_->addRoutineCommand(std::unique_ptr<Command>(this)); return false; } protected: - virtual Command* createCommand(const std::shared_ptr<T>& entry) = 0; + virtual std::unique_ptr<Command> createCommand + (const std::shared_ptr<T>& entry) = 0; }; } // namespace aria2 diff --git a/src/SleepCommand.cc b/src/SleepCommand.cc deleted file mode 100644 index a3ace47e..00000000 --- a/src/SleepCommand.cc +++ /dev/null @@ -1,67 +0,0 @@ -/* <!-- copyright */ -/* - * aria2 - The high speed download utility - * - * Copyright (C) 2006 Tatsuhiro Tsujikawa - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * In addition, as a special exception, the copyright holders give - * permission to link the code of portions of this program with the - * OpenSSL library under certain conditions as described in each - * individual source file, and distribute linked combinations - * including the two. - * You must obey the GNU General Public License in all respects - * for all of the code used other than OpenSSL. If you modify - * file(s) with this exception, you may extend this exception to your - * version of the file(s), but you are not obligated to do so. If you - * do not wish to do so, delete this exception statement from your - * version. If you delete this exception statement from all source - * files in the program, then also delete it here. - */ -/* copyright --> */ -#include "SleepCommand.h" -#include "RequestGroup.h" -#include "DownloadEngine.h" -#include "wallclock.h" - -namespace aria2 { - -SleepCommand::SleepCommand(cuid_t cuid, DownloadEngine* e, - RequestGroup* requestGroup, - Command* nextCommand, time_t wait): - Command(cuid), engine_(e), requestGroup_(requestGroup), - nextCommand_(nextCommand), wait_(wait), checkPoint_(global::wallclock()) {} - -SleepCommand::~SleepCommand() { - delete nextCommand_; -} - -bool SleepCommand::execute() { - if(requestGroup_->downloadFinished() || requestGroup_->isHaltRequested()) { - return true; - } else if(checkPoint_.differenceInMillis(global::wallclock())+A2_DELTA_MILLIS - >= wait_*1000) { - engine_->addCommand(nextCommand_); - nextCommand_ = 0; - engine_->setNoWait(true); - return true; - } else { - engine_->addCommand(this); - return false; - } -} - -} // namespace aria2 diff --git a/src/SleepCommand.h b/src/SleepCommand.h deleted file mode 100644 index 32633fc4..00000000 --- a/src/SleepCommand.h +++ /dev/null @@ -1,62 +0,0 @@ -/* <!-- copyright */ -/* - * aria2 - The high speed download utility - * - * Copyright (C) 2006 Tatsuhiro Tsujikawa - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * In addition, as a special exception, the copyright holders give - * permission to link the code of portions of this program with the - * OpenSSL library under certain conditions as described in each - * individual source file, and distribute linked combinations - * including the two. - * You must obey the GNU General Public License in all respects - * for all of the code used other than OpenSSL. If you modify - * file(s) with this exception, you may extend this exception to your - * version of the file(s), but you are not obligated to do so. If you - * do not wish to do so, delete this exception statement from your - * version. If you delete this exception statement from all source - * files in the program, then also delete it here. - */ -/* copyright --> */ -#ifndef D_SLEEP_COMMAND_H -#define D_SLEEP_COMMAND_H - -#include "Command.h" -#include "TimerA2.h" - -namespace aria2 { - -class DownloadEngine; -class RequestGroup; - -class SleepCommand:public Command { -private: - DownloadEngine* engine_; - RequestGroup* requestGroup_; - Command* nextCommand_; - time_t wait_; - Timer checkPoint_; -public: - SleepCommand(cuid_t cuid, DownloadEngine* e, RequestGroup* requestGroup, - Command* nextCommand, time_t wait); - virtual ~SleepCommand(); - bool execute(); -}; - -} // namespace aria2 - -#endif // D_SLEEP_COMMAND_H diff --git a/src/StreamCheckIntegrityEntry.cc b/src/StreamCheckIntegrityEntry.cc index e267df73..e245f18e 100644 --- a/src/StreamCheckIntegrityEntry.cc +++ b/src/StreamCheckIntegrityEntry.cc @@ -42,15 +42,15 @@ namespace aria2 { -StreamCheckIntegrityEntry::StreamCheckIntegrityEntry(RequestGroup* requestGroup, - Command* nextCommand): - PieceHashCheckIntegrityEntry(requestGroup, nextCommand) +StreamCheckIntegrityEntry::StreamCheckIntegrityEntry +(RequestGroup* requestGroup, std::unique_ptr<Command> nextCommand): + PieceHashCheckIntegrityEntry(requestGroup, std::move(nextCommand)) {} StreamCheckIntegrityEntry::~StreamCheckIntegrityEntry() {} void StreamCheckIntegrityEntry::onDownloadIncomplete -(std::vector<Command*>& commands, DownloadEngine* e) +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) { const std::shared_ptr<PieceStorage>& ps = getRequestGroup()->getPieceStorage(); ps->onDownloadIncomplete(); @@ -63,7 +63,7 @@ void StreamCheckIntegrityEntry::onDownloadIncomplete } void StreamCheckIntegrityEntry::onDownloadFinished -(std::vector<Command*>& commands, DownloadEngine* e) +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) {} } // namespace aria2 diff --git a/src/StreamCheckIntegrityEntry.h b/src/StreamCheckIntegrityEntry.h index 02e48522..8e880944 100644 --- a/src/StreamCheckIntegrityEntry.h +++ b/src/StreamCheckIntegrityEntry.h @@ -43,15 +43,16 @@ class StreamCheckIntegrityEntry:public PieceHashCheckIntegrityEntry { public: StreamCheckIntegrityEntry(RequestGroup* requestGroup, - Command* nextCommand = 0); + std::unique_ptr<Command> nextCommand = + std::unique_ptr<Command>()); virtual ~StreamCheckIntegrityEntry(); - virtual void onDownloadFinished(std::vector<Command*>& commands, - DownloadEngine* e); + virtual void onDownloadFinished + (std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e); - virtual void onDownloadIncomplete(std::vector<Command*>& commands, - DownloadEngine* e); + virtual void onDownloadIncomplete + (std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e); }; } // namespace aria2 diff --git a/src/StreamFileAllocationEntry.cc b/src/StreamFileAllocationEntry.cc index 9afa6f29..d30018a2 100644 --- a/src/StreamFileAllocationEntry.cc +++ b/src/StreamFileAllocationEntry.cc @@ -49,15 +49,15 @@ namespace aria2 { -StreamFileAllocationEntry::StreamFileAllocationEntry(RequestGroup* requestGroup, - Command* nextCommand): - FileAllocationEntry(requestGroup, nextCommand) +StreamFileAllocationEntry::StreamFileAllocationEntry +(RequestGroup* requestGroup, std::unique_ptr<Command> nextCommand): + FileAllocationEntry(requestGroup, std::move(nextCommand)) {} StreamFileAllocationEntry::~StreamFileAllocationEntry() {} void StreamFileAllocationEntry::prepareForNextAction -(std::vector<Command*>& commands, +(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) { // For DownloadContext::resetDownloadStartTime(), see also diff --git a/src/StreamFileAllocationEntry.h b/src/StreamFileAllocationEntry.h index b7635bab..d4522b20 100644 --- a/src/StreamFileAllocationEntry.h +++ b/src/StreamFileAllocationEntry.h @@ -42,12 +42,14 @@ namespace aria2 { class StreamFileAllocationEntry : public FileAllocationEntry { public: StreamFileAllocationEntry(RequestGroup* requestGroup, - Command* nextCommand = 0); + std::unique_ptr<Command> nextCommand = + std::unique_ptr<Command>()); virtual ~StreamFileAllocationEntry(); - virtual void prepareForNextAction(std::vector<Command*>& commands, - DownloadEngine* e); + virtual void prepareForNextAction + (std::vector<std::unique_ptr<Command>>& commands, + DownloadEngine* e); }; } // namespace aria2 diff --git a/src/TimeBasedCommand.cc b/src/TimeBasedCommand.cc index 50d1ff03..f077baed 100644 --- a/src/TimeBasedCommand.cc +++ b/src/TimeBasedCommand.cc @@ -64,9 +64,9 @@ bool TimeBasedCommand::execute() return true; } if(routineCommand_) { - e_->addRoutineCommand(this); + e_->addRoutineCommand(std::unique_ptr<Command>(this)); } else { - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); } return false; } diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index 5b1d63da..303b0c64 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -95,12 +95,10 @@ void HTTPAnnRequest::stop(DownloadEngine* e) bool HTTPAnnRequest::issue(DownloadEngine* e) { try { - std::vector<Command*>* commands = new std::vector<Command*>(); - auto_delete_container<std::vector<Command*> > commandsDel(commands); - rg_->createInitialCommand(*commands, e); - e->addCommand(*commands); + std::vector<std::unique_ptr<Command>> commands; + rg_->createInitialCommand(commands, e); + e->addCommand(std::move(commands)); e->setNoWait(true); - commands->clear(); A2_LOG_DEBUG("added tracker request command"); return true; } catch(RecoverableException& ex) { @@ -162,9 +160,7 @@ void UDPAnnRequest::stop(DownloadEngine* e) bool UDPAnnRequest::issue(DownloadEngine* e) { if(req_) { - NameResolveCommand* command = new NameResolveCommand - (e->newCUID(), e, req_); - e->addCommand(command); + e->addCommand(make_unique<NameResolveCommand>(e->newCUID(), e, req_)); e->setNoWait(true); return true; } else { @@ -214,7 +210,7 @@ bool TrackerWatcherCommand::execute() { } else { trackerRequest_->stop(e_); e_->setRefreshInterval(0); - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } } @@ -253,7 +249,7 @@ bool TrackerWatcherCommand::execute() { } } } - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } @@ -269,12 +265,11 @@ void TrackerWatcherCommand::addConnection() if(!peer) { break; } - PeerInitiateConnectionCommand* command; - command = new PeerInitiateConnectionCommand(ncuid, requestGroup_, peer, - e_, btRuntime_); + auto command = make_unique<PeerInitiateConnectionCommand> + (ncuid, requestGroup_, peer, e_, btRuntime_); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); - e_->addCommand(command); + e_->addCommand(std::move(command)); A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Adding new command CUID#%" PRId64 "", getCuid(), peer->usedBy())); } diff --git a/src/WebSocketInteractionCommand.cc b/src/WebSocketInteractionCommand.cc index a1eaf3af..270726ad 100644 --- a/src/WebSocketInteractionCommand.cc +++ b/src/WebSocketInteractionCommand.cc @@ -104,7 +104,7 @@ bool WebSocketInteractionCommand::execute() return true; } updateWriteCheck(); - e_->addCommand(this); + e_->addCommand(std::unique_ptr<Command>(this)); return false; } diff --git a/src/WebSocketResponseCommand.cc b/src/WebSocketResponseCommand.cc index 245c14d7..193a9c8a 100644 --- a/src/WebSocketResponseCommand.cc +++ b/src/WebSocketResponseCommand.cc @@ -60,11 +60,10 @@ void WebSocketResponseCommand::afterSend { std::shared_ptr<WebSocketSession> wsSession (new WebSocketSession(httpServer->getSocket(), getDownloadEngine())); - WebSocketInteractionCommand* command = - new WebSocketInteractionCommand(getCuid(), wsSession, e, - wsSession->getSocket()); - wsSession->setCommand(command); - e->addCommand(command); + auto command = make_unique<WebSocketInteractionCommand> + (getCuid(), wsSession, e, wsSession->getSocket()); + wsSession->setCommand(command.get()); + e->addCommand(std::move(command)); } } // namespace rpc |
