/* */ #include "PeerReceiveHandshakeCommand.h" #include "PeerConnection.h" #include "DownloadEngine.h" #include "BtHandshakeMessage.h" #include "util.h" #include "DownloadContext.h" #include "DlAbortEx.h" #include "PeerInteractionCommand.h" #include "Peer.h" #include "BtRegistry.h" #include "PeerStorage.h" #include "PieceStorage.h" #include "BtRuntime.h" #include "BtAnnounce.h" #include "BtProgressInfoFile.h" #include "BtConstants.h" #include "message.h" #include "Socket.h" #include "Logger.h" #include "prefs.h" #include "Option.h" #include "RequestGroupMan.h" #include "StringFormat.h" #include "RequestGroup.h" #include "ServerStatMan.h" #include "FileAllocationEntry.h" #include "CheckIntegrityEntry.h" namespace aria2 { PeerReceiveHandshakeCommand::PeerReceiveHandshakeCommand (cuid_t cuid, const SharedHandle& peer, DownloadEngine* e, const SocketHandle& s, const SharedHandle& peerConnection) : PeerAbstractCommand(cuid, peer, e, s), _peerConnection(peerConnection) { if(_peerConnection.isNull()) { _peerConnection.reset(new PeerConnection(cuid, socket)); } } PeerReceiveHandshakeCommand::~PeerReceiveHandshakeCommand() {} bool PeerReceiveHandshakeCommand::exitBeforeExecute() { return e->isHaltRequested() || e->getRequestGroupMan()->downloadFinished(); } bool PeerReceiveHandshakeCommand::executeInternal() { unsigned char data[BtHandshakeMessage::MESSAGE_LENGTH]; size_t dataLength = BtHandshakeMessage::MESSAGE_LENGTH; // ignore return value. The received data is kept in PeerConnection object // because of peek = true. _peerConnection->receiveHandshake(data, dataLength, true); // To handle tracker's NAT-checking feature if(dataLength >= 48) { // check info_hash std::string infoHash = std::string(&data[28], &data[28+INFO_HASH_LENGTH]); SharedHandle downloadContext = e->getBtRegistry()->getDownloadContext(infoHash); if(downloadContext.isNull()) { throw DL_ABORT_EX (StringFormat("Unknown info hash %s", util::toHex(infoHash).c_str()).str()); } BtObject btObject = e->getBtRegistry()->get (downloadContext->getOwnerRequestGroup()->getGID()); SharedHandle btRuntime = btObject._btRuntime; SharedHandle pieceStorage = btObject._pieceStorage; SharedHandle peerStorage = btObject._peerStorage; if(!btRuntime->ready()) { throw DL_ABORT_EX (StringFormat("Unknown info hash %s", util::toHex(infoHash).c_str()).str()); } TransferStat tstat = downloadContext->getOwnerRequestGroup()->calculateStat(); const unsigned int maxDownloadLimit = downloadContext->getOwnerRequestGroup()->getMaxDownloadSpeedLimit(); unsigned int thresholdSpeed = downloadContext->getOwnerRequestGroup()-> getOption()->getAsInt(PREF_BT_REQUEST_PEER_SPEED_LIMIT); if(maxDownloadLimit > 0) { thresholdSpeed = std::min(maxDownloadLimit, thresholdSpeed); } if((!pieceStorage->downloadFinished() && tstat.getDownloadSpeed() < thresholdSpeed) || btRuntime->lessThanMaxPeers()) { if(peerStorage->addPeer(peer)) { peer->usedBy(cuid); PeerInteractionCommand* command = new PeerInteractionCommand (cuid, downloadContext->getOwnerRequestGroup(), peer, e, btRuntime, pieceStorage, peerStorage, socket, PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE, _peerConnection); e->addCommand(command); if(logger->debug()) { logger->debug(MSG_INCOMING_PEER_CONNECTION, util::itos(cuid).c_str(), util::itos(peer->usedBy()).c_str()); } } } return true; } else { e->addCommand(this); return false; } } } // namespace aria2