Beispiel #1
0
        void Start(DownloadTask task)
        {

            m_Task = task;
            m_Task.Status = DownloadTaskStatus.Doing;

            try
            {
                if (File.Exists(downloadFile))
                {
                    m_FileStream = File.OpenWrite(downloadFile);
                    m_FileStream.Seek(0, SeekOrigin.End);
                    m_StartLength = m_SaveLength = (int)m_FileStream.Length;
                    
                }
                else
                {
                    string directory = Path.GetDirectoryName(m_Task.DownloadPath);
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);

                    }
                    m_FileStream = new FileStream(downloadFile, FileMode.Create, FileAccess.Write;

                }

                if (DownloadAgentStart != null) {
                    DownloadAgentStart(this);
                }

                if (m_StartLength > 0) {
                    m_Helper.Download(m_Task.DownloadUri, m_StartLength, m_Task.UserData);

                }
            }
            catch (Exception exception) {
                OnDownloadAgentHelperError(this, new DownloadAgentHelperErrorEventArgs(exception.Message));
            }
        }
        /// <summary>
        /// 增加下载任务。
        /// </summary>
        /// <param name="downloadPath">下载后存放路径。</param>
        /// <param name="downloadUri">原始下载地址。</param>
        /// <param name="priority">下载任务的优先级。</param>
        /// <param name="userData">用户自定义数据。</param>
        /// <returns>新增下载任务的序列编号。</returns>
        public int AddDownload(string downloadPath, string downloadUri, int priority, object userData)
        {
            if (string.IsNullOrEmpty(downloadPath))
            {
                throw new GameFrameworkException("Download path is invalid.");
            }

            if (string.IsNullOrEmpty(downloadUri))
            {
                throw new GameFrameworkException("Download uri is invalid.");
            }

            if (TotalAgentCount <= 0)
            {
                throw new GameFrameworkException("You must add download agent first.");
            }

            DownloadTask downloadTask = DownloadTask.Create(downloadPath, downloadUri, priority, m_FlushSize, m_Timeout, userData);

            m_TaskPool.AddTask(downloadTask);
            return(downloadTask.SerialId);
        }