Esempio n. 1
0
        private void ReadHttpStream(HTTPRequest request, HTTPResponse response)
        {
            string localfile = this.m_DownloadInfo.tempPath;

            try
            {
                List <byte[]> streamedFragments = response.GetStreamedFragments();
                if (streamedFragments != null)
                {
                    int num = 0;
                    using (FileStream fileStream = new FileStream(localfile, FileMode.Append))
                    {
                        foreach (byte[] array in streamedFragments)
                        {
                            num += array.Length;
                            fileStream.Write(array, 0, array.Length);
                        }
                    }
                    this.m_DownloadInfo.downloadedSize += num;
                    m_DownloadInfo?.OnProgressChange();
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(System.IO.IOException))
                {
                    if (Utils.IsDiskFull(ex))
                    {
                        EventManager.Instance.Trigger <SDKEvents.DownloadFileEvent>().Data(this.m_DownloadInfo.fileName, "DiskFullException", this.m_DownloadInfo.downloadedSize).Trigger();
                    }
                    else
                    {
                        EventManager.Instance.Trigger <SDKEvents.DownloadFileEvent>().Data(this.m_DownloadInfo.fileName, "IOException", this.m_DownloadInfo.downloadedSize).Trigger();
                    }
                }
                else
                {
                    EventManager.Instance.Trigger <SDKEvents.DownloadFileEvent>().Data(this.m_DownloadInfo.fileName, "failure", this.m_DownloadInfo.downloadedSize).Trigger();
                }
                DebugUtil.Log("An error occured while downloading {0} due to {1}.Cancelling!", localfile, ex);
                this.m_DownloadInfo.result = DownloadResult.Failed;

                CancelDownload(true);
                this.CancelDownload(true);
                return;
            }

            float num2 = (float)this.m_DownloadInfo.downloadedSize / (float)this.m_DownloadInfo.downloadSize;

            this.m_DownloadInfo.currProgress = num2;

            //DebugUtil.Log("Downloading {0} Status: Range {1}/{2} ({3:0.00})", this.m_DownloadInfo.fileName, this.m_DownloadInfo.downloadedSize, this.m_DownloadInfo.downloadSize, this.m_DownloadInfo.currProgress);
            if (!response.IsStreamingFinished || request.State != HTTPRequestStates.Finished)
            {
                return;
            }

            DebugUtil.Log("Download finished : {0}", this.m_DownloadInfo.savePath);
            this.m_DownloadInfo.currProgress = 1.0f;

            // 续传完成后,做一手MD5验证
            string localMd5 = AssetUtils.BuildFileMd5(localfile);

            if (localMd5.Trim() != m_DownloadInfo.fileMd5.Trim())
            {
                //md5验证失败,删除临时文件
                if (File.Exists(localfile))
                {
                    File.Delete(localfile);
                }

                DebugUtil.Log("md5 error, retry download:" + localfile + " =>" + localMd5 + " ## " + m_DownloadInfo.fileMd5);
                this.m_DownloadInfo.result       = DownloadResult.Md5Error;
                this.m_DownloadInfo.currProgress = 0.0f;
                CancelDownload(true);
            }
            else
            {// md5验证通过,临时文件转为最终文件
                string localfile2 = m_DownloadInfo.savePath;
                if (File.Exists(localfile2))
                {
                    File.Delete(localfile2);
                }

                DebugUtil.Log("md5 check success, move temp to final :" + this.m_DownloadInfo.fileName);
                File.Move(localfile, localfile2);
                this.m_DownloadInfo.result = DownloadResult.Success;
                DownloadManager.Instance.FinishDownloadTask(this.m_DownloadInfo);
            }

            EventManager.Instance.Trigger <SDKEvents.DownloadFileEvent>().Data(this.m_DownloadInfo.fileName, "finish", m_DownloadInfo.downloadedSize).Trigger();
        }