Exemplo n.º 1
0
	/// <param name="result">第一个元素表示是否成功;第二个元素为结果</param>
	private static IEnumerable GetProgramVersionByUrl(String url, ReturnTuple<Boolean, String> result)
	{
		WebClientWithTimeout webClient = new WebClientWithTimeout();
		webClient.Timeout = 30 * 1000;
		DownloadState downloadState = new DownloadState();

		webClient.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs arg) =>
		{
			if (arg.Cancelled)
			{
				downloadState.cancel = true;
			}
			else if (arg.Error != null)	//error occurs
			{
				downloadState.exception = arg.Error;
			}
			else
			{
				downloadState.result = arg.Result;
			}

			downloadState.done = true;
		};

		webClient.DownloadDataAsync(new Uri(PatcherSpace.Utility.AddRandomParamToUrl(url)));
		while (!downloadState.done)
		{
			yield return null;
		}

		if (!downloadState.cancel && downloadState.exception == null)	//success
		{
			result.value_0 = true;
			result.value_1 = Encoding.UTF8.GetString(downloadState.result);
		}
		else
		{
			UnityEngine.Debug.LogWarning("Failed to download version file: " + url);
			if (downloadState.exception != null)
			{
				UnityEngine.Debug.LogWarning(downloadState.exception.ToString());
			}
		}
	}
 private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs args)
 {
     try
     {
         if (WebCalls.Alternative())
         {
             using (WebClient webClient = new WebClient())
             {
                 webClient.Headers.Add("user-agent", "SBRW Launcher " +
                                       Application.ProductVersion + " (+https://github.com/SoapBoxRaceWorld/GameLauncher_NFSW)");
                 webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(this.DownloadManager_DownloadDataCompleted);
                 webClient.CachePolicy            = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                 while (true)
                 {
                     if (this._freeChunks <= 0)
                     {
                         Thread.Sleep(100);
                     }
                     else
                     {
                         lock (this._downloadQueue)
                         {
                             if (this._downloadQueue.Count == 0)
                             {
                                 lock (this._workers)
                                 {
                                     this._workers.Remove((BackgroundWorker)sender);
                                 }
                                 DownloaderHandler._workerCount--;
                                 break;
                             }
                         }
                         string value = null;
                         lock (this._downloadQueue)
                         {
                             value = this._downloadQueue.Last.Value;
                             this._downloadQueue.RemoveLast();
                             lock (this._freeChunksLock)
                             {
                                 this._freeChunks--;
                             }
                         }
                         lock (this._downloadList[value])
                         {
                             if (this._downloadList[value].Status != DownloaderHandler.DownloadStatus.Canceled)
                             {
                                 this._downloadList[value].Status = DownloaderHandler.DownloadStatus.Downloading;
                             }
                         }
                         while (webClient.IsBusy)
                         {
                             Thread.Sleep(100);
                         }
                         webClient.DownloadDataAsync(new Uri(value), value);
                         DownloaderHandler.DownloadStatus status = DownloaderHandler.DownloadStatus.Downloading;
                         while (status == DownloaderHandler.DownloadStatus.Downloading)
                         {
                             status = this._downloadList[value].Status;
                             if (status == DownloaderHandler.DownloadStatus.Canceled)
                             {
                                 break;
                             }
                             Thread.Sleep(100);
                         }
                         if (status == DownloaderHandler.DownloadStatus.Canceled)
                         {
                             webClient.CancelAsync();
                         }
                         lock (this._workers)
                         {
                             if (DownloaderHandler._workerCount > this._maxWorkers || !this._managerRunning)
                             {
                                 this._workers.Remove((BackgroundWorker)sender);
                                 DownloaderHandler._workerCount--;
                                 break;
                             }
                         }
                     }
                 }
             }
         }
         else
         {
             using (WebClientWithTimeout webClient = new WebClientWithTimeout())
             {
                 webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(this.DownloadManager_DownloadDataCompleted);
                 webClient.CachePolicy            = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                 while (true)
                 {
                     if (this._freeChunks <= 0)
                     {
                         Thread.Sleep(100);
                     }
                     else
                     {
                         lock (this._downloadQueue)
                         {
                             if (this._downloadQueue.Count == 0)
                             {
                                 lock (this._workers)
                                 {
                                     this._workers.Remove((BackgroundWorker)sender);
                                 }
                                 DownloaderHandler._workerCount--;
                                 break;
                             }
                         }
                         string value = null;
                         lock (this._downloadQueue)
                         {
                             value = this._downloadQueue.Last.Value;
                             this._downloadQueue.RemoveLast();
                             lock (this._freeChunksLock)
                             {
                                 this._freeChunks--;
                             }
                         }
                         lock (this._downloadList[value])
                         {
                             if (this._downloadList[value].Status != DownloaderHandler.DownloadStatus.Canceled)
                             {
                                 this._downloadList[value].Status = DownloaderHandler.DownloadStatus.Downloading;
                             }
                         }
                         while (webClient.IsBusy)
                         {
                             Thread.Sleep(100);
                         }
                         webClient.DownloadDataAsync(new Uri(value), value);
                         DownloaderHandler.DownloadStatus status = DownloaderHandler.DownloadStatus.Downloading;
                         while (status == DownloaderHandler.DownloadStatus.Downloading)
                         {
                             status = this._downloadList[value].Status;
                             if (status == DownloaderHandler.DownloadStatus.Canceled)
                             {
                                 break;
                             }
                             Thread.Sleep(100);
                         }
                         if (status == DownloaderHandler.DownloadStatus.Canceled)
                         {
                             webClient.CancelAsync();
                         }
                         lock (this._workers)
                         {
                             if (DownloaderHandler._workerCount > this._maxWorkers || !this._managerRunning)
                             {
                                 this._workers.Remove((BackgroundWorker)sender);
                                 DownloaderHandler._workerCount--;
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception Error)
     {
         LogToFileAddons.OpenLog("CDN DOWNLOADER [Background Work DH]", null, Error, null, true);
         Exception exception = Error;
         lock (this._workers)
         {
             this._workers.Remove((BackgroundWorker)sender);
             DownloaderHandler._workerCount--;
         }
     }
 }