public AnotherSmallThreadPool(string _pso2Path, ConcurrentDictionary <string, PSO2File> PSO2filesList) { this._throttlecachespeed = MySettings.GameClientUpdateThrottleCache; MySettings.GameClientUpdateThrottleCacheChanged += this.MySettings_GameClientUpdateThrottleCacheChanged; this.bWorker = new ExtendedBackgroundWorker(); this.bWorker.WorkerReportsProgress = false; this.bWorker.WorkerSupportsCancellation = true; this.bWorker.DoWork += this.Bworker_DoWork; this.bWorker.RunWorkerCompleted += this.Bworker_RunWorkerCompleted; this.SynchronizationContextObject = WebClientPool.SynchronizationContext; this.IsBusy = false; this.PSO2Path = _pso2Path; this.myCheckSumList = new ConcurrentDictionary <string, PSO2FileChecksum>(); this.ResetWork(PSO2filesList); }
private void Bworker_DoWork(object sender, DoWorkEventArgs e) { ExtendedBackgroundWorker bworker = sender as ExtendedBackgroundWorker; string currentfilepath, filemd5, _key; PSO2File _value; PSO2FileChecksum checksumobj; if (_keys.TryDequeue(out _key)) { if (myPSO2filesList.TryGetValue(_key, out _value)) { currentfilepath = null; filemd5 = null; //This hard-coded looks ugly, doesn't it??? if (Leayal.StringHelper.IsEqual(_value.SafeFilename, CommonMethods.CensorFilename, true)) { if (File.Exists(Infos.CommonMethods.PathConcat(this.PSO2Path, _key))) { if (this.myCheckSumList.TryGetValue(_key, out checksumobj)) { currentfilepath = Infos.CommonMethods.PathConcat(this.PSO2Path, checksumobj.RelativePath); FileInfo asd = new FileInfo(currentfilepath); if (asd.Exists && asd.Length == checksumobj.FileSize) { filemd5 = checksumobj.MD5; //Let's slow down a little if (this.ThrottleCacheSpeed > 0) { Thread.Sleep(this.ThrottleCacheSpeed); } } else { currentfilepath = null; } } if (string.IsNullOrEmpty(currentfilepath)) { currentfilepath = Infos.CommonMethods.PathConcat(this.PSO2Path, _key); checksumobj = PSO2FileChecksum.FromFile(this.PSO2Path, currentfilepath); filemd5 = checksumobj.MD5; if (!this.myCheckSumList.TryAdd(checksumobj.RelativePath, checksumobj)) { this.myCheckSumList[checksumobj.RelativePath] = checksumobj; } } if (!string.IsNullOrEmpty(filemd5)) { if (_value.MD5Hash == filemd5) { Interlocked.Increment(ref this._DownloadedFileCount); } else { this.OnStepChanged(new StepEventArgs(string.Format(LanguageManager.GetMessageText("PSO2UpdateManager_DownloadingFile", "Downloading file {0}"), _value.SafeFilename))); try { if (bworker.WebClient.DownloadFile(_value.Url, currentfilepath)) { this.myCheckSumList.TryUpdate(checksumobj.RelativePath, PSO2FileChecksum.FromFile(this.PSO2Path, currentfilepath), checksumobj); Interlocked.Increment(ref this._DownloadedFileCount); } else { _failedList.Add(_key); } } catch (System.Net.WebException) { _failedList.Add(_key); } } } else { this.OnStepChanged(new StepEventArgs(string.Format(LanguageManager.GetMessageText("PSO2UpdateManager_DownloadingFile", "Downloading file {0}"), _value.SafeFilename))); try { if (bworker.WebClient.DownloadFile(_value.Url, currentfilepath)) { this.myCheckSumList.TryUpdate(checksumobj.RelativePath, PSO2FileChecksum.FromFile(this.PSO2Path, currentfilepath), checksumobj); Interlocked.Increment(ref this._DownloadedFileCount); } else { _failedList.Add(_key); } } catch (System.Net.WebException) { _failedList.Add(_key); } } } else if (File.Exists(Infos.CommonMethods.PathConcat(this.PSO2Path, _key + ".backup"))) { if (this.myCheckSumList.TryGetValue(_key, out checksumobj)) { currentfilepath = Infos.CommonMethods.PathConcat(this.PSO2Path, checksumobj.RelativePath + ".backup"); FileInfo asd = new FileInfo(currentfilepath); if (asd.Exists && asd.Length == checksumobj.FileSize) { filemd5 = checksumobj.MD5; //Let's slow down a little if (this.ThrottleCacheSpeed > 0) { Thread.Sleep(this.ThrottleCacheSpeed); } } else { currentfilepath = null; } } if (string.IsNullOrEmpty(currentfilepath)) { currentfilepath = Infos.CommonMethods.PathConcat(this.PSO2Path, _key + ".backup"); checksumobj = PSO2FileChecksum.FromFile(this.PSO2Path, currentfilepath); filemd5 = checksumobj.MD5; if (!this.myCheckSumList.TryAdd(checksumobj.RelativePath, checksumobj)) { this.myCheckSumList[checksumobj.RelativePath] = checksumobj; } } if (!string.IsNullOrEmpty(filemd5)) { if (_value.MD5Hash == filemd5) { Interlocked.Increment(ref this._DownloadedFileCount); } else { this.OnStepChanged(new StepEventArgs(string.Format(LanguageManager.GetMessageText("PSO2UpdateManager_DownloadingFile", "Downloading file {0}"), _value.SafeFilename))); try { if (bworker.WebClient.DownloadFile(_value.Url, currentfilepath)) { currentfilepath = Infos.CommonMethods.PathConcat(this.PSO2Path, _key); using (var myfs = new FileStream(currentfilepath + ".backup", FileMode.Open, FileAccess.Read)) this.myCheckSumList.TryUpdate(checksumobj.RelativePath, new PSO2FileChecksum(currentfilepath, myfs.Length, Leayal.Security.Cryptography.MD5Wrapper.FromStream(myfs)), checksumobj); Interlocked.Increment(ref this._DownloadedFileCount); } else { _failedList.Add(_key); } } catch (System.Net.WebException) { _failedList.Add(_key); } } } else { this.OnStepChanged(new StepEventArgs(string.Format(LanguageManager.GetMessageText("PSO2UpdateManager_DownloadingFile", "Downloading file {0}"), _value.SafeFilename))); try { if (bworker.WebClient.DownloadFile(_value.Url, currentfilepath)) { this.myCheckSumList.TryUpdate(checksumobj.RelativePath, PSO2FileChecksum.FromFile(this.PSO2Path, currentfilepath), checksumobj); Interlocked.Increment(ref this._DownloadedFileCount); } else { _failedList.Add(_key); } } catch (System.Net.WebException) { _failedList.Add(_key); } } } } else { if (this.myCheckSumList.TryGetValue(_key, out checksumobj)) { currentfilepath = Infos.CommonMethods.PathConcat(this.PSO2Path, checksumobj.RelativePath); FileInfo asd = new FileInfo(currentfilepath); if (asd.Exists && asd.Length == checksumobj.FileSize) { filemd5 = checksumobj.MD5; //Let's slow down a little if (this.ThrottleCacheSpeed > 0) { Thread.Sleep(this.ThrottleCacheSpeed); } } else { currentfilepath = null; } } if (string.IsNullOrEmpty(currentfilepath)) { currentfilepath = Infos.CommonMethods.PathConcat(this.PSO2Path, _key); checksumobj = PSO2FileChecksum.FromFile(this.PSO2Path, currentfilepath); filemd5 = checksumobj.MD5; if (!this.myCheckSumList.TryAdd(checksumobj.RelativePath, checksumobj)) { this.myCheckSumList[checksumobj.RelativePath] = checksumobj; } } if (!string.IsNullOrEmpty(filemd5)) { if (_value.MD5Hash == filemd5) { Interlocked.Increment(ref this._DownloadedFileCount); } else { this.OnStepChanged(new StepEventArgs(string.Format(LanguageManager.GetMessageText("PSO2UpdateManager_DownloadingFile", "Downloading file {0}"), _value.SafeFilename))); if (bworker.WebClient.DownloadFile(_value.Url, currentfilepath)) { this.myCheckSumList.TryUpdate(checksumobj.RelativePath, PSO2FileChecksum.FromFile(this.PSO2Path, currentfilepath), checksumobj); Interlocked.Increment(ref this._DownloadedFileCount); } else { _failedList.Add(_key); } } } else { this.OnStepChanged(new StepEventArgs(string.Format(LanguageManager.GetMessageText("PSO2UpdateManager_DownloadingFile", "Downloading file {0}"), _value.SafeFilename))); if (bworker.WebClient.DownloadFile(_value.Url, currentfilepath)) { this.myCheckSumList.TryUpdate(checksumobj.RelativePath, PSO2FileChecksum.FromFile(this.PSO2Path, currentfilepath), checksumobj); Interlocked.Increment(ref this._DownloadedFileCount); } else { _failedList.Add(_key); } } } } } Interlocked.Increment(ref this._FileCount); this.OnProgressChanged(new DetailedProgressChangedEventArgs(this.FileCount, this.FileTotal)); if (bworker.CancellationPending) { e.Cancel = true; } }