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;
import com.google.common.base.Preconditions;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import io.netty.buffer.ByteBuf;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.IOException;
import io.netty.buffer.ReferenceCounted;
import io.netty.buffer.Unpooled;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
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
* retrieved via the {@link #getPacket()} method.
*/
public abstract class DefinedPacket implements DataOutput
public abstract class DefinedPacket implements ByteBuf
{
private interface Overriden
@Delegate(types =
{
void readUTF();
void writeUTF(String s);
}
private ByteArrayInputStream bin;
private DataInputStream input;
@Delegate(excludes = Overriden.class)
private ByteArrayDataOutput out;
ByteBuf.class, ReferenceCounted.class
})
private ByteBuf out;
/**
* Packet id.
*/
public final int id;
/**
* Already constructed packet.
*/
private byte[] packet;
public DefinedPacket(int id, byte[] buf)
{
bin = new ByteArrayInputStream( buf );
input = new DataInputStream( bin );
out = Unpooled.wrappedBuffer( buf );
if ( readUnsignedByte() != id )
{
throw new IllegalArgumentException( "Wasn't expecting packet id " + Util.hex( id ) );
}
this.id = id;
packet = buf;
}
public DefinedPacket(int id)
{
out = ByteStreams.newDataOutput();
out = Unpooled.buffer();
this.id = id;
writeByte( id );
}
/**
* 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)
public void writeString(String s)
{
writeShort( s.length() );
writeChars( s );
for ( char c : s.toCharArray() )
{
writeChar( c );
}
}
public String readUTF()
public String readString()
{
short len = readShort();
char[] chars = new char[ len ];
@ -92,99 +67,17 @@ public abstract class DefinedPacket implements DataOutput
public void writeArray(byte[] b)
{
writeShort( b.length );
write( b );
writeBytes( b );
}
public byte[] readArray()
{
short len = readShort();
byte[] ret = new byte[ len ];
readFully( ret );
readBytes( 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
public abstract boolean equals(Object obj);
@ -195,11 +88,12 @@ public abstract class DefinedPacket implements DataOutput
public abstract String toString();
public abstract void handle(PacketHandler handler) throws Exception;
@SuppressWarnings("unchecked")
private static Class<? extends DefinedPacket>[] classes = new Class[ 256 ];
public static DefinedPacket packet(ByteBuf buf)
{
int id = buf.getUnsignedShort( 0);
int id = buf.getUnsignedShort( 0 );
Class<? extends DefinedPacket> clazz = classes[id];
DefinedPacket ret = null;
if ( clazz != null )
@ -214,7 +108,8 @@ public abstract class DefinedPacket implements DataOutput
} catch ( IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException ex )
{
}
} else {
} else
{
return null;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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