Esempio n. 1
0
 public NetworkClient()
 {
     _decompression                 = new HuffmanDecompression();
     _isDecompressionEnabled        = false;
     _incompletePacket              = new PacketChunk(_bufferPool.AcquireBuffer());
     _incompleteDecompressionPacket = new PacketChunk(_bufferPool.AcquireBuffer());
     _typedHandlers                 = new List <PacketHandler> [0x100];
     _extendedTypedHandlers         = new List <PacketHandler> [0x100][];
     for (var i = 0; i < _typedHandlers.Length; i++)
     {
         _typedHandlers[i] = new List <PacketHandler>();
     }
 }
Esempio n. 2
0
        public NetworkClient()
        {
            m_Decompression          = new HuffmanDecompression();
            m_IsDecompressionEnabled = false;

            m_IncompletePacket = new PacketChunk(m_BufferPool.AcquireBuffer());
            m_IncompleteDecompressionPacket = new PacketChunk(m_BufferPool.AcquireBuffer());

            m_TypedHandlers         = new List <PacketHandler> [0x100];
            m_ExtendedTypedHandlers = new List <PacketHandler> [0x100][];

            for (int i = 0; i < m_TypedHandlers.Length; i++)
            {
                m_TypedHandlers[i] = new List <PacketHandler>();
            }
        }
Esempio n. 3
0
        public void AcquireBufferTest()
        {
            string     strBufferName    = string.Empty;                                                 // TODO: 初始化为适当的值
            long       iInitialCapacity = 0;                                                            // TODO: 初始化为适当的值
            long       iBufferSize      = 0;                                                            // TODO: 初始化为适当的值
            BufferPool target           = new BufferPool(strBufferName, iInitialCapacity, iBufferSize); // TODO: 初始化为适当的值

            byte[] expected = null;                                                                     // TODO: 初始化为适当的值
            byte[] actual;
            actual = target.AcquireBuffer();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Esempio n. 4
0
        public void Enqueue(byte[] buffer, int offset, int size)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            else if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            else if (size < 0)
            {
                throw new ArgumentOutOfRangeException("size");
            }
            else if ((buffer.Length - offset) < size)
            {
                throw new ArgumentException();
            }

            position += size;

            while (size > 0)
            {
                if (buffered.buffer == null)
                {
                    // nothing yet buffered
                    buffered.buffer = bufferPool.AcquireBuffer();
                }

                var page      = buffered.buffer;                       // buffer page
                int pageSpace = page.Length - buffered.length;         // available bytes in page
                int byteCount = (size > pageSpace ? pageSpace : size); // how many bytes we can copy over

                Buffer.BlockCopy(buffer, offset, page, buffered.length, byteCount);

                buffered.length += byteCount;
                offset          += byteCount;
                size            -= byteCount;

                if (buffered.length == page.Length)
                {
                    // page full
                    Append(buffered);

                    buffered.buffer = null;
                    buffered.length = 0;
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="newNetState"></param>
        internal static void NetState_InitializeNetState(NetState newNetState)
        {
            if (newNetState != null && newNetState.EncoderSeed == null && newNetState.ExtendData == null)
            {
                // 初始化客户端加密的数据种子
                newNetState.EncoderSeed = m_CryptTableBuffers.AcquireBuffer();
                Buffer.BlockCopy(ROSECrypt.Instance().CryptTableBuffer, 0, newNetState.EncoderSeed, 0, ROSECrypt.Instance().CryptTableBuffer.Length);

                LoginServerExtendData l_ExtendData = new LoginServerExtendData();
                newNetState.ExtendData = l_ExtendData;
            }
            else
            {
                Debug.WriteLine("ProcessNet.NetState_InitializeNetState(...) - newNetState != null && newNetState.Seed == null && newNetState.ExtendData == null error!");
            }
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void ReceiveFromZoneClusterServer(object sender, ProcessMessageBlockAtClientEventArgs eventArgs)
        {
            if (m_NetState == null)
            {
                Debug.WriteLine("ZoneExtendData.SocketClient_ProcessReceive(...) - m_NetState == null error!");
                return;
            }

            // 获取空数据
            byte[] l_PacketBuffer = s_ProcessorBuffers.AcquireBuffer();
            {
                Marshal.Copy(eventArgs.MessageBlock.ReadPointer(), l_PacketBuffer, 0, eventArgs.MessageBlock.Length);

                m_NetState.Send(new BufferPacket(l_PacketBuffer, 0, eventArgs.MessageBlock.Length));
            }
            s_ProcessorBuffers.ReleaseBuffer(l_PacketBuffer);   // 返回内存池
        }
Esempio n. 7
0
        private void ConnectionRequestCallback(IAsyncResult ar)
        {
            if (ar.IsCompleted == false)
            {
                return;
            }
            Connection.EndConnect(ar);

            if (Connection.Connected == false)
            {
                Connection = null;
                return;
            }

            mReceiveBuffer = mBuffers.AcquireBuffer();
            Connection.Client.BeginReceive(mReceiveBuffer, 0, mReceiveBuffer.Length, SocketFlags.None, OnReceiveCallback, Connection);
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="newNetState"></param>
        internal static void NetState_InitializeNetState(NetState newNetState)
        {
            if (newNetState != null && newNetState.EncoderSeed == null && newNetState.ExtendData == null)
            {
                CharServerExtendData l_ExtendData = new CharServerExtendData();

                // 初始化客户端加密的数据种子
                newNetState.EncoderSeed = m_CryptTableBuffers.AcquireBuffer();
                Buffer.BlockCopy(ROSECrypt.Instance().CryptTableBuffer, 0, newNetState.EncoderSeed, 0, ROSECrypt.Instance().CryptTableBuffer.Length);

                newNetState.ExtendData       = l_ExtendData;
                newNetState.EventDisconnect += new DisconnectNetStateEventHandler(CharPacketHandlers.DisconnectNetState);
            }
            else
            {
                Debug.WriteLine("ProcessNet.NetState_InitializeNetState(...) - newNetState.Seed != null error!");
            }
        }
Esempio n. 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="newNetState"></param>
        public static void NetState_InitializeNetState(NetState newNetState)
        {
            if (newNetState.EncoderSeed == null && newNetState.ExtendData == null)
            {
                // 初始化客户端加密的数据种子
                newNetState.EncoderSeed = m_CryptTableBuffers.AcquireBuffer();
                Buffer.BlockCopy(ROSECrypt.Instance().CryptTableBuffer, 0, newNetState.EncoderSeed, 0, ROSECrypt.Instance().CryptTableBuffer.Length);

                ROSEMobile l_ROSEMobile = new ROSEMobile();
                newNetState.Mobile = l_ROSEMobile;

                l_ROSEMobile.Player   = true;
                l_ROSEMobile.NetState = newNetState;

                WorldServerExtendData l_ExtendData = new WorldServerExtendData();
                l_ExtendData.ROSEMobile = l_ROSEMobile;

                newNetState.ExtendData = l_ExtendData;
            }
            else
            {
                Debug.WriteLine("ProcessNet.MessagePump_ProcessReceive(...) - newNetState.Seed != null error!");
            }
        }
Esempio n. 10
0
        public bool HandleReceive(NetState ns)
        {
            ByteQueue buffer = ns.Buffer;

            if (buffer == null || buffer.Length <= 0)
            {
                return(true);
            }

            CConsole.DebugLine("{0}: Incoming Data - {1} Bytes", ns, buffer.Length);

            /*
             * Packet Analyse/verify && Parsing
             */

            lock ( buffer ) {
                int length = buffer.Length;
                // debug Data
                while (length > 0 && ns.Running)
                {
                    short packetID = buffer.GetPacketID();

                    // debug log
                    using (TextWriter writer = File.CreateText(AppDomain.CurrentDomain.BaseDirectory + @"\packet_" + DateTime.Now.UnixTimestamp() + ".log")) {
                        using (MemoryStream ms = new MemoryStream(buffer.ByteBuffer))
                            Tools.FormatBuffer(writer, ms, (int)ms.Length);
                    }

                    CConsole.DebugLine("{0}: packetID {1}, {2} bytes", ns, packetID, length);

                    PacketHandler handler = PacketHandlers.GetHandler(packetID);
                    if (handler == null)
                    {
                        byte[] data = new byte[length];
                        length = buffer.Dequeue(data, 0, length);

                        CConsole.ErrorLine("{0}: no Handler found! Data dispose", ns);
                        break;
                    }

                    CConsole.StatusLine("{0}: Handler found ({1} bytes)! Trigger Callback...", ns, handler.Length);

                    byte[] packetBuffer;
                    if (mBufferSize >= handler.Length)
                    {
                        packetBuffer = mBuffers.AcquireBuffer();
                    }
                    else
                    {
                        packetBuffer = new byte[handler.Length];
                    }

                    buffer.Dequeue(packetBuffer, 0, handler.Length);

                    PacketReader r = new PacketReader(packetBuffer, handler.Length, 0);
                    handler.OnReceive(ns, r);
                    length = buffer.Length;

                    if (mBufferSize >= handler.Length)
                    {
                        mBuffers.ReleaseBuffer(packetBuffer);
                    }
                }         // end while()*/
            }             // end Lock()

            return(true);
        }
Esempio n. 11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="netState"></param>
        internal static void WorldProcessReceive(object sender, NetStateEventArgs eventArgs)
        {
            LOGs.WriteLine(LogMessageType.MSG_HACK, "World_ProcessReceive...... 0 = {0}", eventArgs.NetState.ReceiveBuffer.Length);

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

            ReceiveQueue receiveQueueBuffer = eventArgs.NetState.ReceiveBuffer;

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

            WorldExtendData extendData = eventArgs.NetState.GetComponent <WorldExtendData>(WorldExtendData.COMPONENT_ID);

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

            if (receiveQueueBuffer.Length < extendData.ProcessReceiveData.Remaining)
            {
                                                                                       // 等待数据包的完整(Remaining 默认的大小为WORLD_HEAD_SIZE)
                return;
            }

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

            while (iReceiveBufferLength >= extendData.ProcessReceiveData.Remaining)
            {
                if (extendData.ProcessReceiveData.PacketBuffer == null)   // 如果空 代表是需要获取新的数据包和包头(Remaining == WORLD_HEAD_SIZE)
                {
                    // 获取空数据
                    byte[] packetBuffer = s_ProcessorBuffers.AcquireBuffer();

                    // 获取数据包头的内容
                    long iReturnPacketHeadSize = receiveQueueBuffer.Dequeue(ref packetBuffer, 0, WORLD_HEAD_SIZE);

                    // 获取的数据不相同
                    if (iReturnPacketHeadSize != WORLD_HEAD_SIZE)
                    {
                        Debug.WriteLine("ProcessNet.World_ProcessReceive(...) - iReturnPacketHeadSize != WORLD_HEAD_SIZE error!");

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

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

                    IPacketEncoder packetEncoder = eventArgs.NetState.PacketEncoder as IPacketEncoder;
                    if (packetEncoder == null)
                    {
                        Debug.WriteLine("ProcessNet.World_ProcessReceive(...) - packetEncoder == null error!");

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

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

                    // 解密数据包头
                    long iClientPacketHeader = WORLD_HEAD_SIZE;
                    packetEncoder.DecodeIncomingPacket(eventArgs.NetState, ref packetBuffer, ref iClientPacketHeader);

                    // 给出数据信息头
                    WOWClientPacketHeader clientPacketHeader = WOWClientPacketHeader.GetClientPacketHeader(packetBuffer);

                    int iRemaining = clientPacketHeader.PacketSize - 4; // -4 是代表剩余的数据包大小(为什么不是减6呢?)
                    if (receiveQueueBuffer.Length < iRemaining)
                    {
                                                                  // 等待数据包的完整
                        {
                            extendData.ProcessReceiveData.PacketBuffer = packetBuffer;
                            extendData.ProcessReceiveData.Remaining    = (uint)iRemaining;

                            return;
                        }
                    }

                    // 获取信息包数据的剩余内容
                    long iReturnPacketSize = receiveQueueBuffer.Dequeue(ref packetBuffer, WORLD_HEAD_SIZE, iRemaining /*获取剩余数据包大小的字节数*/);

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

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

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

                    // 获取包的全部长度
                    long iPacketFullLength = iRemaining + WORLD_HEAD_SIZE;

                    // 读取的数据包
                    PacketReader packetReader = new PacketReader(packetBuffer, iPacketFullLength, WORLD_HEAD_SIZE /*包的ID大小-4个字节、长度大小-2个字节, 6个字节跳过*/);


                    //////////////////////////////////////////////////////////////////////////
                    // 输出信息包的日志
                    //////////////////////////////////////////////////////////////////////////
                    ProcessNet.LogServerPack(packetBuffer, iPacketFullLength, "World_In_Packets.log", eventArgs.NetState.ToString(), clientPacketHeader.PacketID, WordOpCodeName.GetWordOpCodeName(clientPacketHeader.PacketID));
                    //////////////////////////////////////////////////////////////////////////


                    // 获取处理数据包的实例
                    PacketHandler packetHandler = ProcessServer.WorldPacketHandlers.GetHandler(clientPacketHeader.PacketID);
                    if (packetHandler == null)   // 说明还没有解开当前的数据包内容
                    {
                        //////////////////////////////////////////////////////////////////////////
                        // 输出未知信息包的日志
                        //////////////////////////////////////////////////////////////////////////
                        ProcessNet.LogServerPack(packetBuffer, iPacketFullLength, "World_In_Unknown_Packets.log", eventArgs.NetState.ToString(), clientPacketHeader.PacketID, WordOpCodeName.GetWordOpCodeName(clientPacketHeader.PacketID));
                        //////////////////////////////////////////////////////////////////////////

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

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

                        continue;
                    }

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

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

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

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

                    // 获取剩下的数据长度
                    iReceiveBufferLength = receiveQueueBuffer.Length;
                }
                else    //已有数据包头获取剩余数据
                {
                    byte[] packetBuffer = extendData.ProcessReceiveData.PacketBuffer;

                    // 获取信息包数据的剩余内容
                    long iReturnPacketSize = receiveQueueBuffer.Dequeue(ref packetBuffer, WORLD_HEAD_SIZE, extendData.ProcessReceiveData.Remaining /*获取剩余数据包大小的字节数*/);

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

                        // 恢复原始数据状态
                        extendData.ProcessReceiveData.PacketBuffer = null;
                        extendData.ProcessReceiveData.Remaining    = WORLD_HEAD_SIZE;

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

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

                    // 获取包的全部长度
                    long iPacketFullLength = extendData.ProcessReceiveData.Remaining + WORLD_HEAD_SIZE;

                    // 读取的数据包
                    PacketReader packetReader = new PacketReader(extendData.ProcessReceiveData.PacketBuffer, iPacketFullLength, WORLD_HEAD_SIZE /*包的ID大小-4个字节、长度大小-2个字节, 6个字节跳过*/);

                    // 给出数据信息头
                    WOWClientPacketHeader clientPacketHeader = WOWClientPacketHeader.GetClientPacketHeader(extendData.ProcessReceiveData.PacketBuffer);


                    //////////////////////////////////////////////////////////////////////////
                    // 输出信息包的日志
                    //////////////////////////////////////////////////////////////////////////
                    ProcessNet.LogServerPack(packetBuffer, iPacketFullLength, "World_In_Packets.log", eventArgs.NetState.ToString(), clientPacketHeader.PacketID, WordOpCodeName.GetWordOpCodeName(clientPacketHeader.PacketID));
                    //////////////////////////////////////////////////////////////////////////


                    // 获取处理数据包的实例
                    PacketHandler packetHandler = ProcessServer.WorldPacketHandlers.GetHandler(clientPacketHeader.PacketID);
                    if (packetHandler == null)   // 说明还没有解开当前的数据包内容
                    {
                        //////////////////////////////////////////////////////////////////////////
                        // 输出未知信息包的日志
                        //////////////////////////////////////////////////////////////////////////
                        ProcessNet.LogServerPack(packetBuffer, iPacketFullLength, "World_In_Unknown_Packets.log", eventArgs.NetState.ToString(), clientPacketHeader.PacketID, WordOpCodeName.GetWordOpCodeName(clientPacketHeader.PacketID));
                        //////////////////////////////////////////////////////////////////////////

                        // 恢复原始数据状态
                        extendData.ProcessReceiveData.PacketBuffer = null;
                        extendData.ProcessReceiveData.Remaining    = WORLD_HEAD_SIZE;

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

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

                        continue;
                    }

                    // 当前需处理的数据包的大小
                    long iPacketHandlerLength = packetHandler.Length;
                    if (iPacketHandlerLength > iPacketFullLength)   // 包需求的数据大小大于得到的数据大小
                    {
                        // 恢复原始数据状态
                        extendData.ProcessReceiveData.PacketBuffer = null;
                        extendData.ProcessReceiveData.Remaining    = WORLD_HEAD_SIZE;

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

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

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

                    // 返回内存池
                    s_ProcessorBuffers.ReleaseBuffer(extendData.ProcessReceiveData.PacketBuffer);

                    // 恢复原始数据状态
                    extendData.ProcessReceiveData.PacketBuffer = null;
                    extendData.ProcessReceiveData.Remaining    = WORLD_HEAD_SIZE;

                    // 获取剩下的数据长度
                    iReceiveBufferLength = receiveQueueBuffer.Length;
                }
            }
        }
Esempio n. 12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="netState"></param>
        internal static void RealmProcessReceive(object sender, NetStateEventArgs eventArgs)
        {
            LOGs.WriteLine(LogMessageType.MSG_HACK, "Realm_ProcessReceive......= {0}", eventArgs.NetState.ReceiveBuffer.Length);

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

            ReceiveQueue receiveQueueBuffer = eventArgs.NetState.ReceiveBuffer;

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

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

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

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

                // 获取包的 命令ID长度
                long iPacketLength = receiveQueueBuffer.GetPacketLength();

                // 获取包的全部长度
                long iPacketFullLength = iPacketLength + REALM_HEAD_SIZE;

                // 等待数据包的完整
                if (iReceiveBufferLength < iPacketFullLength)
                {
                    break;
                }

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

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

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

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

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

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

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


                //////////////////////////////////////////////////////////////////////////
                // 输出信息包的日志
                //////////////////////////////////////////////////////////////////////////
                ProcessNet.LogServerPack(packetBuffer, iPacketFullLength, "Realm_In_Packets.log", eventArgs.NetState.ToString(), iPacketID, RealmOpCodeName.GetRealmOpCodeName(iPacketID));
                //////////////////////////////////////////////////////////////////////////


                // 读取的数据包
                PacketReader packetReader = new PacketReader(packetBuffer, iPacketFullLength, REALM_HEAD_SIZE /*包的ID大小-1个字节、长度大小-2个字节, 3个字节跳过*/);

                // 获取处理数据包的实例
                PacketHandler packetHandler = ProcessServer.RealmPacketHandlers.GetHandler(iPacketID);
                if (packetHandler == null)   // 说明还没有解开当前的数据包内容
                {
                    //////////////////////////////////////////////////////////////////////////
                    // 输出信息包的日志
                    //////////////////////////////////////////////////////////////////////////
                    ProcessNet.LogServerPack(packetBuffer, iPacketFullLength, "Realm_Unknown_Packets.log", eventArgs.NetState.ToString(), iPacketID, RealmOpCodeName.GetRealmOpCodeName(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;
            }
        }
Esempio n. 13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="netState"></param>
        internal static void Zone_ProcessReceive(object sender, NetStateEventArgs eventArgs)
        {
            LOGs.WriteLine(LogMessageType.MSG_HACK, "ProcessNet.MessagePump_ProcessReceive(...) {0}", Thread.CurrentThread.Name);

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

            ZoneExtendData l_ExtendData = null;

            if (eventArgs.NetState.ExtendData == null)
            {
                Debug.WriteLine("ProcessNet.MessagePump_ProcessReceive(...) - eventArgs.NetState.ExtendData == null error!");

                eventArgs.NetState.Dispose(false);
                return;
            }
            else
            {
                l_ExtendData = eventArgs.NetState.ExtendData as ZoneExtendData;
                if (l_ExtendData == null)
                {
                    Debug.WriteLine("ProcessNet.MessagePump_ProcessReceive(...) - l_ExtendData == null error!");

                    eventArgs.NetState.Dispose(false);
                    return;
                }
            }

            ReceiveQueue l_ReceiveQueueBuffer = eventArgs.NetState.ReceiveBuffer;

            if (l_ReceiveQueueBuffer == null)
            {
                Debug.WriteLine("ProcessNet.MessagePump_ProcessReceive(...) - byteQueueBuffer == null error!");
                return;
            }

            LOGs.WriteLine(LogMessageType.MSG_HACK, "ProcessNet.MessagePump_ProcessReceive(...) Length = {0},{1}", l_ReceiveQueueBuffer.Length, Thread.CurrentThread.Name);

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

            while (l_iReceiveBufferLength > 0)
            {
                // 获取空数据
                byte[] l_PacketBuffer = s_ProcessorBuffers.AcquireBuffer();

                // ReceiveQueue内部已经有锁定
                long l_iReturnPacketSize = l_ReceiveQueueBuffer.Dequeue(ref l_PacketBuffer, 0, l_iReceiveBufferLength);

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

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

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

                // 发送数据
                l_ExtendData.SendToZoneClusterServer(l_PacketBuffer, l_iReturnPacketSize);

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

                // 获取剩下的数据长度
                l_iReceiveBufferLength = l_ReceiveQueueBuffer.Length;
            }
        }
Esempio n. 14
0
        public bool HandleReceive(ByteQueue ns)
        {
            ByteQueue buffer = ns;

            if (buffer == null || buffer.Length <= 0)
            {
                return(true);
            }

            lock (buffer)
            {
                int length = buffer.Length;

                if (!ns.Sender.Seeded)
                {
                    if (buffer.GetPacketID() == 0xEF)
                    {
                        // new packet in client 6.0.5.0 replaces the traditional seed method with a seed packet
                        // 0xEF = 239 = multicast IP, so this should never appear in a normal seed.  So this is backwards compatible with older clients.
                        ns.Sender.Seeded = true;
                    }
                    else if (buffer.Length >= 4)
                    {
                        buffer.Dequeue(m_Peek, 0, 4);

                        int seed = (m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3];

                        if (seed == 0)
                        {
                            Console.WriteLine("Login: {0}: Invalid client detected, disconnecting", ns);
                            ns.Socket.Dispose();
                            return(false);
                        }

                        ns.Sender.Seed   = seed;
                        ns.Sender.Seeded = true;

                        length = buffer.Length;
                    }
                    else
                    {
                        return(true);
                    }
                }

                while (length > 0)
                {
                    int packetID = buffer.GetPacketID();

                    PacketHandler handler = m_PacketsHander.GetHandler(packetID);

                    if (handler == null)
                    {
                        return(false);
                    }

                    int packetLength = handler.Length;

                    if (packetLength <= 0)
                    {
                        if (length >= 3)
                        {
                            packetLength = buffer.GetPacketLength();

                            if (packetLength < 3)
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (length >= packetLength)
                    {
                        byte[] packetBuffer;

                        if (BufferSize >= packetLength)
                        {
                            packetBuffer = m_Buffers.AcquireBuffer();
                        }
                        else
                        {
                            packetBuffer = new byte[packetLength];
                        }

                        packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);

                        PacketReader r = new PacketReader(packetBuffer, packetLength, handler.Length != 0);

                        handler.OnReceive(ns, r, ns.Socket);
                        length = buffer.Length;

                        if (BufferSize >= packetLength)
                        {
                            m_Buffers.ReleaseBuffer(packetBuffer);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(true);
        }
Esempio n. 15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="netState"></param>
        internal void ListenerProcessReceive(object sender, NetStateEventArgs eventArgs)
        {
            if (eventArgs.NetState == null)
            {
                throw new Exception("Domain.ListenerProcessReceive(...) - netState == null error!");
            }

            if (eventArgs.NetState.ReceiveBuffer == null)
            {
                throw new Exception("Domain.ListenerProcessReceive(...) - eventArgs.NetState.ReceiveBuffer == null error!");
            }

            ReceiveQueue receiveQueueBuffer = eventArgs.NetState.ReceiveBuffer;

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

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

            while (iReceiveBufferLength >= PACKAGE_HEAD)
            {
                // ReceiveQueue内部已经有锁定
                long iPacketSize = receiveQueueBuffer.GetPacketLength();
                if (iPacketSize < PACKAGE_HEAD)
                {
                    LOGs.WriteLine(LogMessageType.MSG_ERROR, "Domain.ListenerProcessReceive(...) - iPacketSize < PACKAGE_HEAD error!");

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

                if (iReceiveBufferLength < iPacketSize)
                {
                                                          // 等待数据包的完整
                    break;
                }

                if (iPacketSize > BUFFER_SIZE)   // 数据包过大
                {
                    LOGs.WriteLine(LogMessageType.MSG_ERROR, "Domain.ListenerProcessReceive(...) - iPacketSize > BUFFER_SIZE error!");

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

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

                // ReceiveQueue内部已经有锁定
                long iReturnPacketSize = receiveQueueBuffer.Dequeue(ref packetBuffer, 0, iPacketSize);

                // 获取的数据不相同
                if (iReturnPacketSize != iPacketSize)
                {
                    LOGs.WriteLine(LogMessageType.MSG_ERROR, "Domain.ListenerProcessReceive(...) - iReturnPacketSize != iPacketSize error!");

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

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

                // 读取的数据包
                PacketReader packetReader = new PacketReader(packetBuffer, iPacketSize,
                                                             PACKAGE_HEAD /*包的长度大小-2个字节、ID大小-2个字节, 4个字节跳过*/,
                                                             new EventHandler <PacketIdInfoEventArgs>(this.GetPacketID));

                // 获取数据包命令的ID
                long iPacketID = packetReader.GetPacketID();

                // 获取处理数据包的实例
                PacketHandler packetHandler = DomainPacketHandlers.GetHandler(iPacketID);
                if (packetHandler == null)   // 说明还没有解开当前的数据包内容
                {
                    packetReader.Trace(eventArgs.NetState);

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

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

                    continue;
                }

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

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

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

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

                // 获取剩下的数据长度
                iReceiveBufferLength = receiveQueueBuffer.Length;
            }
        }
Esempio n. 16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="netState"></param>
        internal static void MessagePump_ProcessReceive(object sender, NetStateEventArgs eventArgs)
        {
            LOGs.WriteLine(LogMessageType.MSG_HACK, "ProcessNet.MessagePump_ProcessReceive(...){0}", Thread.CurrentThread.Name);

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

            ReceiveQueue l_ReceiveQueueBuffer = eventArgs.NetState.ReceiveBuffer;

            if (l_ReceiveQueueBuffer == null)
            {
                Debug.WriteLine("ProcessNet.MessagePump_ProcessReceive(...) - byteQueueBuffer == null error!");
                return;
            }

            LOGs.WriteLine(LogMessageType.MSG_HACK, "ProcessNet.MessagePump_ProcessReceive(...)-Length={0},{1}", l_ReceiveQueueBuffer.Length, Thread.CurrentThread.Name);

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

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

            while (l_iReceiveBufferLength >= PACKAGE_HEAD)
            {
                // ReceiveQueue内部已经有锁定
                long l_iPacketSize = l_ReceiveQueueBuffer.GetPacketLength();
                if (l_iPacketSize < PACKAGE_HEAD)
                {
                    Debug.WriteLine("ProcessNet.MessagePump_ProcessReceive(...) - iPacketSize <= 0 error!");

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

                if (l_iReceiveBufferLength < l_iPacketSize)
                {
                                                              // 等待数据包的完整
                    break;
                }

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

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

                // 获取空数据
                byte[] l_PacketBuffer = m_ProcessorBuffers.AcquireBuffer();

                // ReceiveQueue内部已经有锁定
                long l_iReturnPacketSize = l_ReceiveQueueBuffer.Dequeue(ref l_PacketBuffer, 0, l_iPacketSize);

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

                    // 返回内存池
                    m_ProcessorBuffers.ReleaseBuffer(l_PacketBuffer);

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

                //////////////////////////////////////////////////////////////////////////
                using (StreamWriter streamWriter = new StreamWriter("PreIn_Packets.log", true))
                {
                    byte[] byteBuffer = l_PacketBuffer;

                    if (byteBuffer.Length > 0)
                    {
                        streamWriter.WriteLine("客户端:  {0}  未经解密过的信息包 长度 = 0x{1:X4} ID = Unknown", eventArgs.NetState, l_iPacketSize);
                        streamWriter.WriteLine("--------------------------------------------------------------------------");
                    }

                    using (MemoryStream memoryStream = new MemoryStream(byteBuffer))
                        Utility.FormatBuffer(streamWriter, memoryStream, l_iPacketSize);

                    streamWriter.WriteLine();
                    streamWriter.WriteLine();
                }
                //////////////////////////////////////////////////////////////////////////

                try
                {
                    // 解密数据包
                    ROSECrypt.CryptPacket(ref l_PacketBuffer, eventArgs.NetState.EncoderSeed);
                }
                catch
                {
                    Debug.WriteLine("ProcessNet.MessagePump_ProcessReceive(...) - ROSECrypt.CryptPacket(...) Exception error!");

                    // 返回内存池
                    m_ProcessorBuffers.ReleaseBuffer(l_PacketBuffer);

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

                // 读取的数据包
                PacketReader l_PacketReader = new PacketReader(l_PacketBuffer, l_iPacketSize, PACKAGE_HEAD /*包的长度大小-2个字节、ID大小-2个字节、未用数据大小-2个字节, 6个字节跳过*/);

                //////////////////////////////////////////////////////////////////////////
                using (StreamWriter streamWriter = new StreamWriter("In_Packets.log", true))
                {
                    byte[] byteBuffer = l_PacketBuffer;

                    if (byteBuffer.Length > 0)
                    {
                        streamWriter.WriteLine("客户端:  {0}  经解密过的信息包 长度 = 0x{1:X4} ID = 0x{2:X4}", eventArgs.NetState, l_iPacketSize, l_PacketReader.GetPacketID());
                        streamWriter.WriteLine("--------------------------------------------------------------------------");
                    }

                    using (MemoryStream memoryStream = new MemoryStream(byteBuffer))
                        Utility.FormatBuffer(streamWriter, memoryStream, l_iPacketSize);

                    streamWriter.WriteLine();
                    streamWriter.WriteLine();
                }
                //////////////////////////////////////////////////////////////////////////

                // 获取数据包命令的ID
                long l_iPacketID = l_PacketReader.GetPacketID();

                LOGs.WriteLine(LogMessageType.MSG_HACK, "ProcessNet.MessagePump_ProcessReceive(...)-Packet ID = 0x{0:X4}", l_iPacketID);

                // 获取处理数据包的实例
                PacketHandler l_PacketHandler = PacketHandlers.GetHandler(l_iPacketID);
                if (l_PacketHandler == null)   // 说明还没有解开当前的数据包内容
                {
                    l_PacketReader.Trace(eventArgs.NetState);

                    // 返回内存池
                    m_ProcessorBuffers.ReleaseBuffer(l_PacketBuffer);

                    // 获取剩下的数据长度
                    l_iReceiveBufferLength = l_ReceiveQueueBuffer.Length;

                    continue;
                }

                // 当前需处理的数据包的大小
                long l_iPacketHandlerLength = l_PacketHandler.Length;
                if (l_iPacketHandlerLength > l_iReturnPacketSize)   // 包需求的数据大小大于得到的数据大小
                {
                    // 返回内存池
                    m_ProcessorBuffers.ReleaseBuffer(l_PacketBuffer);

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

                PacketProfile l_PacketProfile = PacketProfile.GetIncomingProfile(l_iPacketID);
                DateTime      dateTimeStart   = (l_PacketProfile == null ? DateTime.MinValue : DateTime.Now);
                {
                    l_PacketHandler.OnReceive(eventArgs.NetState, l_PacketReader);
                }
                if (l_PacketProfile != null)
                {
                    l_PacketProfile.Record(l_iPacketHandlerLength, DateTime.Now - dateTimeStart);
                }

                // 返回内存池
                m_ProcessorBuffers.ReleaseBuffer(l_PacketBuffer);

                // 获取剩下的数据长度
                l_iReceiveBufferLength = l_ReceiveQueueBuffer.Length;
            }
        }
 public void AcquireBufferTest()
 {
     string strBufferName = string.Empty; // TODO: 初始化为适当的值
     long iInitialCapacity = 0; // TODO: 初始化为适当的值
     long iBufferSize = 0; // TODO: 初始化为适当的值
     BufferPool target = new BufferPool( strBufferName, iInitialCapacity, iBufferSize ); // TODO: 初始化为适当的值
     byte[] expected = null; // TODO: 初始化为适当的值
     byte[] actual;
     actual = target.AcquireBuffer();
     Assert.AreEqual( expected, actual );
     Assert.Inconclusive( "验证此测试方法的正确性。" );
 }
Esempio n. 18
0
        public bool OnReceive(NetState ns)
        {
            ByteQueue buffer = ns.Buffer;

            if (buffer == null || buffer.Length <= 0)
            {
                return(true);
            }

            lock (buffer)
            {
                int length = buffer.Length;

                while (length > 0)
                {
                    int packetID = buffer.GetPacketID();

                    PacketHandler handler = ns.GetHandler(packetID);

                    if (handler == null)
                    {
                        byte[] data = new byte[length];
                        length = buffer.Dequeue(data, 0, length);

                        new PacketReader(data, length, false).Trace(ns);

                        break;
                    }

                    int packetLength = handler.Length;

                    if (packetLength <= 0)
                    {
                        if (length >= 3)
                        {
                            packetLength = buffer.GetPacketLength();

                            if (packetLength < 3)
                            {
                                ns.Dispose();
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (length < packetLength)
                    {
                        break;
                    }

                    byte[] packetBuffer = BufferSize >= packetLength?_buffers.AcquireBuffer() : new byte[packetLength];

                    packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);

                    PacketReader r = new PacketReader(packetBuffer, packetLength, handler.Length != 0);

                    handler.OnReceive(ns, r);

                    length = buffer.Length;

                    if (BufferSize >= packetLength)
                    {
                        _buffers.ReleaseBuffer(packetBuffer);
                    }
                }
            }

            return(true);
        }
Esempio n. 19
0
 /// <summary>
 ///
 /// </summary>
 /// <returns></returns>
 internal static byte[] AcquireBuffer()
 {
     return(s_UnusedBuffers.AcquireBuffer());
 }