Exemplo n.º 1
0
 /// <summary>
 /// 写入封包头
 /// </summary>
 /// <param name="buff">数据缓存</param>
 /// <param name="index">写入位置</param>
 /// <param name="head">封包头</param>
 public static unsafe void WriteHead(byte[] buff, int index, EnvelopeHead head)
 {
     fixed(byte *b = &buff[index])
     {
         *(EnvelopeHead *)b = *&head;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buff">源数据</param>
        /// <param name="len">数据长度</param>
        /// <param name="buffer">缓存数据</param>
        /// <param name="remain"></param>
        /// <param name="fs"></param>
        /// <returns></returns>
        public static List <EnvelopePart> UnpackPart(byte[] buff, int len, byte[] buffer, ref int remain, int fs)
        {
            fs -= EnvelopeHeadSize;
            List <EnvelopePart> list = new List <EnvelopePart>();
            int s = remain;

            for (int i = 0; i < len; i++)
            {
                buffer[s] = buff[i];
                s++;
            }
            len += remain;
            int index = 0;

            for (int i = 0; i < 1024; i++)
            {
                EnvelopeHead head = ReadHead(buffer, index);
                if (head.PartLen > fs)
                {
                    remain = 0;
                    break;
                }
                if (index + EnvelopeHeadSize + head.PartLen > len)
                {
                    remain = len - index;
                    for (int j = 0; j < len; j++)
                    {
                        buffer[j] = buffer[index];
                        index++;
                    }
                    return(list);
                }
                index += EnvelopeHeadSize;
                if (head.Lenth > 2)
                {
                    EnvelopePart part = new EnvelopePart();
                    part.head = head;
                    int l   = (int)head.PartLen;
                    var buf = new byte[l];
                    int a   = index;
                    for (int j = 0; j < l; j++)
                    {
                        buf[j] = buffer[a]; a++;
                    }
                    part.data = buf;
                    list.Add(part);
                }
                index += head.PartLen;
                if (index >= len)
                {
                    remain = 0;
                    break;
                }
            }
            return(list);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 对数据进行分卷, 标头4字节,总长度4字节, 当前分卷2字节,总分卷2字节,当前分卷长度4字节,总计16字节
        /// </summary>
        /// <param name="buff">需要打包的数据</param>
        /// <param name="type">数据类型</param>
        /// <param name="id">数据包标志</param>
        /// <param name="fs">每个分卷大小</param>
        /// <returns></returns>
        public static byte[][] SubVolume(byte[] buff, byte type, UInt16 id, UInt16 fs)
        {
            fs -= 16;
            if (buff.Length < 2)
            {
                buff = new byte[2];
            }
            int    len     = buff.Length;
            int    part    = len / fs;
            int    r       = len % fs;
            UInt16 allPart = (UInt16)part;

            if (r > 0)
            {
                allPart++;
            }
            byte[][]     buf   = new byte[allPart][];
            UInt16       msgId = id;
            EnvelopeHead head  = new EnvelopeHead();

            for (int i = 0; i < part; i++)
            {
                head.MsgID   = msgId;
                head.Type    = type;
                head.PartID  = id;
                head.CurPart = (UInt16)i;
                head.AllPart = allPart;
                head.PartLen = fs;
                head.Lenth   = (UInt32)len;
                byte[] tmp = new byte[fs + 16];
                WriteHead(tmp, 0, head);
                buf[i] = EnvelopePart(buff, tmp, i, fs, fs);
                id++;
            }
            if (r > 0)
            {
                head.MsgID   = msgId;
                head.Type    = type;
                head.PartID  = id;
                head.CurPart = (UInt16)part;
                head.AllPart = allPart;
                head.PartLen = (UInt16)r;
                head.Lenth   = (UInt32)len;
                byte[] tmp = new byte[r + 16];
                WriteHead(tmp, 0, head);
                buf[part] = EnvelopePart(buff, tmp, part, r, fs);
            }
            return(buf);
        }
Exemplo n.º 4
0
        public static void CopyToBuff(byte[] buff, byte[] src, int start, EnvelopeHead head, int FragmentSize)
        {
            int index = head.CurPart * FragmentSize;
            int len   = (int)head.PartLen;
            int all   = buff.Length;

            for (int i = 0; i < len; i++)
            {
                if (index >= all)
                {
                    break;
                }
                buff[index] = src[start];
                index++;
                start++;
            }
        }
Exemplo n.º 5
0
        static byte[] ReadPart(byte[] data, out EnvelopeHead head)
        {
            head = ReadHead(data, 0);
            int len = data.Length - EnvelopeHeadSize;

            if (len >= head.PartLen)
            {
                byte[] buf   = new byte[head.PartLen];
                int    start = EnvelopeHeadSize;
                for (int i = 0; i < head.PartLen; i++)
                {
                    buf[i] = data[start];
                    start++;
                }
                return(buf);
            }
            return(null);
        }