Example #1
0
 public KcpServer(int port = 0) : base(port)
 {
     Instance = this;
     kcp      = new Kcp();
 }
Example #2
0
        /// <summary>
        /// 添加一个接收到的消息分卷
        /// </summary>
        /// <param name="dat">数据缓存</param>
        /// <param name="len">数据长度</param>
        /// <param name="kcp">封包器</param>
        /// <param name="time">数据接收时间,单位毫秒</param>
        public void AddMsgPart(byte[] dat, int len, Kcp kcp, Int16 time)
        {
            byte type = dat[0];

            if (type == EnvelopeType.Success)
            {
                MsgReturn head;
                unsafe
                {
                    fixed(byte *bp = &dat[0])
                    head = *(MsgReturn *)bp;
                }
                Success(ref head, time);
            }
            else
            {
                MsgHead head;
                unsafe
                {
                    fixed(byte *bp = &dat[0])
                    head = *(MsgHead *)bp;
                }
                int nul  = 0;
                int fill = 0;
                int id   = head.MsgID;
                for (int i = 0; i < 128; i++)
                {
                    int cid = caches[i].head.MsgID;
                    if (cid > 0)
                    {
                        if (cid == id)
                        {
                            FillMsg(ref head, dat, i);
                            nul = i;
                            goto label;
                        }
                        fill++;
                    }
                    else
                    if (nul == 0)
                    {
                        nul = i;
                    }
                    if (fill >= max)
                    {
                        break;
                    }
                }
                caches[nul].buff           = kcp.MemoryRequest((int)head.Lenth);
                caches[nul].buff.DataCount = (int)head.Lenth;
                if (head.AllPart > 1)
                {
                    int c = head.AllPart / 32;
                    if (head.AllPart % 32 > 0)
                    {
                        c++;
                    }
                    caches[nul].states = kcp.statesBuffer.RegNew(c);
                    caches[nul].states.Zero();
                }
                caches[nul].time = time;
                caches[nul].head = head;
                caches[nul].part = head.AllPart;
                AddNew(ref head, dat, nul);
                max++;
                label :;
                if (caches[nul].rcvLen >= caches[nul].part)
                {
                    caches[nul].head.MsgID = 0;
                    caches[nul].states.Release();
                    BlockData data = new BlockData();
                    data.type = head.Type;
                    data.dat  = caches[nul].buff;
                    recvQueue.Enqueue(data);
                    max--;
                }
                kcp.Success(ref head, this);
            }
        }