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); }
public DownloadData AddDownload(DownloadType dt, string fileURL, object obj, string ip = null, ushort port = ushort.MaxValue) { //fileURL = "ftp://61.153.110.167/FishPublish/test/test_version/android_hd_version_com.leduo.buyu.test.xml"; if (m_DownList.HasSpace() == false) { return(null); } try { if (ip == null) { //兼容旧格式,要从fileURL中解析网址 int idx = fileURL.IndexOf("ftp://"); if (idx == -1) { return(null); } idx += 6; int idx2 = fileURL.IndexOf("/", idx); if (idx2 == -1) { return(null); } ip = fileURL.Substring(idx, idx2 - idx); port = ServerSetting.ResFtpPort; fileURL = fileURL.Substring(idx2 + 1); //fileURL = "fishpublish/test/test_version/windows_hd_version.xml"; } List <ServerIPData> serverList = new List <ServerIPData>(); List <DownResData> resList = new List <DownResData>(); ServerIPData sid = new ServerIPData(); sid.IP = ip; sid.Port = port; serverList.Add(sid); DownResData drd = new DownResData(); drd.ResUrl = fileURL; drd.ResSize = 0; resList.Add(drd); return(AddMultiResDownload(dt, serverList, resList, obj)); } catch (System.Exception e) { ReportException.Instance.AddException("AddDownload Exception:" + e.ToString()); return(null); } }
bool SaveClient(MultiFileOK dd) { //验证crc DownResData drd = dd.Drd; if (drd.ResSize != dd.Data.Length) { LogMgr.Log("Client:" + drd.ResType + "大小不匹配,localSize:" + dd.Data.Length + ", serverSize:" + drd.ResSize); return(false); } uint crc = Crc.Crc32(dd.Data, 0, dd.Data.Length); if (crc != drd.ResCrc) { LogMgr.Log("Client:" + drd.ResType + "资源检验失败, size:" + dd.Data.Length + ", localCrc:" + crc + ", serverCrc:" + drd.ResCrc); return(false); } if (RuntimeInfo.GetPlatform() == GamePlatformType.Windows) { SaveWindowClientRes(dd.Data); return(true); } string path = GetClientPath(); try { File.Delete(path); } catch { } FileStream fs = File.Create(path); if (fs == null) { LogMgr.Log("Client文件创建失败:" + drd.ResType); return(false); } fs.Write(dd.Data, 0, dd.Data.Length); fs.Flush(); fs.Close(); return(true); }
public void Test() { return; List <ServerIPData> ipList = new List <ServerIPData>(); List <DownResData> resList = new List <DownResData>(); ServerIPData sip = new ServerIPData(); sip.IP = "127.0.0.1"; //"61.153.110.167";//"192.168.1.150";// sip.Port = 45045; sip.ISP = 0; ipList.Add(sip); for (int i = 1; i <= 6; ++i) { DownResData drd = new DownResData(); drd.ResUrl = "ftp://127.0.0.1/FishPublish/rar/" + i + ".rar"; resList.Add(drd); } AddMultiResDownload(DownloadType.DOWNLOAD_MULTI_FILE, ipList, resList); //AddDownload(DownloadType.DOWNLOAD_DEFAULT, "ftp://127.0.0.1/FishPublish/test.rar", null); //AddDownload(DownloadType.DOWNLOAD_DEFAULT, "ftp://101.64.233.10/FishPublish/test.rar", null); //AddDownload(DownloadType.DOWNLOAD_DEFAULT, "ftp://61.153.110.167/FishPublish/test.rar", null); }
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 bool CheckVersion(string xml) { XmlDocument doc = new XmlDocument(); if (doc == null) { return(false); } doc.LoadXml(xml); XmlElement ele = doc.DocumentElement; if (ele == null) { return(false); } XmlNode nodeVer = ele.SelectSingleNode("Net"); if (nodeVer == null) { return(false); } string resftp = nodeVer.Attributes["resftp"].Value; string runftp = nodeVer.Attributes["runftp"].Value; string clientUrl = nodeVer.Attributes["clienturl"].Value; if (nodeVer.Attributes["ftpport"] != null) { ServerSetting.ResFtpPort = ushort.Parse(nodeVer.Attributes["ftpport"].Value); } ServerSetting.SetRunFTP(runftp); ServerSetting.ServerList.Clear(); for (int i = 0; i < nodeVer.ChildNodes.Count; ++i) { XmlNode node = nodeVer.ChildNodes[i]; if (node.Name == "IP") { ServerIPData sid = new ServerIPData(); sid.IP = node.FirstChild.Value; sid.Port = ushort.Parse(node.Attributes["port"].Value); ServerSetting.ServerList.Add(sid); } else if (node.Name == "FTP") { ServerIPData rfd = new ServerIPData(); rfd.IP = node.FirstChild.Value; rfd.Port = ushort.Parse(node.Attributes["port"].Value); rfd.ISP = byte.Parse(node.Attributes["dx"].Value); m_ResFtpList.Add(rfd); } } ServerSetting.SERVICES_URL = "";// ele.SelectSingleNode("Services").FirstChild.Value; XmlNode reporterNode = ele.SelectSingleNode("Reporter"); if (reporterNode != null) { ServerSetting.ReporterIP = reporterNode.FirstChild.Value; ServerSetting.ReporterPort = ushort.Parse(reporterNode.Attributes["port"].Value); } XmlNode ShareNode = ele.SelectSingleNode("Share"); if (ShareNode != null) { string wxid = "", wxpwd = "", sinaid = "", sinapwd = "", qqid = "", qqpwd = ""; XmlNode xn = ShareNode.ChildNodes[0]; if (xn != null) { wxid = xn.Attributes["id"].Value; wxpwd = xn.Attributes["pwd"].Value; } xn = ShareNode.ChildNodes[1]; if (xn != null) { sinaid = xn.Attributes["id"].Value; sinapwd = xn.Attributes["pwd"].Value; } xn = ShareNode.ChildNodes[2]; if (xn != null) { qqid = xn.Attributes["id"].Value; qqpwd = xn.Attributes["pwd"].Value; } NativeInterface.InitShare(wxid, wxpwd, sinaid, sinapwd, qqid, qqpwd); xn = ShareNode.ChildNodes[3]; if (xn != null) { ServerSetting.ShareWebUrl = xn.Attributes["weburl"].Value; ServerSetting.ShareTxt = xn.Attributes["txt"].Value; ServerSetting.ShareImgUrl = xn.Attributes["imgurl"].Value; } } //读取现有的资源,进行比较 XmlNode clientNode = ele.SelectSingleNode("Client"); string clientVer = clientNode.FirstChild.Value;; uint clientCrc = uint.Parse(clientNode.Attributes["crc"].Value); uint clientSize = uint.Parse(clientNode.Attributes["size"].Value); if (clientNode.Attributes["resver"] != null) { ServerSetting.RES_VERSION = uint.Parse(clientNode.Attributes["resver"].Value); } if (clientNode.Attributes["ping"] != null) { ServerSetting.SHOW_PING = uint.Parse(clientNode.Attributes["ping"].Value) != 0; } if (clientNode.Attributes["extrabtn"] != null) { ServerSetting.ShowExtraBtn = uint.Parse(clientNode.Attributes["extrabtn"].Value) != 0; } if (clientNode.Attributes["ftp_newftp"] != null) { FTPClient.USE_NEW_FTP = byte.Parse(clientNode.Attributes["ftp_newftp"].Value); } if (clientNode.Attributes["showgame"] != null) { ServerSetting.ShowGame = byte.Parse(clientNode.Attributes["showgame"].Value) != 0; } if (clientNode.Attributes["ftp_chunkcount"] != null) { FTPClient.MAX_CHUNK_COUNT = byte.Parse(clientNode.Attributes["ftp_chunkcount"].Value); } if (clientNode.Attributes["ftp_chunksize"] != null) { FTPClient.CHUNK_SIZE = int.Parse(clientNode.Attributes["ftp_chunksize"].Value); } if (clientNode.Attributes["thirdbtn"] != null) { ServerSetting.ShowHallThirdBtn = int.Parse(clientNode.Attributes["thirdbtn"].Value) != 0; } if (clientNode.Attributes["exchange"] != null) { ServerSetting.ShowExchange = int.Parse(clientNode.Attributes["exchange"].Value) != 0; } if (clientNode.Attributes["showjbp"] != null) { ServerSetting.ShowJBP = int.Parse(clientNode.Attributes["showjbp"].Value) != 0; } if (clientNode.Attributes["showmatch"] != null) { ServerSetting.ShowMatch = int.Parse(clientNode.Attributes["showmatch"].Value) != 0; } if (clientNode.Attributes["showthirdlogin"] != null) { ServerSetting.ShowThirdLoginBtn = int.Parse(clientNode.Attributes["showthirdlogin"].Value) != 0; } m_State = UpdateState.UPDATE_COMPLETE; //检查客户端版本号 if (clientVer == null) { return(false); } uint ver = Utility.VersionToUint(clientVer); if (ver > ServerSetting.ClientVer) { //clientUrl = ReplaceFTPAddress(clientUrl); //版本更新 if (RuntimeInfo.GetPlatform() == GamePlatformType.Android) { if (CheckClientFile(clientCrc)) { //文件已经下载完成。 //NativeInterface.DownNewClientVersion(GetClientPath()); } else { //下载资源文件 DownResData drd = new DownResData(); drd.ResType = ResType.MAX; drd.ResUrl = clientUrl; drd.ResCrc = clientCrc; drd.ResSize = clientSize; m_DownList.Add(drd); m_State = UpdateState.UPDATE_DOWNLOAD_RES; m_AllDownloadBytes += clientSize; } m_bNewClient = true; } else if (RuntimeInfo.GetPlatform() == GamePlatformType.Windows) { m_WinSizeList = new int[5]; for (int i = 0; i < 5; ++i) { string str = "winsize" + (i + 1).ToString(); if (clientNode.Attributes[str] != null) { m_WinSizeList[i] = int.Parse(clientNode.Attributes[str].Value); } } DownResData drd = new DownResData(); drd.ResType = ResType.MAX; drd.ResUrl = clientUrl; drd.ResCrc = clientCrc; drd.ResSize = clientSize; m_DownList.Add(drd); m_State = UpdateState.UPDATE_DOWNLOAD_RES; m_AllDownloadBytes += clientSize; m_bNewClient = true; } else { NativeInterface.DownNewClientVersion(clientUrl); m_State = UpdateState.UPDATE_NEW_CLIENT; return(true); } } for (int i = 0; i < (int)ResManager.RES_NUM; ++i) { ResType rt = (ResType)i; XmlNode node = ele.SelectSingleNode(rt.ToString()); DownResData vd = new DownResData(); vd.ResCrc = uint.Parse(node.Attributes["crc"].Value); vd.ResUnzipSize = uint.Parse(node.Attributes["unzipsize"].Value); if (ResManager.Instance.VersionMgr.CheckVerAndCrc(rt, vd.ResCrc, vd.ResUnzipSize)) { //下载资源文件 vd.ResType = rt; vd.ResUrl = resftp + node.Attributes["url"].Value; vd.ResSize = uint.Parse(node.Attributes["size"].Value); m_DownList.Add(vd); m_AllDownloadBytes += vd.ResSize; } } //保存当前的RES_VERSION if (m_DownList.Count > 0) { m_State = UpdateState.UPDATE_DOWNLOAD_RES; return(true); } else { return(true); } }
bool SaveRes(MultiFileOK dd) { //验证crc DownResData drd = dd.Drd; if (drd.ResSize != dd.Data.Length) { LogMgr.Log("Res:" + drd.ResType + " 大小不匹配,localSize:" + dd.Data.Length + ", serverSize:" + drd.ResSize); return(false); } uint crc = Crc.Crc32(dd.Data, 0, dd.Data.Length); if (crc != drd.ResCrc) { LogMgr.Log("Res:" + drd.ResType + "资源检验失败, size:" + dd.Data.Length + ", localCrc:" + crc + ", serverCrc:" + drd.ResCrc); return(false); } System.Random rr = new System.Random(); int name = rr.Next(100, 10000); string tempFile = RuntimeInfo.GetLocalPath(drd.ResType + name.ToString() + "_temp.dat"); string path = RuntimeInfo.GetResPersistentPath(drd.ResType); try { File.Delete(tempFile); } catch { } try { File.Delete(path); } catch { } try { FileStream fsTemp = File.Create(tempFile); if (fsTemp == null) { LogMgr.Log("RES文件创建失败:" + drd.ResType); return(false); } //解压 fsTemp.Write(dd.Data, 0, dd.Data.Length); fsTemp.Flush(); fsTemp.Close(); fsTemp = null; LZMA.DecompressFile(tempFile, path); } catch (System.Exception e) { ReportException.Instance.AddException("文件解压失败:" + e.ToString()); } try { File.Delete(tempFile); } catch { } if (File.Exists(path) == false) { LogMgr.Log("RES文件解压失败:" + drd.ResType); return(false); } ResManager.Instance.VersionMgr.SetResVersion(drd.ResType, drd.ResCrc, drd.ResUnzipSize); return(ResManager.Instance.VersionMgr.SaveVersion()); }