Reuse single buffer for writing packets.

This commit is contained in:
UnknownShadow200 2015-04-28 06:41:03 +10:00
parent 60f0086680
commit 69a166e1f2

View File

@ -157,109 +157,102 @@ namespace ClassicalSharp {
#region Writing #region Writing
private static byte[] MakeLoginPacket( string username, string verKey ) { static byte[] outBuffer = new byte[131];
byte[] buffer = new byte[1 + 1 + 64 + 64 + 1]; static int outIndex = 0;
int index = 0; private static int MakeLoginPacket( string username, string verKey ) {
WriteUInt8( buffer, ref index, 0 ); // packet id WriteUInt8( 0 ); // packet id
WriteUInt8( buffer, ref index, 7 ); // protocol version WriteUInt8( 7 ); // protocol version
WriteString( buffer, ref index, username ); WriteString( username );
WriteString( buffer, ref index, verKey ); WriteString( verKey );
WriteUInt8( buffer, ref index, 0x42 ); WriteUInt8( 0x42 );
return buffer; return 1 + 1 + 64 + 64 + 1;
} }
private static byte[] MakeSetBlockPacket( short x, short y, short z, byte mode, byte block ) { private static int MakeSetBlockPacket( short x, short y, short z, byte mode, byte block ) {
byte[] buffer = new byte[1 + 3 * 2 + 1 + 1]; WriteUInt8( 0x05 ); // packet id
int index = 0; WriteInt16( x );
WriteUInt8( buffer, ref index, 0x05 ); // packet id WriteInt16( y );
WriteInt16( buffer, ref index, x ); WriteInt16( z );
WriteInt16( buffer, ref index, y ); WriteUInt8( mode );
WriteInt16( buffer, ref index, z ); WriteUInt8( block );
WriteUInt8( buffer, ref index, mode ); return 1 + 3 * 2 + 1 + 1;
WriteUInt8( buffer, ref index, block );
return buffer;
} }
private static byte[] MakePositionPacket( Vector3 pos, byte yaw, byte pitch, byte playerId ) { private static int MakePositionPacket( Vector3 pos, byte yaw, byte pitch, byte playerId ) {
byte[] buffer = new byte[1 + 1 + 3 * 2 + 2 * 1]; WriteUInt8( 0x08 ); // packet id
int index = 0; WriteUInt8( playerId ); // player id (-1 is self)
WriteUInt8( buffer, ref index, 0x08 ); // packet id WriteInt16( (short)( pos.X * 32 ) );
WriteUInt8( buffer, ref index, playerId ); // player id (-1 is self) WriteInt16( (short)( (int)( pos.Y * 32 ) + 51 ) );
WriteInt16( buffer, ref index, (short)( pos.X * 32 ) ); WriteInt16( (short)( pos.Z * 32 ) );
WriteInt16( buffer, ref index, (short)( (int)( pos.Y * 32 ) + 51 ) ); WriteUInt8( yaw );
WriteInt16( buffer, ref index, (short)( pos.Z * 32 ) ); WriteUInt8( pitch );
WriteUInt8( buffer, ref index, yaw ); return 1 + 1 + 3 * 2 + 2 * 1;
WriteUInt8( buffer, ref index, pitch );
return buffer;
} }
private static byte[] MakeMessagePacket( string text ) { private static int MakeMessagePacket( string text ) {
byte[] buffer = new byte[1 + 1 + 64]; WriteUInt8( 0x0D ); // packet id
int index = 0; WriteUInt8( 0xFF ); // unused
WriteUInt8( buffer, ref index, 0x0D ); // packet id WriteString( text );
WriteUInt8( buffer, ref index, 0xFF ); // unused return 1 + 1 + 64;
WriteString( buffer, ref index, text );
return buffer;
} }
private static byte[] MakeExtInfo( string appName, int extensionsCount ) { private static int MakeExtInfo( string appName, int extensionsCount ) {
byte[] buffer = new byte[1 + 64 + 2]; WriteUInt8( 0x10 ); // packet id
int index = 0; WriteString( appName );
WriteUInt8( buffer, ref index, 0x10 ); // packet id WriteInt16( (short)extensionsCount );
WriteString( buffer, ref index, appName ); return 1 + 64 + 2;
WriteInt16( buffer, ref index, (short)extensionsCount );
return buffer;
} }
private static byte[] MakeExtEntry( string extensionName, int extensionVersion ) { private static int MakeExtEntry( string extensionName, int extensionVersion ) {
byte[] buffer = new byte[1 + 64 + 4]; WriteUInt8( 0x11 ); // packet id
int index = 0; WriteString( extensionName );
WriteUInt8( buffer, ref index, 0x11 ); // packet id WriteInt32( extensionVersion );
WriteString( buffer, ref index, extensionName ); return 1 + 64 + 4;
WriteInt32( buffer, ref index, extensionVersion );
return buffer;
} }
private static byte[] MakeCustomBlockSupportLevel( byte version ) { private static int MakeCustomBlockSupportLevel( byte version ) {
return new byte[] { (byte)19, version }; // packet id, version WriteUInt8( 19 ); // packet id
WriteUInt8( version );
return 1 + 1;
} }
static void WriteString( byte[] buffer, ref int index, string value ) { static void WriteString( string value ) {
int count = Math.Min( value.Length, 64 ); int count = Math.Min( value.Length, 64 );
for( int i = 0; i < count; i++ ) { for( int i = 0; i < count; i++ ) {
char c = value[i]; char c = value[i];
buffer[index + i] = (byte)( c >= '\u0080' ? '?' : c ); outBuffer[outIndex + i] = (byte)( c >= '\u0080' ? '?' : c );
} }
for( int i = value.Length; i < 64; i++ ) { for( int i = value.Length; i < 64; i++ ) {
buffer[index + i] = (byte)' '; outBuffer[outIndex + i] = (byte)' ';
} }
index += 64; outIndex += 64;
} }
static void WriteUInt8( byte[] buffer, ref int index, byte value ) { static void WriteUInt8( byte value ) {
buffer[index++] = value; outBuffer[outIndex++] = value;
} }
static void WriteInt16( byte[] buffer, ref int index, short value ) { static void WriteInt16( short value ) {
buffer[index++] = (byte)( value >> 8 ); outBuffer[outIndex++] = (byte)( value >> 8 );
buffer[index++] = (byte)( value ); outBuffer[outIndex++] = (byte)( value );
} }
static void WriteInt32( byte[] buffer, ref int index, int value ) { static void WriteInt32( int value ) {
buffer[index++] = (byte)( value >> 24 ); outBuffer[outIndex++] = (byte)( value >> 24 );
buffer[index++] = (byte)( value >> 16 ); outBuffer[outIndex++] = (byte)( value >> 16 );
buffer[index++] = (byte)( value >> 8 ); outBuffer[outIndex++] = (byte)( value >> 8 );
buffer[index++] = (byte)( value ); outBuffer[outIndex++] = (byte)( value );
} }
void WritePacket( byte[] packet ) { void WritePacket( int packetLength ) {
outIndex = 0;
if( Disconnected ) return; if( Disconnected ) return;
#if NET_DEBUG #if NET_DEBUG
Utils.LogDebug( "writing {0} bytes ({1})", packet.Length, (PacketId)packet[0] ); Utils.LogDebug( "writing {0} bytes ({1})", packetLength, (PacketId)outBuffer[0] );
#endif #endif
try { try {
stream.Write( packet, 0, packet.Length ); stream.Write( outBuffer, 0, packetLength );
} catch( IOException ex ) { } catch( IOException ex ) {
Utils.LogError( "Error while writing packets: {0}{1}", Environment.NewLine, ex ); Utils.LogError( "Error while writing packets: {0}{1}", Environment.NewLine, ex );
Window.Disconnect( "&eLost connection to the server", "Underlying connection was closed" ); Window.Disconnect( "&eLost connection to the server", "Underlying connection was closed" );