Checkpoint

This commit is contained in:
md_5
2013-10-12 11:36:53 +11:00
parent b358fd25f5
commit 1b41682e37
10 changed files with 66 additions and 96 deletions

View File

@@ -1,5 +1,6 @@
package net.md_5.bungee.protocol;
import com.google.common.base.Charsets;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
@@ -23,7 +24,7 @@ public class MinecraftCodec extends MessageToMessageCodec<ByteBuf, DefinedPacket
ByteBuf buf = ctx.alloc().buffer();
DefinedPacket.writeVarInt( prot.getId( msg.getClass() ), buf );
msg.write( buf );
out.add( buf );
}
@@ -31,15 +32,20 @@ public class MinecraftCodec extends MessageToMessageCodec<ByteBuf, DefinedPacket
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception
{
Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT;
ByteBuf copy = msg.copy();
int packetId = DefinedPacket.readVarInt( msg );
ByteBuf copy = msg.copy();
DefinedPacket packet = null;
if ( prot.hasPacket( packetId ) )
{
packet = prot.createPacket( packetId );
packet.read( msg );
if ( msg.readableBytes() != 0 )
{
System.out.println( msg.toString( Charsets.UTF_8 ) );
throw new BadPacketException( "Did not read all bytes from packet " + packetId + " Protocol " + protocol + " Direction " + prot );
}
}
out.add( new PacketWrapper( packet, copy ) );

View File

@@ -3,7 +3,6 @@ package net.md_5.bungee.protocol;
import io.netty.buffer.ByteBuf;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import net.md_5.bungee.protocol.DefinedPacket;
@RequiredArgsConstructor
public class PacketWrapper

View File

@@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;
import java.lang.reflect.Constructor;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.protocol.packet.Chat;
import net.md_5.bungee.protocol.packet.ClientSettings;
import net.md_5.bungee.protocol.packet.EncryptionRequest;
@@ -92,12 +93,14 @@ public enum Protocol
public static final int PROTOCOL_VERSION = 0x00;
public static final String MINECRAFT_VERSION = "13w41a";
/*========================================================================*/
public final ProtocolDirection TO_SERVER = new ProtocolDirection();
public final ProtocolDirection TO_CLIENT = new ProtocolDirection();
public final ProtocolDirection TO_SERVER = new ProtocolDirection( "TO_SERVER" );
public final ProtocolDirection TO_CLIENT = new ProtocolDirection( "TO_CLIENT" );
@RequiredArgsConstructor
public class ProtocolDirection
{
private final String name;
private final TObjectIntMap<Class<? extends DefinedPacket>> packetMap = new TObjectIntHashMap<>( MAX_PACKET_ID );
private final Class<? extends DefinedPacket>[] packetClasses = new Class[ MAX_PACKET_ID ];
private final Constructor<? extends DefinedPacket>[] packetConstructors = new Constructor[ MAX_PACKET_ID ];
@@ -107,6 +110,12 @@ public enum Protocol
return id < MAX_PACKET_ID && packetConstructors[id] != null;
}
@Override
public String toString()
{
return name;
}
public final DefinedPacket createPacket(int id)
{
if ( id > MAX_PACKET_ID )

View File

@@ -15,18 +15,21 @@ import net.md_5.bungee.protocol.AbstractPacketHandler;
public class LoginSuccess extends DefinedPacket
{
private String data;
private String uuid;
private String username;
@Override
public void read(ByteBuf buf)
{
data = readString( buf );
uuid = readString( buf );
username = readString( buf );
}
@Override
public void write(ByteBuf buf)
{
writeString( data, buf );
writeString( uuid, buf );
writeString( username, buf );
}
@Override