/// <summary> /// The callback method for the <see cref="IMasterServerReader.BeginReadVersionFileList"/>. /// </summary> /// <param name="sender">The <see cref="IMasterServerReader"/> this event came from.</param> /// <param name="info">The information from the master server(s).</param> /// <param name="userState">An optional state object passed by the caller to supply information to the callback method /// from the method call.</param> void MasterServerReader_Callback_VersionFileList(IMasterServerReader sender, IMasterServerReadInfo info, object userState) { State = UpdateClientState.ReadingLiveVersionFileListDone; // Check for a valid VersionFileList if (string.IsNullOrEmpty(info.VersionFileListText)) { const string errmsg = "Could not get a valid VersionFileList file from the master servers for version `{0}` - download failed."; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, info.Version); if (MasterServerReaderError != null) MasterServerReaderError(this, string.Format(errmsg, info.Version)); HasErrors = true; return; } try { _versionFileList = VersionFileList.CreateFromString(info.VersionFileListText); } catch (Exception ex) { const string errmsg = "Could not get a valid VersionFileList file from the master servers for version `{0}`. Exception: {1}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, info.Version, ex); if (MasterServerReaderError != null) MasterServerReaderError(this, string.Format(errmsg, info.Version, ex)); HasErrors = true; return; } // Find the files to update var toUpdate = FindFilesToUpdate(_versionFileList); // If all file hashes match, then we are good to go if (toUpdate.Count() == 0) { CheckIfDownloadManagerComplete(); return; } // There was one or more files to update, so start the updating... // Create the DownloadManager _dm = new DownloadManager(Settings.TargetPath, Settings.TempPath, info.Version); _dm.DownloadFinished += DownloadManager_DownloadFinished; _dm.FileMoveFailed += DownloadManager_FileMoveFailed; _dm.DownloadFailed += DownloadManager_DownloadFailed; _dm.Finished += DownloadManager_Finished; State = UpdateClientState.UpdatingFiles; // Add the sources to the DownloadManager var sources = info.DownloadSources.Select(x => x.Instantiate()); _dm.AddSources(sources); // Enqueue the files that need to be downloaded _dm.Enqueue(toUpdate); }
/// <summary> /// The callback method for the <see cref="IMasterServerReader.BeginReadVersionFileList"/>. /// </summary> /// <param name="sender">The <see cref="IMasterServerReader"/> this event came from.</param> /// <param name="info">The information from the master server(s).</param> /// <param name="userState">An optional state object passed by the caller to supply information to the callback method /// from the method call.</param> void MasterServerReader_Callback_VersionFileList(IMasterServerReader sender, IMasterServerReadInfo info, object userState) { State = UpdateClientState.ReadingLiveVersionFileListDone; // Check for a valid VersionFileList if (string.IsNullOrEmpty(info.VersionFileListText)) { const string errmsg = "Could not get a valid VersionFileList file from the master servers for version `{0}` - download failed."; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, info.Version); } if (MasterServerReaderError != null) { MasterServerReaderError(this, string.Format(errmsg, info.Version)); } HasErrors = true; return; } try { _versionFileList = VersionFileList.CreateFromString(info.VersionFileListText); } catch (Exception ex) { const string errmsg = "Could not get a valid VersionFileList file from the master servers for version `{0}`. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, info.Version, ex); } if (MasterServerReaderError != null) { MasterServerReaderError(this, string.Format(errmsg, info.Version, ex)); } HasErrors = true; return; } // Find the files to update var toUpdate = FindFilesToUpdate(_versionFileList); // If all file hashes match, then we are good to go if (toUpdate.Count() == 0) { CheckIfDownloadManagerComplete(); return; } // There was one or more files to update, so start the updating... // Create the DownloadManager _dm = new DownloadManager(Settings.TargetPath, Settings.TempPath, info.Version); _dm.DownloadFinished += DownloadManager_DownloadFinished; _dm.FileMoveFailed += DownloadManager_FileMoveFailed; _dm.DownloadFailed += DownloadManager_DownloadFailed; _dm.Finished += DownloadManager_Finished; State = UpdateClientState.UpdatingFiles; // Add the sources to the DownloadManager var sources = info.DownloadSources.Select(x => x.Instantiate()); _dm.AddSources(sources); // Enqueue the files that need to be downloaded _dm.Enqueue(toUpdate); }
/// <summary> /// Checks if the downloading with the <see cref="DownloadManager"/> has been completed. /// </summary> void CheckIfDownloadManagerComplete() { // If the download manager is null, then skip checking it if (_dm != null) { if (!_dm.IsDisposed) { // Do not continue if items are still enqueued for download if (_dm.QueueCount > 0) { const string errmsg = "CheckIfDownloadManagerComplete() called, but items still in the download queue?"; if (log.IsWarnEnabled) log.Warn(errmsg); Debug.Fail(errmsg); return; } // Clean up try { _dm.Dispose(); } catch (Exception ex) { const string errmsg = "Failed to dispose DownloadManager `{0}`. Reason: {1}"; if (log.IsWarnEnabled) log.WarnFormat(errmsg, _dm, ex); Debug.Fail(string.Format(errmsg, _dm, ex)); } } _dm = null; } // Abort if HasErrors is true if (HasErrors) { State = UpdateClientState.Completed; return; } // Clean up Cleanup(); // If done, update the version TrySetClientVersionToLive(); // Change the state State = UpdateClientState.Completed; // Set to not running lock (_isRunningSync) { Debug.Assert(_isRunning); _isRunning = false; try { if (IsRunningChanged != null) IsRunningChanged(this); } catch (NullReferenceException ex) { Debug.Fail(ex.ToString()); } } }
/// <summary> /// Checks if the downloading with the <see cref="DownloadManager"/> has been completed. /// </summary> void CheckIfDownloadManagerComplete() { // If the download manager is null, then skip checking it if (_dm != null) { if (!_dm.IsDisposed) { // Do not continue if items are still enqueued for download if (_dm.QueueCount > 0) { const string errmsg = "CheckIfDownloadManagerComplete() called, but items still in the download queue?"; if (log.IsWarnEnabled) { log.Warn(errmsg); } Debug.Fail(errmsg); return; } // Clean up try { _dm.Dispose(); } catch (Exception ex) { const string errmsg = "Failed to dispose DownloadManager `{0}`. Reason: {1}"; if (log.IsWarnEnabled) { log.WarnFormat(errmsg, _dm, ex); } Debug.Fail(string.Format(errmsg, _dm, ex)); } } _dm = null; } // Abort if HasErrors is true if (HasErrors) { State = UpdateClientState.Completed; return; } // Clean up Cleanup(); // If done, update the version TrySetClientVersionToLive(); // Change the state State = UpdateClientState.Completed; // Set to not running lock (_isRunningSync) { Debug.Assert(_isRunning); _isRunning = false; try { if (IsRunningChanged != null) { IsRunningChanged(this); } } catch (NullReferenceException ex) { Debug.Fail(ex.ToString()); } } }