void DownloadBase::OnTransferItemResumed(OpTransferItem* transfer_item) { if(transfer_item == m_transfer_item) { if(IsTrying() == FALSE) { // FIX: memory leak BTInfo *info = OP_NEW(BTInfo, ()); if(info != NULL) { OpString /*filename,*/ target_dir; //((TransferItem *)transfer_item)->GetMetaFile(filename); ((TransferItem *)transfer_item)->GetDownloadDirectory(target_dir); info->GetImpl()->SetTargetDirectory(target_dir); info->GetImpl()->AttachToDownload(this); Resume(); info->LoadTorrentFile(transfer_item); } } }
void CDownload::StartTrying() { ASSERT( ! IsCompleted() || IsSeeding() ); ASSERT( ! IsPaused() ); if ( IsTrying() || IsPaused() || ( IsCompleted() && ! IsSeeding() ) ) return; if ( ! Network.IsConnected() && ! Network.Connect( TRUE ) ) return; m_tBegan = GetTickCount(); }
void CDownload::StopTrying() { if ( ! IsTrying() || ( IsCompleted() && ! IsSeeding() ) ) return; m_tBegan = 0; m_bDownloading = false; // If m_bTorrentRequested = TRUE, sends Stop // CloseTorrent() additionally closes uploads if ( IsTorrent() ) CloseTorrent(); CloseTransfers(); CloseFile(); StopSearch(); SetModified(); }
void CDownload::OnRun() { // Set the currently downloading state // (Used to optimize display in Ctrl/Wnd functions) m_bDownloading = false; const DWORD tNow = GetTickCount(); if ( ! IsPaused() ) { if ( GetFileError() != ERROR_SUCCESS ) { // File or disk errors Pause( FALSE ); } else if ( IsMoving() ) { if ( ! IsCompleted() && ! IsTasking() ) OnDownloaded(); } else if ( IsTrying() || IsSeeding() ) { // This download is trying to download if ( HasHash() ) // Workaround for direct downloads stuck "verifying" OpenDownload(); // Dead Download Check: if download appears dead, give up and allow another to start. // Incomplete, and trying for at least 3 hours: if ( ! IsCompleted() && tNow > GetStartTimer() + ( 3 * 60 * 60 * 1000 ) ) { const DWORD tHoursToTry = min( ( GetEffectiveSourceCount() + 49u ) / 50u, 9lu ) + Settings.Downloads.StarveGiveUp; // No new data for 5-14 hours if ( tNow > m_tReceived + ( tHoursToTry * 60 * 60 * 1000 ) ) { if ( IsTorrent() ) { // Are there other torrents that should start? if ( Downloads.GetTryingCount( TRUE ) >= Settings.BitTorrent.DownloadTorrents ) { StopTrying(); // Give up for now, try again later return; } } else // Regular download { // Are there other downloads that should try? if ( Downloads.GetTryingCount() >= ( Settings.Downloads.MaxFiles + Settings.Downloads.MaxFileSearches ) ) { StopTrying(); // Give up for now, try again later return; } } } } // End Dead Download Check // Run the download RunTorrent( tNow ); RunSearch( tNow ); RunValidation(); if ( IsSeeding() ) { // Mark as collapsed to get correct heights when dragging files if ( ! Settings.General.DebugBTSources && m_bExpanded ) m_bExpanded = FALSE; } else // if ( ! IsMoving() ) { if ( IsComplete() && IsFileOpen() ) { if ( IsFullyVerified() ) OnDownloaded(); } else if ( CheckTorrentRatio() ) { if ( ! Network.IsConnected() ) { StopTrying(); return; } StartTransfersIfNeeded( tNow ); } } // Calculate current downloading state if ( HasActiveTransfers() ) m_bDownloading = true; // Mutate regular download to torrent download if ( Settings.BitTorrent.EnablePromote && m_oBTH && ! IsTorrent() ) { m_pTorrent.Clear(); m_pTorrent.m_oMD5 = m_oMD5; m_pTorrent.m_oBTH = m_oBTH; m_pTorrent.m_oSHA1 = m_oSHA1; m_pTorrent.m_oED2K = m_oED2K; m_pTorrent.m_oTiger = m_oTiger; m_pTorrent.m_sName = m_sName; m_pTorrent.m_nSize = m_nSize; SetTorrent(); } } else if ( ! IsCompleted() && m_bVerify != TRI_TRUE ) { // This is pending download // // If this download isn't trying to download, see if it can try // if ( IsDownloading() ) // SetStartTimer(); // This download was probably started by a push/etc if ( Network.IsConnected() ) { // Have extra regular downloads 'trying' so when a new slot is ready, // a download has sources and is ready to go. if ( Downloads.GetTryingCount() < ( Settings.Downloads.MaxFiles + Settings.Downloads.MaxFileSearches ) ) { if ( ! IsTorrent() || Downloads.GetTryingCount( true ) < Settings.BitTorrent.DownloadTorrents ) { // Torrents only try when 'ready to go'. (Reduce tracker load) Resume(); } } } // else // ASSERT( ! IsTrying() ); } } // Don't save Downloads with many sources too often, since it's slow if ( tNow >= m_tSaved + ( GetCount() > 20 ? 5 * Settings.Downloads.SaveInterval : Settings.Downloads.SaveInterval ) ) { if ( IsModified() ) { FlushFile(); if ( Save() ) m_tSaved = tNow; } } }
CString CDownload::GetDownloadStatus() const { CString strText; if ( m_bClearing ) // Briefly marked for removal { LoadString( strText, IDS_STATUS_CLEARING ); } else if ( IsPaused() ) { if ( GetFileError() == ERROR_SUCCESS || IsSeeding() ) LoadString( strText, IDS_STATUS_PAUSED ); else LoadString( strText, IsMoving() ? IDS_STATUS_CANTMOVE : IDS_STATUS_FILEERROR ); } else if ( IsCompleted() ) { if ( IsSeeding() ) LoadString( strText, m_bTorrentTrackerError ? IDS_STATUS_TRACKERDOWN : IDS_STATUS_SEEDING ); else LoadString( strText, IDS_STATUS_COMPLETED ); } else if ( IsMoving() ) { LoadString( strText, IDS_STATUS_MOVING ); } else if ( IsStarted() && GetProgress() == 100.0f ) { LoadString( strText, IDS_STATUS_VERIFYING ); } else if ( ! IsTrying() ) { LoadString( strText, IDS_STATUS_QUEUED ); } else if ( IsDownloading() ) { const DWORD nTime = GetTimeRemaining(); if ( nTime == 0xFFFFFFFF ) LoadString( strText, IDS_STATUS_ACTIVE ); // IDS_STATUS_DOWNLOADING else if ( nTime == 0 ) LoadString( strText, IDS_STATUS_DOWNLOADING ); else if ( nTime > 86400 ) strText.Format( L"%u:%.2u:%.2u:%.2u", nTime / 86400, ( nTime / 3600 ) % 24, ( nTime / 60 ) % 60, nTime % 60 ); else strText.Format( L"%u:%.2u:%.2u", nTime / 3600, ( nTime / 60 ) % 60, nTime % 60 ); } else if ( GetEffectiveSourceCount() > 0 ) { LoadString( strText, IDS_STATUS_PENDING ); } else if ( IsTorrent() ) { if ( GetTaskType() == dtaskAllocate ) LoadString( strText, IDS_STATUS_CREATING ); else if ( m_bTorrentTrackerError ) LoadString( strText, IDS_STATUS_TRACKERDOWN ); else LoadString( strText, IDS_STATUS_TORRENT ); } else // Inactive { LoadString( strText, IDS_STATUS_QUEUED ); } return strText; }