Switch main packet interface to bytebuf

This commit is contained in:
md_5 2013-03-08 17:35:01 +11:00
parent cdf26f7950
commit c3d702a5b3
9 changed files with 41 additions and 146 deletions

View File

@ -1,13 +1,9 @@
package net.md_5.bungee.packet; package net.md_5.bungee.packet;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.io.ByteArrayInputStream; import io.netty.buffer.ReferenceCounted;
import java.io.DataInputStream; import io.netty.buffer.Unpooled;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import lombok.Delegate; import lombok.Delegate;
@ -18,67 +14,46 @@ import net.md_5.bungee.Util;
* subclasses can read and write to the backing byte array which can be * subclasses can read and write to the backing byte array which can be
* retrieved via the {@link #getPacket()} method. * retrieved via the {@link #getPacket()} method.
*/ */
public abstract class DefinedPacket implements DataOutput public abstract class DefinedPacket implements ByteBuf
{ {
private interface Overriden @Delegate(types =
{ {
ByteBuf.class, ReferenceCounted.class
void readUTF(); })
private ByteBuf out;
void writeUTF(String s);
}
private ByteArrayInputStream bin;
private DataInputStream input;
@Delegate(excludes = Overriden.class)
private ByteArrayDataOutput out;
/** /**
* Packet id. * Packet id.
*/ */
public final int id; public final int id;
/**
* Already constructed packet.
*/
private byte[] packet;
public DefinedPacket(int id, byte[] buf) public DefinedPacket(int id, byte[] buf)
{ {
bin = new ByteArrayInputStream( buf ); out = Unpooled.wrappedBuffer( buf );
input = new DataInputStream( bin );
if ( readUnsignedByte() != id ) if ( readUnsignedByte() != id )
{ {
throw new IllegalArgumentException( "Wasn't expecting packet id " + Util.hex( id ) ); throw new IllegalArgumentException( "Wasn't expecting packet id " + Util.hex( id ) );
} }
this.id = id; this.id = id;
packet = buf;
} }
public DefinedPacket(int id) public DefinedPacket(int id)
{ {
out = ByteStreams.newDataOutput(); out = Unpooled.buffer();
this.id = id; this.id = id;
writeByte( id ); writeByte( id );
} }
/** public void writeString(String s)
* Gets the bytes that make up this packet.
*
* @return the bytes which make up this packet, either the original byte
* array or the newly written one.
*/
public byte[] getPacket()
{
return packet == null ? packet = out.toByteArray() : packet;
}
@Override
public void writeUTF(String s)
{ {
writeShort( s.length() ); writeShort( s.length() );
writeChars( s ); for ( char c : s.toCharArray() )
{
writeChar( c );
}
} }
public String readUTF() public String readString()
{ {
short len = readShort(); short len = readShort();
char[] chars = new char[ len ]; char[] chars = new char[ len ];
@ -92,99 +67,17 @@ public abstract class DefinedPacket implements DataOutput
public void writeArray(byte[] b) public void writeArray(byte[] b)
{ {
writeShort( b.length ); writeShort( b.length );
write( b ); writeBytes( b );
} }
public byte[] readArray() public byte[] readArray()
{ {
short len = readShort(); short len = readShort();
byte[] ret = new byte[ len ]; byte[] ret = new byte[ len ];
readFully( ret ); readBytes( ret );
return ret; return ret;
} }
public final int available()
{
return bin.available();
}
public final void readFully(byte b[])
{
try
{
input.readFully( b );
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final boolean readBoolean()
{
try
{
return input.readBoolean();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final byte readByte()
{
try
{
return input.readByte();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final int readUnsignedByte()
{
try
{
return input.readUnsignedByte();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final short readShort()
{
try
{
return input.readShort();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final char readChar()
{
try
{
return input.readChar();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final int readInt()
{
try
{
return input.readInt();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
@Override @Override
public abstract boolean equals(Object obj); public abstract boolean equals(Object obj);
@ -195,6 +88,7 @@ public abstract class DefinedPacket implements DataOutput
public abstract String toString(); public abstract String toString();
public abstract void handle(PacketHandler handler) throws Exception; public abstract void handle(PacketHandler handler) throws Exception;
@SuppressWarnings("unchecked")
private static Class<? extends DefinedPacket>[] classes = new Class[ 256 ]; private static Class<? extends DefinedPacket>[] classes = new Class[ 256 ];
public static DefinedPacket packet(ByteBuf buf) public static DefinedPacket packet(ByteBuf buf)
@ -214,7 +108,8 @@ public abstract class DefinedPacket implements DataOutput
} catch ( IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException ex ) } catch ( IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException ex )
{ {
} }
} else { } else
{
return null; return null;
} }

View File

@ -20,7 +20,7 @@ public class Packet1Login extends DefinedPacket
{ {
super( 0x01 ); super( 0x01 );
writeInt( entityId ); writeInt( entityId );
writeUTF( levelType ); writeString( levelType );
writeByte( gameMode ); writeByte( gameMode );
writeByte( dimension ); writeByte( dimension );
writeByte( difficulty ); writeByte( difficulty );
@ -32,12 +32,12 @@ public class Packet1Login extends DefinedPacket
{ {
super( 0x01, buf ); super( 0x01, buf );
this.entityId = readInt(); this.entityId = readInt();
this.levelType = readUTF(); this.levelType = readString();
this.gameMode = readByte(); this.gameMode = readByte();
if ( available() == 4 ) if ( readableBytes() == 4 )
{ {
this.dimension = readByte(); this.dimension = readByte();
} else if ( available() == 7 ) } else if ( readableBytes() == 7 )
{ {
this.dimension = readInt(); this.dimension = readInt();
} else } else

View File

@ -17,8 +17,8 @@ public class Packet2Handshake extends DefinedPacket
{ {
super( 0x02 ); super( 0x02 );
writeByte( protocolVersion ); writeByte( protocolVersion );
writeUTF( username ); writeString( username );
writeUTF( host ); writeString( host );
writeInt( port ); writeInt( port );
} }
@ -26,8 +26,8 @@ public class Packet2Handshake extends DefinedPacket
{ {
super( 0x02, buf ); super( 0x02, buf );
this.procolVersion = readByte(); this.procolVersion = readByte();
this.username = readUTF(); this.username = readString();
this.host = readUTF(); this.host = readString();
this.port = readInt(); this.port = readInt();
} }

View File

@ -13,13 +13,13 @@ public class Packet3Chat extends DefinedPacket
public Packet3Chat(String message) public Packet3Chat(String message)
{ {
super( 0x03 ); super( 0x03 );
writeUTF( message ); writeString( message );
} }
public Packet3Chat(byte[] buf) public Packet3Chat(byte[] buf)
{ {
super( 0x03, buf ); super( 0x03, buf );
this.message = readUTF(); this.message = readString();
} }
@Override @Override

View File

@ -23,7 +23,7 @@ public class Packet9Respawn extends DefinedPacket
writeByte( difficulty ); writeByte( difficulty );
writeByte( gameMode ); writeByte( gameMode );
writeShort( worldHeight ); writeShort( worldHeight );
writeUTF( levelType ); writeString( levelType );
} }
public Packet9Respawn(byte[] buf) public Packet9Respawn(byte[] buf)
@ -33,7 +33,7 @@ public class Packet9Respawn extends DefinedPacket
this.difficulty = readByte(); this.difficulty = readByte();
this.gameMode = readByte(); this.gameMode = readByte();
this.worldHeight = readShort(); this.worldHeight = readShort();
this.levelType = readUTF(); this.levelType = readString();
} }
@Override @Override

View File

@ -15,7 +15,7 @@ public class PacketC9PlayerListItem extends DefinedPacket
public PacketC9PlayerListItem(byte[] packet) public PacketC9PlayerListItem(byte[] packet)
{ {
super( 0xC9, packet ); super( 0xC9, packet );
username = readUTF(); username = readString();
online = readBoolean(); online = readBoolean();
ping = readShort(); ping = readShort();
} }
@ -23,7 +23,7 @@ public class PacketC9PlayerListItem extends DefinedPacket
public PacketC9PlayerListItem(String username, boolean online, int ping) public PacketC9PlayerListItem(String username, boolean online, int ping)
{ {
super( 0xC9 ); super( 0xC9 );
writeUTF( username ); writeString( username );
writeBoolean( online ); writeBoolean( online );
writeShort( ping ); writeShort( ping );
} }

View File

@ -14,7 +14,7 @@ public class PacketFAPluginMessage extends DefinedPacket
public PacketFAPluginMessage(String tag, byte[] data) public PacketFAPluginMessage(String tag, byte[] data)
{ {
super( 0xFA ); super( 0xFA );
writeUTF( tag ); writeString( tag );
writeArray( data ); writeArray( data );
this.tag = tag; this.tag = tag;
this.data = data; this.data = data;
@ -23,7 +23,7 @@ public class PacketFAPluginMessage extends DefinedPacket
public PacketFAPluginMessage(byte[] buf) public PacketFAPluginMessage(byte[] buf)
{ {
super( 0xFA, buf ); super( 0xFA, buf );
this.tag = readUTF(); this.tag = readString();
this.data = readArray(); this.data = readArray();
} }

View File

@ -15,7 +15,7 @@ public class PacketFDEncryptionRequest extends DefinedPacket
public PacketFDEncryptionRequest(String serverId, byte[] publicKey, byte[] verifyToken) public PacketFDEncryptionRequest(String serverId, byte[] publicKey, byte[] verifyToken)
{ {
super( 0xFD ); super( 0xFD );
writeUTF( serverId ); writeString( serverId );
writeArray( publicKey ); writeArray( publicKey );
writeArray( verifyToken ); writeArray( verifyToken );
this.serverId = serverId; this.serverId = serverId;
@ -26,7 +26,7 @@ public class PacketFDEncryptionRequest extends DefinedPacket
public PacketFDEncryptionRequest(byte[] buf) public PacketFDEncryptionRequest(byte[] buf)
{ {
super( 0xFD, buf ); super( 0xFD, buf );
serverId = readUTF(); serverId = readString();
publicKey = readArray(); publicKey = readArray();
verifyToken = readArray(); verifyToken = readArray();
} }

View File

@ -13,13 +13,13 @@ public class PacketFFKick extends DefinedPacket
public PacketFFKick(String message) public PacketFFKick(String message)
{ {
super( 0xFF ); super( 0xFF );
writeUTF( message ); writeString( message );
} }
public PacketFFKick(byte[] buf) public PacketFFKick(byte[] buf)
{ {
super( 0xFF, buf ); super( 0xFF, buf );
this.message = readUTF(); this.message = readString();
} }
@Override @Override