123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ToolsClassLibrary.UDP
- {
- /// <summary>
- /// UDP数据包分割器
- /// </summary>
- public static class UdpPacketSplitter
- {
- public static ICollection<UdpPacket> Split(Msg message)
- {
- byte[] datagram = null;
- try
- {
- datagram = SerializationUnit.SerializeObject(message);
- }
- catch (Exception e)
- {
- //AddTalkMessage("数据转型异常");
- }
- //产生一个序列号,用来标识包数据属于哪一组
- Random Rd = new Random();
- long SequenceNumber = Rd.Next(88888, 999999);
- ICollection<UdpPacket> udpPackets = UdpPacketSplitter.Split(SequenceNumber, datagram, 10240, message.destinationIP, message.port);
- return udpPackets;
- }
- /// <summary>
- /// 分割UDP数据包
- /// </summary>
- /// <param name="sequence">UDP数据包所持有的序号</param>
- /// <param name="datagram">被分割的UDP数据包</param>
- /// <param name="chunkLength">分割块的长度</param>
- /// <returns>
- /// 分割后的UDP数据包列表
- /// </returns>
- public static ICollection<UdpPacket> Split(long sequence, byte[] datagram, int chunkLength, string desip, int port)
- {
- if (datagram == null)
- throw new ArgumentNullException("datagram");
- List<UdpPacket> packets = new List<UdpPacket>();
- int chunks = datagram.Length / chunkLength;
- int remainder = datagram.Length % chunkLength;
- int total = chunks;
- if (remainder > 0) total++;
- for (int i = 1; i <= chunks; i++)
- {
- byte[] chunk = new byte[chunkLength];
- Buffer.BlockCopy(datagram, (i - 1) * chunkLength, chunk, 0, chunkLength);
- packets.Add(new UdpPacket(sequence, total, i, chunk, chunkLength, remainder, desip, port));
- }
- if (remainder > 0)
- {
- int length = datagram.Length - (chunkLength * chunks);
- byte[] chunk = new byte[length];
- Buffer.BlockCopy(datagram, chunkLength * chunks, chunk, 0, length);
- packets.Add(new UdpPacket(sequence, total, total, chunk, chunkLength, remainder, desip, port));
- }
- return packets;
- }
- }
- }
|