public void AsyncDownloadList(List <string> list, string remoteDir, string localDir, Action <DownloadTask> onFinish, List <string> exts = null) { lock (_pendingTasks) { int count = list.Count; string url; for (int i = 0; i < count; ++i) { url = list[i]; DownloadTask task = new DownloadTask(); task.file = url; task.storagePath = CombineUrl(localDir, url); task.url = CombineUrl(remoteDir, url); task.onFinish = onFinish; if (!_pendingTasks.Contains(task)) { _pendingTasks.Enqueue(task); } DownloadFileTransferInfo info = _transferMgr.GetDownloadFileInfo(task.file); task.md5 = info.md5; Debug.Log("<color='#ff0000'>[正在下载文件: " + task.url + "</color>"); } if (_downloadThread.isWaitting) { _downloadThread.Notify(); } } }
public BreakpointTransferInfo(DownloadFileInfo[] allNeedHotUpdateFiles /*, string version*/) { //this.version = version; allFiles = new DownloadFileTransferInfo[allNeedHotUpdateFiles.Length]; for (int i = 0; i < allNeedHotUpdateFiles.Length; i++) { allFiles[i] = new DownloadFileTransferInfo(allNeedHotUpdateFiles[i]); } }
private bool ReadBytesFromResponse(DownloadTask task, WebResponse response) { bool okay = false; DownloadFileTransferInfo fileInfo = _transferMgr.GetDownloadFileInfo(task.file); FileUtils.Instance.CheckDirExistsForFile(task.storagePath); using (FileStream fileStream = new FileStream(task.storagePath, task.receivedLength == 0 ? FileMode.Create : FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { try { fileStream.Position = task.receivedLength; byte[] array = new byte[1024]; using (Stream responseStream = response.GetResponseStream()) { int bytesRead = 0; while (task.receivedLength < task.fileLength) { bytesRead = responseStream.Read(array, 0, array.Length); //Debug.Log("############bytesRead=" + bytesRead); fileStream.Write(array, 0, bytesRead); task.receivedLength += bytesRead; _currentTaskReceivedBytes = task.receivedLength; _transferMgr.UpdateFileTransferProgress(fileInfo, task.receivedLength); } okay = true; } if (task.receivedLength != task.fileLength) { string s = string.Format("DownloadThread::ReadBytesFromResponse() - Download length not fit Error:{0}/{1}", task.receivedLength, task.fileLength); CLogger.LogError(s); okay = false; this.OnDownloadFinished(task, new Exception(s)); } } catch (System.Threading.ThreadAbortException) { // 忽略 } catch (Exception ex) { okay = false; string s = ex.Message + "\n" + ex.StackTrace; CLogger.LogError(s); this.OnDownloadFinished(task, ex); } finally { _transferMgr.SaveTransferProgress("DownloadThread->>ReadBytesFromResponse"); } } return(okay); }
public void AsyncDownload(string url, string localPath, Action <DownloadTask> onFinish) { DownloadTask task = new DownloadTask(); task.url = url; task.storagePath = localPath; task.onFinish = onFinish; DownloadFileTransferInfo info = _transferMgr.GetDownloadFileInfo(url); task.md5 = info.md5; EnqueueTask(task); }
/// <summary> /// 更新文件传输进度信息 /// </summary> /// <param name="file"></param> /// <param name="receivedSize"></param> public void UpdateFileTransferProgress(DownloadFileTransferInfo file, long receivedSize) { _totalReceivedBytes += receivedSize - file.receivedSize; file.receivedSize = receivedSize; }
/// <summary> /// 断点续传下载 /// </summary> /// <param name="task"></param> private void DownloadFromBreakPoint(DownloadTask task) { try { Uri uri = new Uri(task.url); _currentTaskFileName = task.file; // Debug.Log("Download1"); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); //// Debug.Log("Download2"); //request.Timeout = TIME_OUT; //request.ReadWriteTimeout = TIME_OUT; //HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //// Debug.Log("Download3"); //long totalLength = response.ContentLength; //response.Close(); //request.Abort(); //Debug.LogWarning(totalLength); DownloadFileTransferInfo dfi = _transferMgr.GetDownloadFileInfo(_currentTaskFileName); long totalLength = dfi.size; long receivedLength = 0L; long toDownloadLength = totalLength; if (File.Exists(task.storagePath)) { //FileInfo fileinfo = new FileInfo(tempFileName); //if (fileinfo.Exists) //{ // receivedLength = fileinfo.Length; // toDownloadLength = totalLength - receivedLength; //} using (FileStream fileStream = new FileStream(task.storagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { receivedLength = fileStream.Length; toDownloadLength = totalLength - receivedLength; fileStream.Close(); } if (receivedLength != dfi.receivedSize) { CLogger.Log(string.Format("DownloadThread::DownloadFromBreakPoint() - break point save receive size is wrong for file[{0}], saveSize={1}, fileSize={2}", _currentTaskFileName, dfi.receivedSize, receivedLength)); } } task.fileLength = totalLength; task.receivedLength = receivedLength; _currentTaskTotalBytes = totalLength; _currentTaskReceivedBytes = receivedLength; bool transferOkay = true; if (toDownloadLength > 0L) { CLogger.Log("DownloadThread::DownloadFromBreakPoint() - start http download, The request url is [" + uri + "] with range [" + receivedLength + "," + totalLength + "]"); HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(uri); request2.Timeout = kTimeOut; request2.KeepAlive = true; request2.ReadWriteTimeout = kTimeOut; request2.AddRange((int)receivedLength, (int)totalLength); HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse(); transferOkay = this.ReadBytesFromResponse(task, response2); response2.Close(); request2.Abort(); } if (transferOkay) { this.OnDownloadFinished(task, null); } } catch (Exception ex) { CLogger.LogError("DownloadThread::DownloadFromBreakPoint() - ex: " + ex.Message + ",stackTrack:" + ex.StackTrace); this.OnDownloadFinished(task, ex); } }