Пример #1
0
 /// <summary>
 /// 数据包写入器
 /// </summary>
 /// <param name="codeId">消息报的id</param>
 private PacketWriter(ushort codeId)
 {
     buffer = DogBuffer.GetFromPool32K();
     //  先预留2位用于存放消息id
     buffer.Length = ReceiveQueue.PacketLengthSize;
     Write(codeId);
 }
Пример #2
0
        /// <summary>
        /// 设置网络消息码
        /// </summary>
        /// <param name="codeId"></param>
        public void SetNetCode(ushort codeId)
        {
            if (buffer != null)
            {
                buffer.Release();
            }

            buffer = DogBuffer.GetFromPool32K();
            //  先预留2位用于存放消息id
            buffer.Length = ReceiveQueue.PacketLengthSize;
            Write(codeId);
        }
Пример #3
0
        public void TestSize()
        {
            var _4Kbuf = DogBuffer.GetFromPool4K();

            Assert.IsNotNull(_4Kbuf);
            Assert.AreEqual(_4Kbuf.Bytes.Length, 1024 * 4);
            Assert.AreEqual(_4Kbuf.Length, 0);

            var _32kbuf = DogBuffer.GetFromPool32K();

            Assert.IsNotNull(_32kbuf);
            Assert.AreEqual(_32kbuf.Bytes.Length, 1024 * 32);
            Assert.AreEqual(_32kbuf.Length, 0);
        }
Пример #4
0
        public void TestRelease32K()
        {
            var firstFree = Get32KbufferFree();

            var buf    = DogBuffer.GetFromPool32K();
            var 分配后的数量 = Get32KbufferFree();

            Assert.AreEqual(firstFree - 1, 分配后的数量);

            buf.Release();
            var 释放缓冲区后的数量 = Get32KbufferFree();

            Assert.AreEqual(firstFree, 释放缓冲区后的数量);

            //  测试重复释放会不会有问题
            buf.Release();
            释放缓冲区后的数量 = Get32KbufferFree();
            Assert.AreEqual(firstFree, 释放缓冲区后的数量);
        }
Пример #5
0
        public void BaseTest()
        {
            var manager = new PacketHandlersBase();
            var pr      = new PacketReaderModule();

            new RegisterNetMethod(manager).Register(new ILogicModule[] { pr });

            Assert.IsNotNull(manager.Handlers[1], "方法没注册到消息管理器里");

            manager.Handlers[1].OnReceive(new NetState(), new PacketReader(DogBuffer.GetFromPool32K()));
            Assert.IsTrue(pr.IsTouchOnReadTest);

            Assert.IsNotNull(manager.Handlers[2], "包读取方法没注册到消息管理器");
            manager.Handlers[2].OnReceive(new NetState(), new PacketReader(DogBuffer.GetFromPool32K()));
            Assert.IsTrue(pr.IsTouchPackageReader);

            Assert.IsNotNull(manager.Handlers[3], "简单读取方法没注册到消息管理器");
            manager.Handlers[3].OnReceive(new NetState(), new PacketReader(DogBuffer.GetFromPool32K()));
            Assert.IsTrue(pr.IsTouchSimpleMethod);
        }
Пример #6
0
        /// <summary>
        /// 收到Socket数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnSocketRecv(object sender, SocketRecvEventArgs<Session> e)
        {
            lock (sessions)
            {
                var session = e.Session.Data;
                DogBuffer buf;
                if (e.Buffer.Length > 1024*4)
                {
                    buf = DogBuffer.GetFromPool32K();
                }
                else
                {
                    buf = DogBuffer.GetFromPool4K();
                }

                Array.Copy(e.Buffer.Bytes, buf.Bytes, e.Buffer.Length);
                buf.Length = e.Buffer.Length;
                session.Client.SendPackage(buf);
                buf.Release();

                recvPackageCount++;
                recvPackageLength += e.Buffer.Length;
            }
        }
Пример #7
0
        /// <summary>
        /// 收到网络消息包
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSocketRecv(object sender, SocketRecvEventArgs <NetState> e)
        {
            var netState = e.Session.Data;

            netState.ReceiveBuffer.Enqueue(e.Buffer.Bytes, 0, e.Buffer.Length);
            if (netState.ReceiveBuffer.Length > MaxPackageSize)
            {
                //  缓冲区过多,一定发送了某种异常情况
                Logs.Error("client recv buff is full.");
                netState.NetSocket.CloseSocket();
                return;
            }

            while (netState.ReceiveBuffer.Length > 2)   // 大于包头长度才具备解析的需求
            {
                var len = netState.ReceiveBuffer.GetPacketLength();
                if (len == 0)
                {
                    Logs.Error("get package len is zero.");
                    netState.NetSocket.CloseSocket();
                    return;
                }

                if (len < 4)
                {
                    Logs.Error("get package len is min 4.");
                    netState.NetSocket.CloseSocket();
                    return;
                }

                if (len > MaxPackageSize)
                {
                    Logs.Error("get package len is error. size:{0}", len);
                    netState.NetSocket.CloseSocket();
                    return;
                }

                if (len <= netState.ReceiveBuffer.Length)
                {
                    DogBuffer readBuffer;
                    if (len < 1024 * 4)
                    {
                        readBuffer = DogBuffer.GetFromPool4K();
                    }
                    else
                    {
                        readBuffer = DogBuffer.GetFromPool32K();
                    }

                    var get = netState.ReceiveBuffer.Dequeue(readBuffer.Bytes, 0, len);
                    if (get == len)
                    {
                        readBuffer.Length = len;

                        var packageReader = PacketReader.AcquireContent(readBuffer);
                        var packageId     = packageReader.GetPacketID();
                        //Debug.Write("msgId = " + packageId.ToString());

                        var packetHandler = PacketHandlersManger.GetHandler(packageId);
                        if (packetHandler != null)
                        {
                            //  加入网络消息处理
                            if (_useManyTaskThread)
                            {
                                switch (packetHandler.TaskType)
                                {
                                case TaskType.Low:
                                    lowTask.AppendTask(netState, packetHandler, packageReader);
                                    break;

                                case TaskType.Assist:
                                    Logs.Debug("assist task.");
                                    assistTask.AppendTask(netState, packetHandler, packageReader);
                                    break;

                                default:
                                    mainTask.AppendTask(netState, packetHandler, packageReader);
                                    break;
                                }
                            }
                            else
                            {
                                mainTask.AppendTask(netState, packetHandler, packageReader);
                            }
                        }
                        else
                        {
                            Logs.Error("unknow packetid. code={0}", packageId);
                            netState.ErrorCount++;
                            if (netState.ErrorCount >= 10)
                            {
                                //  错误达到极大值,则关闭连接
                                Logs.Error("ip {0} error count max.", netState.GetIP());
                                netState.NetSocket.CloseSocket();
                            }
                        }
                    }
                    continue;
                }
                break;
            }
        }
Пример #8
0
        void NetSocket_SocketRecv(object sender, SocketRecvEventArgs <NetState> e)
        {
            var netState = e.Session.Data;

            netState.ReceiveBuffer.Enqueue(e.Buffer.Bytes, 0, e.Buffer.Length);

            if (netState.ReceiveBuffer.Length > MaxPackageSize)
            {
                //  缓冲区过多,一定发送了某种异常情况
                Logs.Error("client recv buff is full.");
                netState.NetSocket.CloseSocket();
                return;
            }

            while (netState.ReceiveBuffer.Length > 2)
            {
                var len = netState.ReceiveBuffer.GetPacketLength();
                if (len == 0)
                {
                    Logs.Error("get package len is zero.");
                    netState.NetSocket.CloseSocket();
                    return;
                }

                if (len < 4)
                {
                    Logs.Error("package is min 4.");
                    netState.NetSocket.CloseSocket();
                    return;
                }


                if (len > MaxPackageSize)
                {
                    Logs.Error("get package len is error. size:{0}", len);
                    netState.NetSocket.CloseSocket();
                    return;
                }

                IsWaitCheckOnline = false;

                if (len <= netState.ReceiveBuffer.Length)
                {
                    DogBuffer readBuffer;
                    if (len < 1024 * 4)
                    {
                        readBuffer = DogBuffer.GetFromPool4K();
                    }
                    else
                    {
                        readBuffer = DogBuffer.GetFromPool32K();
                    }

                    if (len >= readBuffer.Bytes.Length)
                    {
                        readBuffer.UpdateCapacity(len);
                    }

                    var get = netState.ReceiveBuffer.Dequeue(readBuffer.Bytes, 0, len);
                    if (get == len)
                    {
                        readBuffer.Length = len;

                        var    packageReader = PacketReader.AcquireContent(readBuffer);
                        ushort id            = packageReader.GetPacketID();
                        Logs.Debug("msgid= {0}", id);

                        var packetHandler = PacketHandlersManger.GetHandler(id);
                        if (packetHandler != null)
                        {
                            //  加入网络消息处理
                            TaskManager.AppendTask(netState, packetHandler, packageReader);
                        }
                        else
                        {
                            Logs.Error("unknow packetid. code={0}", id);
                        }
                    }

                    continue;
                }

                break;
            }
        }
Пример #9
0
        /// <summary>
        /// 收到网络消息包
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSocketRecv(object sender, SocketRecvEventArgs <NetState> e)
        {
            var netState = e.Session.Data;

            netState.ReceiveBuffer.Enqueue(e.Buffer.Bytes, 0, e.Buffer.Length);

            var len = netState.ReceiveBuffer.GetPacketLength();

            do
            {
                if (len <= netState.ReceiveBuffer.Length)
                {
                    DogBuffer readBuffer;
                    if (len < 1024 * 4)
                    {
                        readBuffer = DogBuffer.GetFromPool4K();
                    }
                    else
                    {
                        readBuffer = DogBuffer.GetFromPool32K();
                    }

                    var get = netState.ReceiveBuffer.Dequeue(readBuffer.Bytes, 0, len);
                    if (get == len)
                    {
                        readBuffer.Length = len;

                        var packageReader = new PacketReader(readBuffer);
                        var packageId     = packageReader.GetPacketID();
                        var packetHandler = PacketHandlersManger.GetHandler(packageId);
                        if (packetHandler != null)
                        {
                            //  加入网络消息处理
                            if (_useManyTaskThread)
                            {
                                switch (packetHandler.TaskType)
                                {
                                case TaskType.Low:
                                    lowTask.AppendTask(netState, packetHandler, packageReader);
                                    break;

                                case TaskType.Assist:
                                    Logs.Debug("assist task.");
                                    assistTask.AppendTask(netState, packetHandler, packageReader);
                                    break;

                                default:
                                    mainTask.AppendTask(netState, packetHandler, packageReader);
                                    break;
                                }
                            }
                            else
                            {
                                mainTask.AppendTask(netState, packetHandler, packageReader);
                            }
                        }
                        else
                        {
                            Logs.Error("unknow packetid. code={0}", packageId);
                        }
                    }
                }
                //  一次网络消息可能会对应多个消息包,因此这里用循环获得消息包
                len = netState.ReceiveBuffer.GetPacketLength();
            } while (len > 0);
        }