/// <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.MessagePump_ProcessReceive(...) - netState == null error!" ); return; } ReceiveQueue l_ReceiveQueueBuffer = eventArgs.NetState.ReceiveBuffer; if ( l_ReceiveQueueBuffer == null ) { Debug.WriteLine( "ProcessNet.MessagePump_ProcessReceive(...) - byteQueueBuffer == null error!" ); return; } //// 检测是否已经在处理数据中,防止多线程读取里面的数据 //if ( Utility.Lock( netState.ReceiveBuffer ) == false ) // return; LOGs.WriteLine( LogMessageType.MSG_HACK, "ProcessNet.MessagePump_ProcessReceive(...)-Lock(...){0}", Thread.CurrentThread.Name ); if ( l_ReceiveQueueBuffer.Length < PACKAGE_HEAD ) // 等待数据包的完整 { //Utility.Free( netState.ReceiveBuffer ); 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( true ); // 断开 //Utility.Free( netState.ReceiveBuffer ); return; } if ( l_iReceiveBufferLength < l_iPacketSize ) // 等待数据包的完整 break; if ( l_iPacketSize > BUFFER_SIZE ) // 数据包过大 { Debug.WriteLine( "ProcessNet.MessagePump_ProcessReceive(...) - iPacketSize > BUFFER_SIZE error!" ); eventArgs.NetState.Dispose( true ); // 断开 //Utility.Free( netState.ReceiveBuffer ); 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( true ); // 断开 //Utility.Free( netState.ReceiveBuffer ); 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( true ); // 断开 //Utility.Free( netState.ReceiveBuffer ); 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( true ); // 断开 //Utility.Free( netState.ReceiveBuffer ); return; } //ThrottlePacketCallback l_ThrottlePacketCallback = l_PacketHandler.ThrottleCallback; //if ( l_ThrottlePacketCallback != null ) //{ // if ( l_ThrottlePacketCallback( netState ) == false ) // { // Monitor.Enter( m_LockNetStateThrottled ); // { // m_NetStateThrottled.Enqueue( netState ); // } // Monitor.Exit( m_LockNetStateThrottled ); // // 返回内存池 // m_ProcessorBuffers.ReleaseBuffer( l_PacketBuffer ); // 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; } //Utility.Free( netState.ReceiveBuffer ); }