/// <summary> /// 接收端根据文件缺失信息发送重传请求子程序(第一次传输完进行的检查) /// </summary> /// <param name="client"></param> /// <param name="info"></param> private void SendRetranInfo(NetworkStream stream, FileCheckInfo info, string tempInfoSavepath) { byte[] bytes = new byte[2]; stream.Read(bytes, 0, 2); Console.WriteLine("正在发送重传信息"); //如果返回确定 if (BitConverter.ToInt16(bytes, 0) == 9) { List <byte> sendList = new List <byte>(); List <int> lackPieces = info.lackPieces; sendList.AddRange(BitConverter.GetBytes(info.PackId)); sendList.AddRange(BitConverter.GetBytes(info.Count)); foreach (var item in lackPieces) { sendList.AddRange(BitConverter.GetBytes(item)); } int position = 0; byte[] infoBytes = sendList.ToArray(); //创建临时信息文件(包含文件缺失片段信息) FileStream fs = File.Create(tempInfoSavepath); fs.Write(infoBytes, 0, infoBytes.Length); fs.Close(); /*while (position < infoBytes.Length) * { * if (position + 1024 < infoBytes.Length) * { * stream.Write(infoBytes, position, 1024); * position += 1024; * } * else * { * stream.Write(infoBytes, position, infoBytes.Length - position); * position = infoBytes.Length; * } * }*/ SendRetranInfo(new IPEndPoint(remoteAddress, 8070), tempInfoSavepath); stream.Write(InfoToBytes(Info.retranEnd), 0, 2); Console.WriteLine("retran end"); } else { Console.WriteLine("重传信息发送请求失败"); } }
/// <summary> /// 接收端根据文件缺失信息发送重传请求主程序(第一次传输完进行的检查) /// </summary> /// <param name="stream"></param> /// <param name="tempFilePath"></param> /// <param name="tempLostInfoSavepath"></param> private void SendRetranInfo(NetworkStream stream, string tempFilePath, string tempLostInfoSavepath) { FileCheckInfo checkInfo = packetUtil.FileCheck(tempFilePath); if (checkInfo.lackPieces.Count == 0) { stream.Write(InfoToBytes(Info.complete), 0, 2); Console.WriteLine("finshed"); } else { SendRetranInfo(stream, checkInfo, tempLostInfoSavepath); Console.WriteLine("retran"); } }
/// <summary> /// 接收端根据文件缺失信息发送重传请求子程序(第一次传输完进行的检查) /// </summary> /// <param name="client"></param> /// <param name="info"></param> private void SendRetranInfo(NetworkStream stream, FileCheckInfo info, string tempInfoSavepath) { List <byte> sendList = new List <byte>(); List <int> lackPieces = info.lackPieces; sendList.AddRange(BitConverter.GetBytes(info.PackId)); sendList.AddRange(BitConverter.GetBytes(info.Count)); foreach (var item in lackPieces) { sendList.AddRange(BitConverter.GetBytes(item)); } int position = 0; byte[] infoBytes = sendList.ToArray(); //创建临时信息文件(包含文件缺失片段信息) /* * FileStream fs = File.Create(tempInfoSavepath); * fs.Write(infoBytes, 0, infoBytes.Length); * fs.Close();*/ while (position < infoBytes.Length) { if (position + 1024 < infoBytes.Length) { stream.Write(infoBytes, position, 1024); position += 1024; } else { stream.Write(infoBytes, position, infoBytes.Length - position); position = infoBytes.Length; } } stream.Write(InfoToBytes(Info.retranEnd), 0, 2); }
private List <FileUpdateVo> PrepareDownRes(VersionConfig serverVersionConfig, FileCheckResult checkResult, byte[] fileListBytes, FileUpdateVo versionVo) { string url = versionVo.Url; string random = versionVo.Random; FileUpdateVo fileUpdateVo; string persistentMd5 = null; foreach (FileCheckInfo item in checkResult.DeleteList) { fileUpdateVo = new FileUpdateVo(item.name, url, random, item.md5, item.size); if (File.Exists(fileUpdateVo.PersistentPath)) { File.Delete(fileUpdateVo.PersistentPath); } } List <FileUpdateVo> fileVoList = new List <FileUpdateVo>(); for (int i = 0; i < checkResult.DownList.Count; i++) { FileCheckInfo item = checkResult.DownList[i]; fileUpdateVo = new FileUpdateVo(item.name, url, random, item.md5, item.size); if (File.Exists(fileUpdateVo.PersistentPath)) { persistentMd5 = Util.Md5file(fileUpdateVo.PersistentPath); } else { persistentMd5 = string.Empty; } if (persistentMd5 != fileUpdateVo.Md5)//需要下载 { fileVoList.Add(fileUpdateVo); } } return(fileVoList); }