/// <summary> /// 开始处理下载任务。 /// </summary> /// <param name="task">要处理的下载任务。</param> /// <returns>开始处理任务的状态。</returns> public StartTaskStatus Start(DownloadTask task) { if (task == null) { throw new GameFrameworkException("Task is invalid."); } m_Task = task; m_Task.Status = DownloadTaskStatus.Doing; string downloadFile = Utility.Text.Format("{0}.download", m_Task.DownloadPath); try { if (File.Exists(downloadFile)) { m_FileStream = File.OpenWrite(downloadFile); m_FileStream.Seek(0, SeekOrigin.End); m_StartLength = m_SavedLength = (int)m_FileStream.Length; m_DownloadedLength = 0; } else { string directory = Path.GetDirectoryName(m_Task.DownloadPath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } m_FileStream = new FileStream(downloadFile, FileMode.Create, FileAccess.Write); m_StartLength = m_SavedLength = m_DownloadedLength = 0; } if (DownloadAgentStart != null) { DownloadAgentStart(this); } if (m_StartLength > 0) { m_Helper.Download(m_Task.DownloadUri, m_StartLength, m_Task.UserData); } else { m_Helper.Download(m_Task.DownloadUri, m_Task.UserData); } return(StartTaskStatus.CanResume); } catch (Exception exception) { DownloadAgentHelperErrorEventArgs downloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(exception.Message); OnDownloadAgentHelperError(this, downloadAgentHelperErrorEventArgs); ReferencePool.Release(downloadAgentHelperErrorEventArgs); return(StartTaskStatus.UnknownError); } }
/// <summary> /// 开始处理下载任务。 /// </summary> /// <param name="task">要处理的下载任务。</param> public void Start(DownloadTask task) { if (task == null) { throw new AshException("Task is invalid."); } m_Task = task; m_Task.Status = DownloadTaskStatus.Doing; string downloadFile = string.Format("{0}.download", m_Task.DownloadPath); try { if (File.Exists(downloadFile)) { m_FileStream = File.OpenWrite(downloadFile); m_FileStream.Seek(0, SeekOrigin.End); m_StartLength = m_SavedLength = (int)m_FileStream.Length; m_DownloadedLength = 0; } else { string directory = Path.GetDirectoryName(m_Task.DownloadPath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } m_FileStream = new FileStream(downloadFile, FileMode.Create, FileAccess.Write); m_StartLength = m_SavedLength = m_DownloadedLength = 0; } if (DownloadAgentStart != null) { DownloadAgentStart(this); } if (m_StartLength > 0) { m_Helper.Download(m_Task.DownloadUri, m_StartLength, m_Task.UserData); } else { m_Helper.Download(m_Task.DownloadUri, m_Task.UserData); } } catch (Exception exception) { OnDownloadAgentHelperError(this, new DownloadAgentHelperErrorEventArgs(exception.Message)); } }