static Dictionary <string, byte[][]> dataTemp = new Dictionary <string, byte[][]>(); //存储临时数据包数据,用于重发 /// <summary> /// 获取分片udp数据集合 /// </summary> /// <param name="source">原数据包</param> /// <param name="blockSize">块大小</param> /// <returns></returns> public static List <byte[]> DataFragme(byte[] source, int blockSize = 50000) { List <byte[]> list = new List <byte[]>(); string key = DateTime.Now.ToString("yyyyMMddhhmmssff");//单任务唯一标识 byte[] keyBuff = Encoding.UTF8.GetBytes(key); int bls = source.Length / blockSize; //计算包个数 if (source.Length % blockSize != 0) { bls++; } byte[] blocks = BitConverter.GetBytes(bls); byte[] id = BitConverter.GetBytes(0); //报头序列约定:单任务标识,id,包分组数 byte[] headBuff = new byte[24]; keyBuff.CopyTo(headBuff, 0); id.CopyTo(headBuff, 16); blocks.CopyTo(headBuff, 20); list.Add(headBuff); if (source.Length < blockSize) { id = BitConverter.GetBytes(1);//该包编号 byte[] len = BitConverter.GetBytes(source.Length + 1); byte[] overBuff = new byte[source.Length + 25]; keyBuff.CopyTo(overBuff, 0); id.CopyTo(overBuff, keyBuff.Length); len.CopyTo(overBuff, keyBuff.Length + id.Length); source.CopyTo(overBuff, 24); byte crc = CRC8.CRC(source); overBuff[overBuff.Length - 1] = crc; list.Add(overBuff); return(list); } //子报头序列约定:单任务标识,id,子包有效数据长度 int block = 0; for (int i = 1; block != source.Length; i++) { byte[] bys = source.Take(block + blockSize).Skip(block).ToArray(); //当次分块数据 block += bys.Length; id = BitConverter.GetBytes(i); //该包编号 byte[] len = BitConverter.GetBytes(bys.Length + 1); //包长度 byte[] overBuff = new byte[bys.Length + 25]; //CRC校验位附加 byte crc = CRC8.CRC(bys); byte[] temp = new byte[bys.Length + 1]; bys.CopyTo(temp, 0); temp[temp.Length - 1] = crc; bys = temp; keyBuff.CopyTo(overBuff, 0); id.CopyTo(overBuff, keyBuff.Length); len.CopyTo(overBuff, keyBuff.Length + id.Length); bys.CopyTo(overBuff, 24); list.Add(overBuff); } if (!dataTemp.ContainsKey(key)) { dataTemp.Add(key, list.ToArray()); } StartWaitRequest(key); return(list); }