private void RaiseDownloadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            _culcDownloadSpeedStopwatch.Reset();
            _limitRaiseEventSw.Reset();
            DownloadCompletedArgs args = new DownloadCompletedArgs();

            args.ReceivedBytes = TotalSize;
            NowDownloading     = false;
            DownloadFileCompleted?.Invoke(this, args, UserToken);
        }
예제 #2
0
        public async void OnDownloadCompleted(DownloadClient <DownloadItem> sender, DownloadCompletedArgs e, DownloadItem userToken)
        {
            _clients.Add(sender); //클라이언트 큐에 추가


            Downloaded++; //다운로드된 아이템 추가(DownloadFileAsync는 비동기-동기화를 쓰기때문에 InterLocked를 쓸 필요가 없다.)

            DownloadItem di = userToken;
            FileDownloadCompletedArgs args = new FileDownloadCompletedArgs();

            args.DownloadedItem     = di;
            args.DownloadQueue      = _filequeue.Count;
            args.ClientId           = sender.ClientId;
            args.Downloaded         = Downloaded;
            args.DownloadToComplete = DownloadToComplete;
            args.ProgressPercentage = Math.Round((double)(100 * Downloaded) / DownloadToComplete);

            if (di.UseFileIntegrityCheck) // 파일 무결성 검사
            {
                args.DownloadedFileHash = await FileHashing.GetFileHashAsync(di.Path, di.HashType);

                if (di.FileHash != args.DownloadedFileHash)
                {
                    args.FileIntegrity = false;
                }
                else
                {
                    args.FileIntegrity = true;
                }
            }


            if (di.UseAutoRedownloadWhenFileIsCracked) //파일 깨졌다면 재다운로드
            {
                if (!args.FileIntegrity)
                {
                    if (di.UseTempPath)
                    {
                        File.Delete(di.TempPath);
                    }
                    else
                    {
                        File.Delete(di.Path);
                    }
                    EnqueueItem(userToken);
                }
            }

            if (di.UseTempPath)
            {
                string path = Path.GetDirectoryName(di.Path);
                if (!System.IO.Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string temppath = Path.GetDirectoryName(di.TempPath);
                if (!System.IO.Directory.Exists(temppath))
                {
                    Directory.CreateDirectory(temppath);
                }
                File.Delete(di.Path);
                File.Move(di.TempPath, di.Path);
            }
            RaiseDownloadCompleted(args);
            if (Downloaded == DownloadToComplete)
            {
                RaiseAllDownloadCompleted(new AllFileDownloadCompletedArgs());
            }
        }