Esempio n. 1
0
        /// <summary>
        /// 发送用户消息
        /// </summary>
        /// <param name="link">用户连接</param>
        /// <param name="time">当前时间,用于统计时延</param>
        /// <returns></returns>
        public int SendMsg(KcpData link, Int16 time)
        {
            var q = link.sendQueue;
            int c = q.Count;

            for (int i = 0; i < c; i++)
            {
                var dat = q.Dequeue();
                link.MsgId++;
                if (link.MsgId >= MaxID)
                {
                    link.MsgId = MinID;
                }
                PackAll(link, dat.dat, dat.type, link.MsgId, time);
            }
            var msg2 = link.Msgs2;

            c = msg2.Count;
            for (int i = 0; i < c; i++)
            {
                kcpListener.soc.SendTo(msg2[i].data, SocketFlags.None, link.endpPoint);//发送广播消息
            }
            ReSendTimeOut(link, time);
            return(c);
        }
Esempio n. 2
0
        void SendMsg(KcpData link, ref MsgInfo msg, Int16 time)
        {
            int c = msg.data.DataCount;

            unsafe
            {
                byte *bp = msg.data.Addr;
                for (int i = 0; i < c; i++)
                {
                    tmpBuffer[i] = bp[i];
                }
            }
            msg.SendTime = time;
            msg.SendCount++;
            kcpListener.soc.SendTo(tmpBuffer, c, SocketFlags.None, link.endpPoint);
        }
Esempio n. 3
0
 void ReSendTimeOut(KcpData link, Int16 time)
 {
     for (int i = 0; i < link.Msgs.Count; i++)
     {
         int os = time - link.Msgs[i].SendTime;
         if (os < 0)
         {
             os += 10000;
         }
         if (os >= MsgTimeOut)
         {
             var msg = link.Msgs[i];
             SendMsg(link, ref msg, time);
             link.Msgs[i] = msg;
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 成功发送的消息回执,剔除掉缓存中的消息备份
        /// </summary>
        /// <param name="head">消息头</param>
        /// <param name="link">用户连接</param>
        public void Success(ref KcpHead head, KcpData link)
        {
            unsafe
            {
                fixed(byte *bp = &tmp2[0])
                {
                    KcpReturn *hp = (KcpReturn *)bp;

                    hp->Type    = EnvelopeType.Success;
                    hp->MsgID   = head.MsgID;
                    hp->CurPart = head.CurPart;
                    hp->Time    = head.Time;
                }
            }
            int c = PackInt(tmp2, KcpReturn.Size, tmpBuffer);

            kcpListener.soc.SendTo(tmpBuffer, c, SocketFlags.None, link.endpPoint);
        }
Esempio n. 5
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="buf">缓存</param>
        /// <param name="len">数据长度</param>
        /// <param name="link">用户连接</param>
        public void ReciveMsg(byte[] buf, int len, KcpData link)
        {
            var tmp = recvBuffer.RegNew(len);

            tmp.DataCount = len;
            unsafe
            {
                byte *ptr = tmp.Addr;
                for (int i = 0; i < len; i++)
                {
                    ptr[i] = buf[i];
                }
            }
            SocMsg msg = new SocMsg();

            msg.link = link;
            msg.dat  = tmp;
            queue.Enqueue(msg);
        }
Esempio n. 6
0
        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);
                    }
                }
            }
        }
Esempio n. 7
0
 /// <summary>
 /// 释放指针数据
 /// </summary>
 public void Dispose()
 {
     dat.Release();
     link = null;
 }