Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 public void SaveTransferProgress(string msg)
 {
     _transferMgr.SaveTransferProgress(msg);
 }