/// <summary> /// /// </summary> /// <param name="packetBuffer"></param> /// <returns></returns> public static WOWClientPacketHeader GetClientPacketHeader(byte[] packetBuffer) { WOWClientPacketHeader returnPacketHeader = new WOWClientPacketHeader(); returnPacketHeader.m_iPacketSize = (ushort)((packetBuffer[0] << 8) | packetBuffer[1]); returnPacketHeader.m_iPacketID = (uint)((packetBuffer[2]) | packetBuffer[3] << 8 | packetBuffer[4] << 16 | packetBuffer[5] << 24); LOGs.WriteLine(LogMessageType.MSG_HACK, "GetClientPacketHeader...... 0 = {0} {1}", returnPacketHeader.m_iPacketSize, returnPacketHeader.m_iPacketID); return(returnPacketHeader); }
/// <summary> /// /// </summary> /// <param name="packetBuffer"></param> /// <returns></returns> public static WOWClientPacketHeader GetClientPacketHeader( byte[] packetBuffer ) { WOWClientPacketHeader returnPacketHeader = new WOWClientPacketHeader(); returnPacketHeader.m_iPacketSize = (ushort)( ( packetBuffer[0] << 8 ) | packetBuffer[1] ); returnPacketHeader.m_iPacketID = (uint)( ( packetBuffer[2] ) | packetBuffer[3] << 8 | packetBuffer[4] << 16 | packetBuffer[5] << 24 ); LOGs.WriteLine( LogMessageType.MSG_HACK, "GetClientPacketHeader...... 0 = {0} {1}", returnPacketHeader.m_iPacketSize, returnPacketHeader.m_iPacketID ); return returnPacketHeader; }
/// <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; } } }