public override void Update(float elapseSeconds, float realElapseSeconds) { if (m_UnityWebRequest == null) { return; } if (!m_UnityWebRequest.isDone) { return; } bool isError = false; #if UNITY_2017_1_OR_NEWER isError = m_UnityWebRequest.isNetworkError || m_UnityWebRequest.isHttpError; #else isError = m_UnityWebRequest.isError; #endif if (isError) { DownloadAgentHelperErrorEventArgs downloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(m_UnityWebRequest.responseCode == RangeNotSatisfiableErrorCode, m_UnityWebRequest.error); m_DownloadAgentHelperErrorEventHandler(this, downloadAgentHelperErrorEventArgs); ReferencePool.Release(downloadAgentHelperErrorEventArgs); } else { //DownloadAgentHelperProgressEventArgs downloadAgentHelperProgressEventArgs = DownloadAgentHelperProgressEventArgs.Create(1.0f); //m_DownloadAgentHelperProgressEventHandler(this, downloadAgentHelperProgressEventArgs); //ReferencePool.Release(downloadAgentHelperProgressEventArgs); DownloadAgentHelperCompleteEventArgs downloadAgentHelperCompleteEventArgs = DownloadAgentHelperCompleteEventArgs.Create((int)m_UnityWebRequest.downloadedBytes); m_DownloadAgentHelperCompleteEventHandler(this, downloadAgentHelperCompleteEventArgs); ReferencePool.Release(downloadAgentHelperCompleteEventArgs); } }
private void OnDownloadAgentHelperUpdateBytes(object sender, DownloadAgentHelperUpdateBytesEventArgs e) { m_WaitTime = 0f; try { m_FileStream.Write(e.GetBytes(), e.Offset, e.Length); m_WaitFlushSize += e.Length; m_SavedLength += e.Length; if (m_WaitFlushSize >= m_Task.FlushSize) { m_FileStream.Flush(); m_WaitFlushSize = 0; } } catch (Exception exception) { DownloadAgentHelperErrorEventArgs downloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(false, exception.ToString()); OnDownloadAgentHelperError(this, downloadAgentHelperErrorEventArgs); ReferencePool.Release(downloadAgentHelperErrorEventArgs); } }
/// <summary> /// 开始处理下载任务。 /// </summary> /// <param name="task">要处理的下载任务。</param> /// <returns>开始处理任务的状态。</returns> public void Start(DownloadTask task) { if (task == null) { Debug.LogError("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); } //return StartTaskStatus.CanResume; } catch (Exception exception) { Debug.LogError(exception.Message); DownloadAgentHelperErrorEventArgs downloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(false, exception.ToString()); OnDownloadAgentHelperError(this, downloadAgentHelperErrorEventArgs); ReferencePool.Release(downloadAgentHelperErrorEventArgs); //return StartTaskStatus.UnknownError; } }
/// <summary> /// 下载代理轮询。 /// </summary> /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param> /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param> public void Update(float elapseSeconds, float realElapseSeconds) { if (m_Task.Status == DownloadTaskStatus.Doing) { m_WaitTime += realElapseSeconds; if (m_WaitTime >= m_Task.Timeout) { DownloadAgentHelperErrorEventArgs downloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(false, "Timeout"); OnDownloadAgentHelperError(this, downloadAgentHelperErrorEventArgs); ReferencePool.Release(downloadAgentHelperErrorEventArgs); } m_Helper.Update(elapseSeconds, realElapseSeconds); } }
private void Update() { if (m_WWW == null) { return; } int deltaLength = m_WWW.bytesDownloaded - m_LastDownloadedSize; if (deltaLength > 0) { m_LastDownloadedSize = m_WWW.bytesDownloaded; DownloadAgentHelperUpdateLengthEventArgs downloadAgentHelperUpdateLengthEventArgs = DownloadAgentHelperUpdateLengthEventArgs.Create(deltaLength); m_DownloadAgentHelperUpdateLengthEventHandler(this, downloadAgentHelperUpdateLengthEventArgs); ReferencePool.Release(downloadAgentHelperUpdateLengthEventArgs); } if (m_WWW == null) { return; } if (!m_WWW.isDone) { return; } if (!string.IsNullOrEmpty(m_WWW.error)) { DownloadAgentHelperErrorEventArgs dodwnloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(m_WWW.error.StartsWith(RangeNotSatisfiableErrorCode.ToString()), m_WWW.error); m_DownloadAgentHelperErrorEventHandler(this, dodwnloadAgentHelperErrorEventArgs); ReferencePool.Release(dodwnloadAgentHelperErrorEventArgs); } else { byte[] bytes = m_WWW.bytes; DownloadAgentHelperUpdateBytesEventArgs downloadAgentHelperUpdateBytesEventArgs = DownloadAgentHelperUpdateBytesEventArgs.Create(bytes, 0, bytes.Length); m_DownloadAgentHelperUpdateBytesEventHandler(this, downloadAgentHelperUpdateBytesEventArgs); ReferencePool.Release(downloadAgentHelperUpdateBytesEventArgs); DownloadAgentHelperCompleteEventArgs downloadAgentHelperCompleteEventArgs = DownloadAgentHelperCompleteEventArgs.Create(bytes.Length); m_DownloadAgentHelperCompleteEventHandler(this, downloadAgentHelperCompleteEventArgs); ReferencePool.Release(downloadAgentHelperCompleteEventArgs); } }