Exemplo n.º 1
0
        /// <summary>
        /// 拆包
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="identity"></param>
        /// <param name="fileModel"></param>
        /// <returns></returns>
        public List <byte[]> PackageSlice(byte[] buffer, ushort identity, UdpFileModel fileModel)
        {
            List <byte[]> result = new List <byte[]>();
            ushort        size   = (ushort)(buffer.Length / Count);

            if (buffer.Length % Count > 0)
            {
                size++;
            }

            for (ushort i = 0; i < size; i++)
            {
                var tmp          = buffer.Length - i * Count;
                var slicePackage = new UdpPackage()
                {
                    PackageIdentityNum = identity,
                    PackageSerialNum   = i,
                    PackageFileMode    = fileModel,
                    PackageCount       = size,
                    PackageBuffer      = buffer.Skip(i * Count).Take(tmp < Count ? tmp : Count).ToArray()
                };

                result.Add(UdpPackageToBytes(slicePackage));
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Udp数据包转byte数组
        /// </summary>
        /// <param name="package">udp数据包</param>
        /// <returns></returns>
        public static byte[] UdpPackageToBytes(UdpPackage package)
        {
            byte[] buffer = new byte[package.PackageBuffer.Length + 8];
            BitConverter.GetBytes(package.PackageIdentityNum).CopyTo(buffer, 0);
            BitConverter.GetBytes(package.PackageSerialNum).CopyTo(buffer, 2);
            BitConverter.GetBytes((ushort)package.PackageFileMode).CopyTo(buffer, 4);
            BitConverter.GetBytes(package.PackageCount).CopyTo(buffer, 6);
            package.PackageBuffer.CopyTo(buffer, 8);


            return(buffer);
        }