void OnHttpRead(HttpClientResponse response, long totalRead) { FileThreadInfo info = response.UserData as FileThreadInfo; if (info != null && m_Data != null && info.fileIdx >= 0) { AutoUpdateCfgItem item = m_Data[info.fileIdx]; long delta = totalRead - response.ReadBytes; if (delta > 0) { double curM = AutoUpdateMgr.Instance.CurDownM; curM += ((double)delta) / ((double)1024 * 1024); AutoUpdateMgr.Instance.CurDownM = curM; item.readBytes += delta; float process; double maxM = AutoUpdateMgr.Instance.TotalDownM; if (maxM > float.Epsilon) { process = (float)(curM / maxM); if (process > 1f) { process = 1f; } } else { process = 0f; } AutoUpdateMgr.Instance.DownProcess = process; } if (totalRead >= response.MaxReadBytes) { item.isDone = true; } m_Data[info.fileIdx] = item; lock (m_Lock) { AutoUpdateMgr.Instance.DownloadUpdateToUpdateTxt(item); } if (totalRead >= response.MaxReadBytes) { lock (m_Lock) { info.Reset(); } StartNextDown(); } } }
// 只是关闭下载线程 public void Stop() { lock (m_Lock) { if (m_Clients != null) { for (int i = 0; i < m_Clients.Length; ++i) { FileThreadInfo client = m_Clients[i]; client.Reset(); } } } FileIdx = -1; HasDownError = false; }
public HttpClientThreadFileStream(AutoUpdateCfgItem[] data, int maxThreadCnt = 5) { m_Data = data; if (maxThreadCnt > 0) { lock (m_Lock) { m_Clients = new FileThreadInfo[maxThreadCnt]; for (int i = 0; i < m_Clients.Length; ++i) { FileThreadInfo info = new FileThreadInfo(); info.Reset(); m_Clients[i] = info; } } } }
void OnHttpError(HttpClientResponse response, int status) { FileThreadInfo info = response.UserData as FileThreadInfo; if (info != null && info.fileIdx >= 0) { lock (m_Lock) { if (m_Data != null && m_Data.Length > 0) { var item = m_Data[info.fileIdx]; UnityEngine.Debug.LogErrorFormat("[downloadFileErr]{0} download: {1:D} isOk: {2}", item.fileContentMd5, item.readBytes, item.isDone.ToString()); } info.Reset(); } } HasDownError = true; StartNextDown(); }
// 开始当前位置下载 private void StartNextDown() { bool isOver = true; #if UNITY_EDITOR int startCnt = 0; #endif lock (m_Lock) { if (m_Data == null || m_Data.Length <= 0) { return; } // 获得空闲线程 FileThreadInfo idleThread = null; for (int i = 0; i < m_Clients.Length; ++i) { var info = m_Clients[i]; if (info.IsIdle) { idleThread = info; break; } } if (idleThread == null) { return; } ++m_FileIdx; while (m_FileIdx < m_Data.Length) { var item = m_Data[m_FileIdx]; if (item.isDone) { // 下载大小在一开始已经计算过了,所以这里不计算 ++m_FileIdx; continue; } #if UNITY_EDITOR ++startCnt; #endif isOver = false; string url = string.Format("{0}/{1}/{2}", AutoUpdateMgr.Instance.ResServerAddr, AutoUpdateMgr.Instance.CurrServeResrVersion, item.fileContentMd5); #if UNITY_EDITOR UnityEngine.Debug.LogFormat("开始下载: {0}", item.fileContentMd5); #endif HttpClient client = AutoUpdateMgr.Instance.CreateMultHttpFile(url, item.readBytes, OnHttpRead, OnHttpError); if (client != null) { HttpClientResponse rep = client.Listener as HttpClientResponse; if (rep != null) { idleThread.client = client; idleThread.fileIdx = m_FileIdx; rep.UserData = idleThread; } } // 查找下一个空闲的线程 idleThread = null; for (int i = 0; i < m_Clients.Length; ++i) { var info = m_Clients[i]; if (info.IsIdle) { idleThread = info; break; } } if (idleThread == null) { break; } ++m_FileIdx; } } if (isOver) { CallEndEvent(); } #if UNITY_EDITOR UnityEngine.Debug.LogFormat("同时下载数量:{0:D}", startCnt); #endif }