/// <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> /// Adds the text read for the <see cref="VersionFileList"/>. This method will use a "best guess" approach /// to determine what text to use when multiple different texts are added. /// </summary> /// <param name="text">The text for the <see cref="VersionFileList"/>.</param> public void AddVersionFileListText(string text) { if (log.IsDebugEnabled) { log.DebugFormat("Adding VersionFileList text `{0}` to MasterServerReadInfo `{1}`.", text, this); } lock (_versionFileListTextSync) { // If the text is exactly the same as the current _versionFileListText, then just ignore it since there is obviously // nothing to be done if (StringComparer.Ordinal.Equals(text, _versionFileListText)) { return; } // Try to parse the text as a VersionFileList to see if it is valid bool textIsValid; try { VersionFileList.CreateFromString(text); textIsValid = true; } catch (InvalidDataException) { // Ignore InvalidDataException since its expected when the version is invalid textIsValid = false; } catch (Exception ex) { // For any other exception, it still must be invalid, but check it out when debugging Debug.Fail(ex.ToString()); textIsValid = false; } // If no value set, just use the text no matter what if (string.IsNullOrEmpty(_versionFileListText)) { _versionFileListText = text; _currVersionFileListTextLegal = textIsValid; return; } // The _versionFileListText already exists. If the current text is invalid but the new text is valid, then // just use the new text. if (!_currVersionFileListTextLegal && textIsValid) { _versionFileListText = text; _currVersionFileListTextLegal = textIsValid; return; } // Likewise, if the current text is valid but the new text is invalid, do not use the new text if (_currVersionFileListTextLegal && !textIsValid) { return; } // From here, they are either both valid or both invalid. To keep things simple, just assume that the larger // of the two strings are "better". So change to the new text if it is longer. if (text.Length > _versionFileListText.Length) { _versionFileListText = text; _currVersionFileListTextLegal = textIsValid; return; } } }