/* */ #include "InitiateConnectionCommand.h" #include "Request.h" #include "DownloadEngine.h" #include "Option.h" #include "Logger.h" #include "LogFactory.h" #include "message.h" #include "prefs.h" #include "NameResolver.h" #include "SocketCore.h" #include "FileEntry.h" #include "RequestGroup.h" #include "Segment.h" #include "a2functional.h" #include "InitiateConnectionCommandFactory.h" #include "util.h" #include "RecoverableException.h" #include "fmt.h" #include "SocketRecvBuffer.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) { 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 socket was found. getDownloadEngine()->markBadIPAddress(hostname, ipaddr, port); if(!getDownloadEngine()->findCachedIPAddress(hostname, port).empty()) { A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, ex); A2_LOG_INFO(fmt(MSG_CONNECT_FAILED_AND_RETRY, getCuid(), 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; } } void InitiateConnectionCommand::setConnectedAddrInfo (const SharedHandle& req, const std::string& hostname, const SharedHandle& socket) { std::pair peerAddr; socket->getPeerInfo(peerAddr); req->setConnectedAddrInfo(hostname, peerAddr.first, peerAddr.second); } } // namespace aria2