Beispiel #1
0
        /// <summary>
        /// 输出发送信息包的日志
        /// </summary>
        private static void AuthSendPacket(object sender, NetStateSendPacketEventArgs eventArgs)
        {
            PacketBuffer sendBuffer = eventArgs.SendPacket.AcquireBuffer();

            //////////////////////////////////////////////////////////////////////////
            // 输出信息包的日志
            //////////////////////////////////////////////////////////////////////////
            ProcessNet.LogClientPack(sendBuffer.Buffer, sendBuffer.Length, "Auth_Out_Packets.log", eventArgs.SendNetState.ToString(), eventArgs.SendPacket.PacketID, AuthOpCodeName.GetAuthOpCodeName(eventArgs.SendPacket.PacketID));
            //////////////////////////////////////////////////////////////////////////
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="netState"></param>
        internal static void AuthProcessReceive(object sender, NetStateEventArgs eventArgs)
        {
            LOGs.WriteLine(LogMessageType.MSG_HACK, "Auth_ProcessReceive......= {0}", eventArgs.NetState.ReceiveBuffer.Length);

            if (eventArgs.NetState == null)
            {
                Debug.WriteLine("ProcessNet.Auth_ProcessReceive(...) - eventArgs.NetState == null error!");
                return;
            }

            ReceiveQueue receiveQueueBuffer = eventArgs.NetState.ReceiveBuffer;

            if (receiveQueueBuffer == null)
            {
                Debug.WriteLine("ProcessNet.Auth_ProcessReceive(...) - receiveQueueBuffer == null error!");
                return;
            }

            if (receiveQueueBuffer.Length < AUTH_ID_SIZE)
            {
                                                            // 等待数据包的完整
                return;
            }

            long iReceiveBufferLength = receiveQueueBuffer.Length; // 隔段时间就会有数据过来,所以可以不用锁定的,锁定了也没用,很难保证多线程中处理了所有的数据

            while (iReceiveBufferLength >= AUTH_ID_SIZE)
            {
                // 获取包的 ID
                long iPacketID = receiveQueueBuffer.GetPacketID();

                // 获取包的长度(数据包里面没有带数据包大小的字段)
                long iPacketLength = receiveQueueBuffer.Length;

                if (iPacketLength > BUFFER_SIZE)   // 数据包过大
                {
                    Debug.WriteLine("ProcessNet.Auth_ProcessReceive(...) - iPacketLength > BUFFER_SIZE error!");

                    eventArgs.NetState.Dispose(); // 断开
                    return;
                }

                // 获取空数据
                byte[] packetBuffer = s_ProcessorBuffers.AcquireBuffer();

                // 获取数据内容
                long iReturnPacketSize = receiveQueueBuffer.Dequeue(ref packetBuffer, 0, iPacketLength);

                // 获取的数据不相同
                if (iReturnPacketSize != iPacketLength)
                {
                    Debug.WriteLine("ProcessNet.Auth_ProcessReceive(...) - iReturnPacketSize != iPacketLength error!");

                    // 返回内存池
                    s_ProcessorBuffers.ReleaseBuffer(packetBuffer);

                    eventArgs.NetState.Dispose(); // 断开
                    return;
                }


                //////////////////////////////////////////////////////////////////////////
                // 输出信息包的日志
                //////////////////////////////////////////////////////////////////////////
                ProcessNet.LogServerPack(packetBuffer, iPacketLength, "Auth_In_Packets.log", eventArgs.NetState.ToString(), iPacketID, AuthOpCodeName.GetAuthOpCodeName(iPacketID));
                //////////////////////////////////////////////////////////////////////////


                // 读取的数据包
                PacketReader packetReader = new PacketReader(packetBuffer, iPacketLength, 0);   // 不跳过数据包头

                // 获取处理数据包的实例
                PacketHandler packetHandler = ProcessServer.AuthPacketHandlers.GetHandler(iPacketID);
                if (packetHandler == null)   // 说明还没有解开当前的数据包内容
                {
                    //////////////////////////////////////////////////////////////////////////
                    // 输出信息包的日志
                    //////////////////////////////////////////////////////////////////////////
                    ProcessNet.LogServerPack(packetBuffer, iPacketLength, "Auth_Unknown_Packets.log", eventArgs.NetState.ToString(), iPacketID, AuthOpCodeName.GetAuthOpCodeName(iPacketID));
                    //////////////////////////////////////////////////////////////////////////

                    // 返回内存池
                    s_ProcessorBuffers.ReleaseBuffer(packetBuffer);

                    // 获取剩下的数据长度
                    iReceiveBufferLength = receiveQueueBuffer.Length;

                    continue;
                }

                // 当前需处理的数据包的大小
                long iPacketHandlerLength = packetHandler.Length;
                if (iPacketHandlerLength > iReturnPacketSize)   // 包需求的数据大小大于得到的数据大小
                {
                    // 返回内存池
                    s_ProcessorBuffers.ReleaseBuffer(packetBuffer);

                    eventArgs.NetState.Dispose(); // 断开
                    return;
                }

                // 处理数据
                packetHandler.OnReceive(eventArgs.NetState, packetReader);

                // 返回内存池
                s_ProcessorBuffers.ReleaseBuffer(packetBuffer);

                // 获取剩下的数据长度
                iReceiveBufferLength = receiveQueueBuffer.Length;
            }
        }