示例#1
0
文件: Kcp.cs 项目: pkwzsqsdly/HGUI
        /// <summary>
        /// 数据封包
        /// </summary>
        /// <param name="dat">数据</param>
        /// <param name="type">数据类型</param>
        /// <param name="msgId">消息id</param>
        /// <param name="time">发送时间</param>
        /// <returns></returns>
        public static byte[][] Pack(byte[] dat, byte type, UInt16 msgId, Int16 time)
        {
            int len = dat.Length;
            int p   = len / FragmentSize;
            int r   = len % FragmentSize;
            int all = p;//总计分卷数量

            if (r > 0)
            {
                all++;
            }
            byte[][] tmp = new byte[all][];
            int      s   = 0;

            unsafe
            {
                byte *buf = stackalloc byte[1490];
                for (int i = 0; i < p; i++)
                {
                    KcpHead *head = (KcpHead *)buf;
                    head->Type    = type;
                    head->MsgID   = msgId;
                    head->CurPart = (UInt16)i;
                    head->AllPart = (UInt16)all;
                    head->PartLen = (UInt16)FragmentSize;
                    head->Lenth   = (UInt32)len;
                    byte *dp = buf + KcpHead.Size;
                    for (int j = 0; j < FragmentSize; j++)
                    {
                        dp[j] = dat[s];
                        s++;
                    }
                    tmp[i] = PackInt(buf, FragmentSize + KcpHead.Size);
                }
                if (r > 0)
                {
                    KcpHead *head = (KcpHead *)buf;
                    head->Type    = type;
                    head->MsgID   = msgId;
                    head->CurPart = (UInt16)p;
                    head->AllPart = (UInt16)all;
                    head->PartLen = (UInt16)r;
                    head->Lenth   = (UInt32)len;
                    byte *dp = buf + KcpHead.Size;
                    for (int j = 0; j < r; j++)
                    {
                        dp[j] = dat[s];
                        s++;
                    }
                    tmp[p] = PackInt(buf, r + KcpHead.Size);
                }
            }
            return(tmp);
        }
示例#2
0
文件: Kcp.cs 项目: pkwzsqsdly/HGUI
        void PackAll(KcpData link, byte[] dat, byte type, UInt16 msgId, Int16 time)
        {
            int len = dat.Length;
            int p   = len / FragmentSize;
            int r   = len % FragmentSize;
            int all = p;//总计分卷数量

            if (r > 0)
            {
                all++;
            }
            int s = 0;

            unsafe
            {
                fixed(byte *bp = &tmpBuffer[0])
                {
                    for (int i = 0; i < p; i++)
                    {
                        KcpHead *head = (KcpHead *)bp;
                        head->Type    = type;
                        head->MsgID   = msgId;
                        head->CurPart = (UInt16)i;
                        head->AllPart = (UInt16)all;
                        head->PartLen = (UInt16)FragmentSize;
                        head->Lenth   = (UInt32)len;
                        head->Time    = time;
                        byte *dp = bp + KcpHead.Size;
                        for (int j = 0; j < FragmentSize; j++)
                        {
                            dp[j] = dat[s];
                            s++;
                        }
                        BlockInfo <byte> block = PackInt(FragmentSize + KcpHead.Size);
                        MsgInfo          msg   = new MsgInfo();
                        msg.data       = block;
                        msg.MsgID      = msgId;
                        msg.CurPart    = (UInt16)i;
                        msg.CreateTime = time;
                        SendMsg(link, ref msg, time);
                        link.Msgs.Add(msg);
                    }
                    if (r > 0)
                    {
                        KcpHead *head = (KcpHead *)bp;
                        head->Type    = type;
                        head->MsgID   = msgId;
                        head->CurPart = (UInt16)p;
                        head->AllPart = (UInt16)all;
                        head->PartLen = (UInt16)r;
                        head->Lenth   = (UInt32)len;
                        head->Time    = time;
                        byte *dp = bp + KcpHead.Size;
                        for (int j = 0; j < r; j++)
                        {
                            dp[j] = dat[s];
                            s++;
                        }
                        BlockInfo <byte> block = PackInt(r + KcpHead.Size);
                        MsgInfo          msg   = new MsgInfo();
                        msg.data       = block;
                        msg.MsgID      = msgId;
                        msg.CurPart    = (UInt16)p;
                        msg.CreateTime = time;
                        SendMsg(link, ref msg, time);
                        link.Msgs.Add(msg);
                    }
                }
            }
        }