private void Receive_Process(SocketAsyncEventArgs e) { int byteCount = e.BytesTransferred; if (e.SocketError != SocketError.Success || byteCount <= 0) { Dispose(false); return; } m_NextCheckActivity = DateTime.Now + TimeSpan.FromMinutes(1.2); byte[] buffer = m_RecvBuffer; if (m_Encoder != null) { m_Encoder.DecodeIncomingPacket(this, ref buffer, ref byteCount); } lock (m_Buffer) m_Buffer.Enqueue(buffer, 0, byteCount); m_MessagePump.OnReceive(this); lock (m_AsyncLock) { m_AsyncState &= ~AsyncState.Pending; } }
private void Receive_Process(SocketAsyncEventArgs e) { int byteCount = e.BytesTransferred; if (e.SocketError != SocketError.Success || byteCount <= 0) { Dispose(false); return; } m_NextCheckActivity = DateTime.UtcNow + TimeSpan.FromMinutes(1.2); byte[] buffer = m_RecvBuffer; if (m_Encoder != null) { m_Encoder.DecodeIncomingPacket(this, ref buffer, ref byteCount); } //If we support encryption, then it is possible that the client was DISPOSED at this point. if (!m_Disposing) { lock (m_Buffer) m_Buffer.Enqueue(buffer, 0, byteCount); m_MessagePump.OnReceive(this); lock (m_AsyncLock) m_AsyncState &= ~AsyncState.Pending; } }
public void DecodeIncomingPacket(NetState from, ref byte[] buffer, ref int length) { if (_Successor != null) { _Successor.DecodeIncomingPacket(from, ref buffer, ref length); } }
private void OnReceive(IAsyncResult asyncResult) { Socket s = (Socket)asyncResult.AsyncState; try { int byteCount = s.EndReceive(asyncResult); if (byteCount > 0) { m_NextCheckActivity = DateTime.Now + TimeSpan.FromMinutes(1.2); byte[] buffer = m_RecvBuffer; if (m_Encoder != null) { m_Encoder.DecodeIncomingPacket(this, ref buffer, ref byteCount); } lock (m_Buffer) m_Buffer.Enqueue(buffer, 0, byteCount); m_MessagePump.OnReceive(this); lock (m_AsyncLock) { m_AsyncState &= ~AsyncState.Pending; if ((m_AsyncState & AsyncState.Paused) == 0) { try { InternalBeginReceive(); } catch (Exception ex) { TraceException(ex); Dispose(false); } } } } else { Dispose(false); } } catch { Dispose(false); } }
private void OnReceive(IAsyncResult asyncResult) { Socket s = (Socket)asyncResult.AsyncState; try { int byteCount = s.EndReceive(asyncResult); if (byteCount > 0) { UpdateAcitivty(); byte[] buffer = mRecvBuffer; if (mEncoder != null) { mEncoder.DecodeIncomingPacket(this, ref buffer, ref byteCount); } lock (mBuffer) { mBuffer.Enqueue(buffer, 0, byteCount); } //mMessagePump.OnReceive(this); lock (mMessagePump) { //Core.Set(); mMessagePump.HandleReceive(this); } lock (mAsyncLock) { mAsyncState &= ~AsyncNetState.Pending; if ((mAsyncState & AsyncNetState.Paused) == 0) { try { InternalBeginReceive(); } catch (Exception ex) { ExceptionHandler.Trace(ex); Dispose(false); } } } } else { Dispose(false); } } catch (Exception ex) { ServerConsole.ErrorLine(ex.Message); ServerConsole.ErrorLine(ex.StackTrace); //ServerConsole.ErrorLine(Tools.CleanExcepionStacktrace(ex.StackTrace)); Dispose(false); } }
public void DecodeIncomingPacketTest() { IPacketEncoder target = CreateIPacketEncoder(); // TODO: 初始化为适当的值 NetState netStateFrom = null; // TODO: 初始化为适当的值 byte[] byteBuffer = null; // TODO: 初始化为适当的值 byte[] byteBufferExpected = null; // TODO: 初始化为适当的值 long iLength = 0; // TODO: 初始化为适当的值 long iLengthExpected = 0; // TODO: 初始化为适当的值 target.DecodeIncomingPacket(netStateFrom, ref byteBuffer, ref iLength); Assert.AreEqual(byteBufferExpected, byteBuffer); Assert.AreEqual(iLengthExpected, iLength); Assert.Inconclusive("无法验证不返回值的方法。"); }
private void OnReceive(IAsyncResult asyncResult) { Socket s = (Socket)asyncResult.AsyncState; try { int byteCount = s.EndReceive(asyncResult); if (byteCount > 0) { UpdateAcitivty(); byte[] buffer = mRecvBuffer; if (mEncoder != null) { mEncoder.DecodeIncomingPacket(this, ref buffer, ref byteCount); } lock (mBuffer) mBuffer.Enqueue(buffer, 0, byteCount); mMessagePump.OnReceive(this); lock ( mAsyncLock ) { mAsyncState &= ~AsyncNetState.Pending; if ((mAsyncState & AsyncNetState.Paused) == 0) { try { InternalBeginReceive(); } catch (Exception ex) { ExceptionHandler.Trace(ex); Dispose(false); } } } } else { Dispose(false); } } catch { Dispose(false); } }
private void OnReceive(IAsyncResult asyncResult) { lock (this) { if (m_Socket == null) { return; } try { int byteCount = m_Socket.EndReceive(asyncResult); if (byteCount > 0) { m_NextCheckActivity = DateTime.Now + TimeSpan.FromMinutes(1.2); byte[] buffer = m_RecvBuffer; if (m_Encoder != null) { m_Encoder.DecodeIncomingPacket(this, ref buffer, ref byteCount); } m_Buffer.Enqueue(buffer, 0, byteCount); m_Incoming += byteCount; m_NetServer.OnReceive(this, byteCount); } else { Dispose(false); } } catch (Exception ex) { TraceException(ex); Dispose(false); } } }
private void OnReceive(IAsyncResult asyncResult) { lock (this) { if (m_Socket == null) { return; } try { int byteCount = m_Socket.EndReceive(asyncResult); if (byteCount > 0) { m_NextCheckActivity = Core.Now + TimeSpan.FromMinutes(1.2); byte[] buffer = m_RecvBuffer; if (m_Encoder != null) { m_Encoder.DecodeIncomingPacket(this, ref buffer, ref byteCount); } m_Buffer.Enqueue(buffer, 0, byteCount); m_MessagePump.OnReceive(this); } else { Dispose(false); } } catch // ( Exception ex ) { //Console.WriteLine(ex); Dispose(false); } } }
/// <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; } } }
private void Receive_Process( SocketAsyncEventArgs e ) { //Plume : Net 4 /*int byteCount = e.BytesTransferred; if ( e.SocketError != SocketError.Success || byteCount <= 0 ) { Dispose( false ); return; } m_NextCheckActivity = DateTime.Now + TimeSpan.FromMinutes( 1.2 ); byte[] buffer = m_RecvBuffer; if ( m_Encoder != null ) m_Encoder.DecodeIncomingPacket( this, ref buffer, ref byteCount ); lock ( m_Buffer ) m_Buffer.Enqueue( buffer, 0, byteCount ); m_MessagePump.OnReceive( this ); lock ( m_AsyncLock ) { m_AsyncState &= ~AsyncState.Pending;*/ try { if (!m_Disposing) { int byteCount = e.BytesTransferred; if ( e.SocketError != SocketError.Success || byteCount <= 0 ) { Dispose( false ); return; } else { m_NextCheckActivity = DateTime.Now + TimeSpan.FromMinutes( 1.2 ); byte[] buffer = m_RecvBuffer; if ( m_Encoder != null ) m_Encoder.DecodeIncomingPacket( this, ref buffer, ref byteCount ); if (m_Buffer != null) { lock ( m_Buffer ) m_Buffer.Enqueue( buffer, 0, byteCount ); if (!m_Disposing) { m_MessagePump.OnReceive( this ); lock ( m_AsyncLock ) { m_AsyncState &= ~AsyncState.Pending; } } } } } } catch (Exception ex) { Console.WriteLine("Exception on Netstate in Receive_Process"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }