/* */ #include "InitiateConnectionCommand.h" #include "Request.h" #include "DownloadEngine.h" #include "Option.h" #include "Logger.h" #include "message.h" #include "prefs.h" #include "NameResolver.h" #include "SocketCore.h" #include "FileEntry.h" #include "RequestGroup.h" #include "DownloadContext.h" #include "Segment.h" #include "a2functional.h" #include "InitiateConnectionCommandFactory.h" #include "util.h" #include "RequestGroupMan.h" #include "ServerStatMan.h" #include "FileAllocationEntry.h" #include "CheckIntegrityEntry.h" #include "RecoverableException.h" namespace aria2 { InitiateConnectionCommand::InitiateConnectionCommand (cuid_t cuid, const SharedHandle& req, const SharedHandle& fileEntry, RequestGroup* requestGroup, DownloadEngine* e): AbstractCommand(cuid, req, fileEntry, requestGroup, e) { setTimeout(getOption()->getAsInt(PREF_DNS_TIMEOUT)); // give a chance to be executed in the next loop in DownloadEngine setStatus(Command::STATUS_ONESHOT_REALTIME); disableReadCheckSocket(); disableWriteCheckSocket(); } InitiateConnectionCommand::~InitiateConnectionCommand() {} bool InitiateConnectionCommand::executeInternal() { std::string hostname; uint16_t port; SharedHandle proxyRequest = createProxyRequest(); if(proxyRequest.isNull()) { hostname = getRequest()->getHost(); port = getRequest()->getPort(); } else { hostname = proxyRequest->getHost(); port = proxyRequest->getPort(); } std::vector addrs; std::string ipaddr = resolveHostname(addrs, hostname, port); if(ipaddr.empty()) { getDownloadEngine()->addCommand(this); return false; } try { Command* command = createNextCommand(hostname, ipaddr, port, addrs, proxyRequest); getDownloadEngine()->addCommand(command); return true; } catch(RecoverableException& ex) { // Catch exception and retry another address. // See also AbstractCommand::checkIfConnectionEstablished // TODO ipaddr might not be used if pooled sockt was found. getDownloadEngine()->markBadIPAddress(hostname, ipaddr, port); if(!getDownloadEngine()->findCachedIPAddress(hostname, port).empty()) { if(getLogger()->info()) { getLogger()->info(EX_EXCEPTION_CAUGHT, ex); getLogger()->info(MSG_CONNECT_FAILED_AND_RETRY, util::itos(getCuid()).c_str(), ipaddr.c_str(), port); } Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine()); getDownloadEngine()->setNoWait(true); getDownloadEngine()->addCommand(command); return true; } getDownloadEngine()->removeCachedIPAddress(hostname, port); throw; } } } // namespace aria2