void SaveDownloadData(DownloadData dd) { MultiFileDownData mfdd = (MultiFileDownData)dd.Data; for (; mfdd.OutsideRecvIndex < mfdd.RecvCount; ++mfdd.OutsideRecvIndex) { MultiFileOK mfo = mfdd.CompletionList[mfdd.OutsideRecvIndex]; m_ThreadList.AddItem(mfo); continue; if (mfo.Drd.ResType == ResType.MAX) { //新的客户端 if (!SaveClient(mfo)) { LogMgr.Log("客户端保存失败."); m_bError = true; } } else { if (!SaveRes(mfo)) { LogMgr.Log("资源保存失败:" + mfo.Drd.ResType); m_bError = true; } } } }
void CopyBuffToData(MultiFileDownData mfdd, DownChunkData chunk) { RecvFileDataFlag cfd = mfdd.RecvFileList[chunk.FileIdx]; if (cfd.FileData == null) { cfd.FileData = new byte[mfdd.FileList[chunk.FileIdx].ResSize]; } System.Array.Copy(chunk.RecvData, 0, cfd.FileData, chunk.Offset, chunk.RecvSize); cfd.RecvSize += (uint)chunk.RecvSize; }
bool CreateChunkCmdData(DownloadData dd, DownChunkData dcd) { MultiFileDownData mfdd = (MultiFileDownData)dd.Data; DownResData curDrd = mfdd.FileList[dcd.FileIdx]; try { //1.封装命令 if (dd.Type == DownloadType.DOWNLOAD_CHECK_VER) { NetCmdCheckVer cmd = new NetCmdCheckVer(); cmd.SetCmdType(NetCmdType.CMD_EXTRA_CHECK_VER); cmd.Version = ServerSetting.ClientVer; cmd.Plateform = (byte)RuntimeInfo.GetPlatform(); cmd.ScreenWidth = (ushort)Resolution.GetScreenWidth(); cmd.ScreenHeight = (ushort)Resolution.GetScreenHeight(); cmd.FileName = System.Text.Encoding.ASCII.GetBytes(curDrd.ResUrl); cmd.Length = (ushort)mfdd.FileList[0].ResUrl.Length; SetPcakgeMac(cmd, m_PackageName, m_MacAddress); dcd.SendCmdData = NetCmdHelper.CmdToBytes <NetCmdCheckVer>(cmd, 0); } else { byte[] data1 = System.BitConverter.GetBytes(dcd.Offset); byte[] data2 = System.BitConverter.GetBytes(dcd.Length); byte[] dataUrl = System.Text.Encoding.ASCII.GetBytes(curDrd.ResUrl); byte[] sendData = new byte[data1.Length + data2.Length + dataUrl.Length + 1]; System.Array.Copy(data1, 0, sendData, 0, 4); System.Array.Copy(data2, 0, sendData, 4, 4); System.Array.Copy(dataUrl, 0, sendData, 8, dataUrl.Length); NetCmdFileRequest ncf = new NetCmdFileRequest(); ncf.FileName = sendData; ncf.Count = (ushort)(mfdd.FileList.Count | ((int)dcd.XOR << 15)); //高位是1表示要异或 ncf.Length = (ushort)sendData.Length; ncf.SetCmdType(NetCmdType.CMD_EXTRA_FILE_EX); dcd.SendCmdData = NetCmdHelper.CmdToBytes <NetCmdFileRequest>(ncf, 0); } } catch (System.Exception e) { throw new System.Exception("CreateChunkCmdData Err:" + e.ToString()); return(false); } return(true); }
bool DownSingleFiles(DownloadData dd) { MultiFileDownData mfdd = (MultiFileDownData)dd.Data; DownChunkData chunk = new DownChunkData(); chunk.FileIdx = 0; chunk.Length = 0; chunk.Offset = 0; chunk.RecvSize = 0; chunk.RecvData = new byte[128]; chunk.State = ChunkState.CHUNK_CONNECT; if (!InitChunk(dd, chunk, mfdd.FTPIPList[0])) { return(false); } chunk.RecvTick = Utility.GetTickCount(); while (true) { uint tick = Utility.GetTickCount(); if (!DownChunk(dd, chunk, tick)) { dd.Data = mfdd.ExtraData; CloseSocket(chunk); return(false); } else if (chunk.State == ChunkState.CHUNK_COMPLETION) { dd.Data = mfdd.ExtraData; CloseSocket(chunk); if (chunk.RecvData.Length != chunk.Length) { dd.m_Bytes = new byte[chunk.Length]; System.Array.Copy(chunk.RecvData, dd.m_Bytes, chunk.Length); } else { dd.m_Bytes = chunk.RecvData; } return(true); } Thread.Sleep(1); } }
bool DownMultiFiles(DownloadData dd) { MultiFileDownData mfdd = (MultiFileDownData)dd.Data; List <DownChunkData> chunkList = new List <DownChunkData>(); List <DownChunkData> waitList = new List <DownChunkData>(); List <DownChunkData> retryList = new List <DownChunkData>(); List <CheckServerIP> serverList = new List <CheckServerIP>(); bool bHasNoYD = false; for (byte i = 0; i < mfdd.FTPIPList.Count; ++i) { CheckServerIP ssi = new CheckServerIP(i); if (mfdd.FTPIPList[i].ISP == (byte)ISPType.ISP_YD) { ssi.FailedCount = (uint)(MAX_CHUNK_COUNT * 2); } serverList.Add(ssi); if (mfdd.FTPIPList[i].ISP != (byte)ISPType.ISP_YD) { bHasNoYD = true; } } //1.划分Chunk //================================ uint tick = Utility.GetTickCount(); byte ipidx = 0; int CHUNK_SIZE2 = CHUNK_SIZE << 1; uint chunkIdx = 0; for (byte i = 0; i < mfdd.FileList.Count; ++i) { int resSize = (int)mfdd.FileList[i].ResSize; int resOffset = 0; while (resSize > 0) { DownChunkData dcd = new DownChunkData(); dcd.FileIdx = i; if (resSize >= CHUNK_SIZE2) { dcd.Length = CHUNK_SIZE; } else { dcd.Length = resSize; } dcd.ChunkIdx = chunkIdx++; dcd.Offset = resOffset; dcd.RecvSize = 0; dcd.RecvTick = 0; resOffset += dcd.Length; resSize -= dcd.Length; if (chunkList.Count < MAX_CHUNK_COUNT) { do { ipidx = (byte)((ipidx + 1) % serverList.Count); } while (bHasNoYD && mfdd.FTPIPList[ipidx].ISP == (byte)ISPType.ISP_YD); dcd.checkServer = serverList[ipidx]; dcd.checkServer.BeginUse(); if (!InitChunk(dd, dcd, mfdd.FTPIPList[ipidx])) { return(false); } dcd.RecvTick = tick; chunkList.Add(dcd); } else { waitList.Add(dcd); } } } //1.接收Chunk //================================ while (true) { tick = Utility.GetTickCount(); for (int i = 0; i < chunkList.Count;) { DownChunkData chunk = chunkList[i]; if (!DownChunk(dd, chunk, tick)) { CloseSocket(chunk); chunk.checkServer.EndUse(chunk.State != ChunkState.CHUNK_CONNECT && chunk.RecvSize != 0); if (chunk.State == ChunkState.CHUNK_RECV_DATA) { //将接收的数据coy到data CopyBuffToData(mfdd, chunk); chunk.Offset += chunk.RecvSize; chunk.Length -= chunk.RecvSize; chunk.RecvSize = 0; ++chunk.RetryCount; chunk.SendCmdData = null; } if (chunk.RetryCount >= MAX_RETRY_COUNT) { //split if ((chunk.Length > MAX_XOR_SIZE || chunk.XOR == 1) && chunk.Length > 16) { DownChunkData dcd1, dcd2; SplitChunk(ref chunkIdx, chunk, out dcd1, out dcd2); waitList.Add(dcd1); waitList.Add(dcd2); } else { chunk.RetryCount = 0; chunk.XOR = 1; chunk.SendCmdData = null; retryList.Add(chunk); } } else { retryList.Add(chunk); } Utility.ListRemoveAt(chunkList, i); continue; } else if (chunk.State == ChunkState.CHUNK_COMPLETION) { CopyBuffToData(mfdd, chunk); CloseSocket(chunk); chunk.checkServer.EndUse(true); RecvFileDataFlag cfd = mfdd.RecvFileList[chunk.FileIdx]; if (cfd.RecvSize == cfd.FileData.Length) { //完成 MultiFileOK mfo = new MultiFileOK(); mfo.Data = cfd.FileData; mfo.Drd = mfdd.FileList[chunk.FileIdx]; mfdd.CompletionList[mfdd.RecvCount] = mfo; if (++mfdd.RecvCount == mfdd.FileList.Count) { Debug.Log("所有文件完成!"); return(true); } } Utility.ListRemoveAt(chunkList, i); continue; } ++i; }//end for if (chunkList.Count < MAX_CHUNK_COUNT) { tick = Utility.GetTickCount(); DownChunkData dcd = null; if (waitList.Count > 0) { dcd = waitList[0]; waitList.RemoveAt(0); } else if (retryList.Count > 0) { dcd = retryList[0]; retryList.RemoveAt(0); } if (dcd != null) { serverList.Sort(SortCheckServer); serverList[0].BeginUse(); dcd.checkServer = serverList[0]; if (!InitChunk(dd, dcd, mfdd.FTPIPList[dcd.checkServer.ServerIdx])) { return(false); } dcd.RecvTick = Utility.GetTickCount(); chunkList.Add(dcd); } } if (mfdd.RecvCount == mfdd.FileList.Count) { break; } Thread.Sleep(1); } ; //end while return(true); }
void DownMultiFile_OldFTP(DownloadData dd) { byte[] buffer = new byte[65536]; MultiFileDownData mfdd = (MultiFileDownData)dd.Data; for (int i = 0; i < mfdd.FTPIPList.Count; ++i) { ServerIPData sid = mfdd.FTPIPList[i]; if (sid.ISP == (int)ISPType.ISP_DX) { LogMgr.Log("<电信FTP:" + sid.IP + ">"); } else if (sid.ISP == (int)ISPType.ISP_LT) { LogMgr.Log("<联通FTP:" + sid.IP + ">"); } else { LogMgr.Log("<移动FTP:" + sid.IP + ">"); } int retryCount = 0; while (mfdd.RecvCount < mfdd.FileList.Count) { DownResData drd = mfdd.FileList[mfdd.RecvCount]; string url = "ftp://" + sid.IP + "/" + drd.ResUrl; int readSize = 0; try { FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(url); if (ftpRequest == null) { if (retryCount >= MAX_RETRY_COUNT) { break; } else { ++retryCount; Thread.Sleep(500); continue; } } ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; ftpRequest.UseBinary = true; ftpRequest.KeepAlive = false; FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse(); Stream ftpStream = null; if (response == null || (ftpStream = response.GetResponseStream()) == null) { if (retryCount >= MAX_RETRY_COUNT) { break; } else { ++retryCount; Thread.Sleep(500); continue; } } MemoryStream ms = new MemoryStream(); while (true) { int s = ftpStream.Read(buffer, 0, buffer.Length); if (s <= 0) { break; } ms.Write(buffer, 0, s); readSize += s; mfdd.CurrentRecvSize += s; dd.DownloadBytes += (uint)s; } try { ftpStream.Close(); response.Close(); } catch { } if (mfdd.CurrentRecvSize == drd.ResSize) { MultiFileOK mfo = new MultiFileOK(); mfo.Data = ms.ToArray(); mfo.Drd = mfdd.FileList[mfdd.RecvCount]; mfdd.CompletionList[mfdd.RecvCount] = mfo; ++mfdd.RecvCount; if (mfdd.RecvCount == mfdd.FileList.Count) { dd.DownState = DownloadState.DS_DOWNLOADED_OK; if (dd.Type != DownloadType.DOWNLOAD_DEFAULT) { m_CompletionList.AddItem(dd); } return; } else { mfdd.CurrentRecvSize = 0; } } } catch (System.Exception e) { dd.DownloadBytes -= (uint)readSize; LogMgr.Log("OLD_FTP下载失败:" + e.ToString()); if (retryCount >= MAX_RETRY_COUNT) { break; } else { ++retryCount; Thread.Sleep(500); continue; } } } } dd.DownState = DownloadState.DS_DOWNLOADED_ERROR; if (dd.Type != DownloadType.DOWNLOAD_DEFAULT) { m_CompletionList.AddItem(dd); } }
//资源有带前缀"ftp://" public DownloadData AddMultiResDownload(DownloadType dt, List <ServerIPData> serverList, List <DownResData> resList, object obj = null) { if (m_DownList.HasSpace() == false) { return(null); } MultiFileDownData mfdd = new MultiFileDownData(); mfdd.CompletionList = new MultiFileOK[resList.Count]; mfdd.FTPIPList = new List <ServerIPData>(); mfdd.FileList = new List <DownResData>(); mfdd.OutsideRecvIndex = 0; mfdd.RecvCount = 0; mfdd.CurrentRecvSize = 0; mfdd.ExtraData = obj; mfdd.RecvFileList = new RecvFileDataFlag[resList.Count]; List <ServerIPData> ipList1 = new List <ServerIPData>(); List <ServerIPData> ipList2 = new List <ServerIPData>(); for (int i = 0; i < serverList.Count; ++i) { if (serverList[i].ISP == ISP_TYPE || serverList[i].ISP == (byte)ISPType.ISP_YD || ISP_TYPE == (byte)ISPType.ISP_YD) { ipList1.Add(serverList[i]); } //else // ipList2.Add(sip); } if (ipList1.Count == 0) { for (int i = 0; i < serverList.Count; ++i) { ipList1.Add(serverList[i]); } } for (int i = 0; i < resList.Count; ++i) { mfdd.RecvFileList[i] = new RecvFileDataFlag(); DownResData drd = resList[i]; string url = drd.ResUrl.ToLower(); int idx = url.IndexOf("fishpublish"); if (idx == -1) { return(null); } drd.ResUrl = url.Substring(idx, url.Length - idx); mfdd.FileList.Add(drd); } //将FTP排队 for (int i = 0; i < ipList1.Count;) { int idx = Utility.Range(0, ipList1.Count); if (idx >= ipList1.Count) { idx = ipList1.Count - 1; } mfdd.FTPIPList.Add(ipList1[idx]); ipList1.RemoveAt(idx); } for (int i = 0; i < ipList2.Count;) { int idx = Utility.Range(0, ipList2.Count); if (idx >= ipList2.Count) { idx = ipList2.Count - 1; } mfdd.FTPIPList.Add(ipList2[idx]); ipList2.RemoveAt(idx); } DownloadData dd = new DownloadData(null, mfdd, dt); dd.DownState = DownloadState.DS_DOWNLOADING; m_DownList.AddItem(dd); return(dd); }
public void Update(float delta) { if (m_bError) { NativeInterface.ShowMsgAndExit("res_save_error", 109); return; } if (m_ActiveDown != null) { if (m_ActiveDown.Type == DownloadType.DOWNLOAD_MULTI_FILE && m_ActiveDown.DownloadBytes != 0) { } else { m_UpdateUI.SetIsQueue(m_ActiveDown.IsQueue, m_ActiveDown.Type == DownloadType.DOWNLOAD_CHECK_VER); } if (m_ActiveDown.IsQueue) { m_UpdateTick = Utility.GetTickCount(); } } switch (m_State) { case UpdateState.UPDATE_CHECK_LOCALVER: CheckLocalFile(); break; case UpdateState.UPDATE_UNZIPING_FILE: CheckUnzipping(); break; case UpdateState.UPDATE_INIT: m_UpdateTick = Utility.GetTickCount(); string ftpurl = RuntimeInfo.GetFTPVersionURL(); m_ActiveDown = FTPClient.Instance.AddDownload(DownloadType.DOWNLOAD_CHECK_VER, ftpurl, null); m_State = UpdateState.UPDATE_DOWNLOAD_VERSION; break; case UpdateState.UPDATE_DOWNLOAD_VERSION: if (m_ActiveDown.IsOK) { m_RetryCount = 0; string xml = m_ActiveDown.Text; m_ActiveDown = null; if (!CheckVersion(xml)) { NativeInterface.ShowMsgAndExit("update_error", 101); } } else if (m_ActiveDown.HasError) { //出错了 if (++m_RetryCount > MAX_RETRY_COUNT) { NativeInterface.ShowMsgAndExit("res_connect_error", 102); } else { m_State = UpdateState.UPDATE_INIT; m_ActiveDown = null; } } else { if (m_ActiveDown.IsQueue == false && Utility.GetTickCount() - m_UpdateTick > UPDATE_VERSION_TIME_OUT) { NativeInterface.ShowMsgAndExit("res_connect_error", 103); } } break; case UpdateState.UPDATE_DOWNLOAD_RES: if (m_ActiveDown == null) { if (m_DownList.Count > 0) { m_DownloadBytes = 0; m_UpdateTick = Utility.GetTickCount(); m_UpdateUI.BeginDown(ResType.FishRes); m_ActiveDown = FTPClient.Instance.AddMultiResDownload(DownloadType.DOWNLOAD_MULTI_FILE, m_ResFtpList, m_DownList); m_RecvFileCount = m_DownList.Count; m_DownList.Clear(); } else { m_State = UpdateState.UPDATE_COMPLETE; } } else if (m_ActiveDown.IsOK) { m_RetryCount = 0; m_CurrentDownloadBytes += m_ActiveDown.DownloadBytes; m_DownloadBytes = 0; //m_ThreadList.AddItem(m_ActiveDown); SaveDownloadData(m_ActiveDown); m_ActiveDown = null; } else if (m_ActiveDown.HasError) { NativeInterface.ShowMsgAndExit("update_error", 105); } else { if (m_ActiveDown.IsQueue) { //排队中 } else if (m_DownloadBytes != m_ActiveDown.DownloadBytes) { m_DownloadBytes = m_ActiveDown.DownloadBytes; m_UpdateTick = Utility.GetTickCount(); MultiFileDownData mfdd = (MultiFileDownData)m_ActiveDown.Data; if (mfdd.OutsideRecvIndex < mfdd.RecvCount) { SaveDownloadData(m_ActiveDown); } } else if (Utility.GetTickCount() - m_UpdateTick > UPDATE_RES_TIME_OUT) { NativeInterface.ShowMsgAndExit("update_error", 106); } } break; case UpdateState.UPDATE_COMPLETE: //if (m_DownloadCompletionCount != m_DownloadOrgCount) //{ // m_UpdateUI.SetUnzipping(); // return; //} //更新完成,进入下一个逻辑 if (m_SaveCount != m_RecvFileCount) { return; } if (m_bNewClient) { if (RuntimeInfo.GetPlatform() == GamePlatformType.Android) { NativeInterface.DownNewClientVersion(GetClientPath()); } else if (RuntimeInfo.GetPlatform() == GamePlatformType.Windows) { NativeInterface.ShowMsgAndExit("update_restart", 0); } else { NativeInterface.DownNewClientVersion(m_NewClientURL); } } else { LogicManager.Instance.Forward(null); } break; case UpdateState.UPDATE_NEW_CLIENT: LogicManager.Instance.Shutdown(); break; } }