public void Send(IPacket p) { if (m_Socket == null || m_BlockAllPackets) { return; } PacketProfile prof = PacketProfile.GetOutgoingProfile((byte)p.PacketID); DateTime start = (prof == null ? DateTime.MinValue : DateTime.UtcNow); byte[] buffer = p.Compile(m_CompressionEnabled); if (buffer != null) { if (buffer.Length <= 0) { return; } int length = buffer.Length; if (m_Encoder != null) { m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length); } bool shouldBegin = false; lock (m_SendQueue) shouldBegin = (m_SendQueue.Enqueue(buffer, length)); if (shouldBegin) { int sendLength = 0; byte[] sendBuffer = m_SendQueue.Peek(ref sendLength); try { m_Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, m_OnSend, null); m_Sending = true; //Console.WriteLine( "Send: {0}: Begin send of {1} bytes", this, sendLength ); } catch (Exception ex) { log.Error(ex); Dispose(false); } } if (prof != null) { prof.Record(length, DateTime.UtcNow - start); } } else { Dispose(); } }
public void Send( IPacket p ) { if ( m_Socket == null || m_BlockAllPackets ) return; PacketProfile prof = PacketProfile.GetOutgoingProfile( (byte)p.PacketID ); DateTime start = ( prof == null ? DateTime.MinValue : DateTime.UtcNow ); byte[] buffer = p.Compile( m_CompressionEnabled ); if ( buffer != null ) { if ( buffer.Length <= 0 ) return; int length = buffer.Length; if ( m_Encoder != null ) m_Encoder.EncodeOutgoingPacket( this, ref buffer, ref length ); bool shouldBegin = false; lock ( m_SendQueue ) shouldBegin = ( m_SendQueue.Enqueue( buffer, length ) ); if ( shouldBegin ) { int sendLength = 0; byte[] sendBuffer = m_SendQueue.Peek( ref sendLength ); try { m_Socket.BeginSend( sendBuffer, 0, sendLength, SocketFlags.None, m_OnSend, null ); m_Sending = true; //Console.WriteLine( "Send: {0}: Begin send of {1} bytes", this, sendLength ); } catch (Exception ex) { log.Error(ex); Dispose( false ); } } if ( prof != null ) prof.Record( length, DateTime.UtcNow - start ); } else { Dispose(); } }