/* */ #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" 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 = req->getHost(); port = req->getPort(); } else { hostname = proxyRequest->getHost(); port = proxyRequest->getPort(); } std::vector addrs; std::string ipaddr = resolveHostname(addrs, hostname, port); if(ipaddr.empty()) { e->commands.push_back(this); return false; } try { Command* command = createNextCommand(hostname, ipaddr, port, addrs, proxyRequest); e->commands.push_back(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. e->markBadIPAddress(hostname, ipaddr, port); if(!e->findCachedIPAddress(hostname, port).empty()) { if(logger->info()) { logger->info(EX_EXCEPTION_CAUGHT, ex); logger->info(MSG_CONNECT_FAILED_AND_RETRY, util::itos(cuid).c_str(), ipaddr.c_str(), port); } Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand (cuid, req, _fileEntry, _requestGroup, e); e->setNoWait(true); e->commands.push_back(command); return true; } e->removeCachedIPAddress(hostname, port); throw; } } } // namespace aria2