Called for each chunk by one main (GUI) thread.
| 202 | |
| 203 | /// Called for each chunk by one main (GUI) thread. |
| 204 | virtual void OnFinish(long httpOrErrorCode, int64_t begRange, int64_t endRange) |
| 205 | { |
| 206 | #ifdef DEBUG |
| 207 | static threads::ThreadID const id = threads::GetCurrentThreadID(); |
| 208 | ASSERT_EQUAL(id, threads::GetCurrentThreadID(), ("OnFinish called from different threads")); |
| 209 | #endif |
| 210 | |
| 211 | bool const isChunkOk = (httpOrErrorCode == 200); |
| 212 | string const urlError = m_strategy.ChunkFinished(isChunkOk, std::make_pair(begRange, endRange)); |
| 213 | |
| 214 | // remove completed chunk from the list, beg is the key |
| 215 | RemoveHttpThreadByKey(begRange); |
| 216 | |
| 217 | // report progress |
| 218 | if (isChunkOk) |
| 219 | { |
| 220 | m_progress.m_bytesDownloaded += (endRange - begRange) + 1; |
| 221 | if (m_onProgress) |
| 222 | m_onProgress(*this); |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | auto const message = non_http_error_code::DebugPrint(httpOrErrorCode); |
| 227 | LOG(LWARNING, (m_filePath, "HttpRequest error:", message)); |
| 228 | } |
| 229 | |
| 230 | ChunksDownloadStrategy::ResultT const result = StartThreads(); |
| 231 | if (result == ChunksDownloadStrategy::EDownloadFailed) |
| 232 | m_status = httpOrErrorCode == 404 ? DownloadStatus::FileNotFound : DownloadStatus::Failed; |
| 233 | else if (result == ChunksDownloadStrategy::EDownloadSucceeded) |
| 234 | m_status = DownloadStatus::Completed; |
| 235 | |
| 236 | // Save chunks statuses into the resume file. |
| 237 | if (isChunkOk && m_status != DownloadStatus::Completed) |
| 238 | SaveResumeChunks(); |
| 239 | |
| 240 | if (m_status == DownloadStatus::InProgress) |
| 241 | return; |
| 242 | |
| 243 | // 1. Save downloaded chunks if some error occured. |
| 244 | if (m_status == DownloadStatus::Failed || m_status == DownloadStatus::FileNotFound) |
| 245 | SaveResumeChunks(); |
| 246 | |
| 247 | // 2. Free file handle. |
| 248 | CloseWriter(); |
| 249 | |
| 250 | // 3. Clean up resume file with chunks range on success |
| 251 | if (m_status == DownloadStatus::Completed) |
| 252 | { |
| 253 | Platform::RemoveFileIfExists(m_filePath + RESUME_FILE_EXTENSION); |
| 254 | |
| 255 | // Rename finished file to it's original name. |
| 256 | Platform::RemoveFileIfExists(m_filePath); |
| 257 | base::RenameFileX(m_filePath + DOWNLOADING_FILE_EXTENSION, m_filePath); |
| 258 | } |
| 259 | |
| 260 | // 4. Finish downloading. |
| 261 | m_onFinish(*this); |
no test coverage detected