Compiles yet again

This commit is contained in:
md_5 2013-10-11 20:34:21 +11:00
parent d900417d95
commit 7121c20338
9 changed files with 206 additions and 97 deletions

View File

@ -4,7 +4,6 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.protocol.packet.Packet2Handshake;
import net.md_5.bungee.api.plugin.Event; import net.md_5.bungee.api.plugin.Event;
import net.md_5.bungee.protocol.packet.Handshake; import net.md_5.bungee.protocol.packet.Handshake;

View File

@ -5,31 +5,38 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec; import io.netty.handler.codec.MessageToMessageCodec;
import java.util.List; import java.util.List;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Setter;
@AllArgsConstructor @AllArgsConstructor
public class MinecraftCodec extends MessageToMessageCodec<ByteBuf, DefinedPacket> public class MinecraftCodec extends MessageToMessageCodec<ByteBuf, DefinedPacket>
{ {
@Setter
private Protocol protocol; private Protocol protocol;
private boolean server;
@Override @Override
protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, List<Object> out) throws Exception protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, List<Object> out) throws Exception
{ {
Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_CLIENT : protocol.TO_SERVER;
ByteBuf buf = ctx.alloc().buffer(); ByteBuf buf = ctx.alloc().buffer();
DefinedPacket.writeVarInt( protocol.getId( msg.getClass() ), buf ); DefinedPacket.writeVarInt( prot.getId( msg.getClass() ), buf );
msg.write( buf ); msg.write( buf );
} }
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception
{ {
Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT;
int packetId = DefinedPacket.readVarInt( msg ); int packetId = DefinedPacket.readVarInt( msg );
ByteBuf copy = msg.copy(); ByteBuf copy = msg.copy();
DefinedPacket packet = null; DefinedPacket packet = null;
if ( protocol.hasPacket( packetId ) ) if ( prot.hasPacket( packetId ) )
{ {
packet = protocol.createPacket( packetId ); packet = prot.createPacket( packetId );
packet.read( msg ); packet.read( msg );
} }

View File

@ -26,74 +26,56 @@ public enum Protocol
{ {
// Undef // Undef
SERVER_HANDSHAKE HANDSHAKE
{ {
{ {
registerPacket( 0x00, Handshake.class ); TO_SERVER.registerPacket( 0x00, Handshake.class );
} }
}, },
// 0 // 0
CLIENT_GAME GAME
{ {
{ {
registerPacket( 0x00, KeepAlive.class ); TO_CLIENT.registerPacket( 0x00, KeepAlive.class );
registerPacket( 0x01, Login.class ); TO_CLIENT.registerPacket( 0x01, Login.class );
registerPacket( 0x02, Chat.class ); TO_CLIENT.registerPacket( 0x02, Chat.class );
registerPacket( 0x07, Respawn.class ); TO_CLIENT.registerPacket( 0x07, Respawn.class );
registerPacket( 0x3B, PlayerListItem.class ); TO_CLIENT.registerPacket( 0x3B, PlayerListItem.class );
registerPacket( 0x3D, TabComplete.class ); TO_CLIENT.registerPacket( 0x3D, TabComplete.class );
registerPacket( 0x3E, ScoreboardObjective.class ); TO_CLIENT.registerPacket( 0x3E, ScoreboardObjective.class );
registerPacket( 0x3F, ScoreboardScore.class ); TO_CLIENT.registerPacket( 0x3F, ScoreboardScore.class );
registerPacket( 0x40, ScoreboardDisplay.class ); TO_CLIENT.registerPacket( 0x40, ScoreboardDisplay.class );
registerPacket( 0x41, Team.class ); TO_CLIENT.registerPacket( 0x41, Team.class );
registerPacket( 0x42, PluginMessage.class ); TO_CLIENT.registerPacket( 0x42, PluginMessage.class );
registerPacket( 0x43, Kick.class ); TO_CLIENT.registerPacket( 0x43, Kick.class );
}
},
// 0 TO_SERVER.registerPacket( 0x00, KeepAlive.class );
SERVER_GAME TO_SERVER.registerPacket( 0x14, TabComplete.class );
{ TO_SERVER.registerPacket( 0x15, ClientSettings.class );
TO_SERVER.registerPacket( 0x17, PluginMessage.class );
{
registerPacket( 0x00, KeepAlive.class );
registerPacket( 0x14, TabComplete.class );
registerPacket( 0x15, ClientSettings.class );
registerPacket( 0x17, PluginMessage.class );
} }
}, },
// 1 // 1
CLIENT_STATUS STATUS
{ {
{ {
} }
}, },
// 1 //2
SERVER_STATUS LOGIN
{ {
{ {
} TO_CLIENT.registerPacket( 0x00, Kick.class );
}, TO_CLIENT.registerPacket( 0x01, EncryptionRequest.class );
// 2 TO_CLIENT.registerPacket( 0x02, LoginSuccess.class );
CLIENT_LOGIN
{ TO_SERVER.registerPacket( 0x00, LoginRequest.class );
TO_SERVER.registerPacket( 0x01, EncryptionResponse.class );
{
registerPacket( 0x00, Kick.class );
registerPacket( 0x01, EncryptionRequest.class );
registerPacket( 0x02, LoginSuccess.class );
}
},
// 2
SERVER_LOGIN
{
{
registerPacket( 0x00, LoginRequest.class );
registerPacket( 0x01, EncryptionResponse.class );
} }
}; };
/*========================================================================*/ /*========================================================================*/
@ -101,57 +83,64 @@ public enum Protocol
public static final int PROTOCOL_VERSION = 0x00; public static final int PROTOCOL_VERSION = 0x00;
public static final String MINECRAFT_VERSION = "13w41a"; public static final String MINECRAFT_VERSION = "13w41a";
/*========================================================================*/ /*========================================================================*/
private final TObjectIntMap<Class<? extends DefinedPacket>> packetMap = new TObjectIntHashMap<>( MAX_PACKET_ID ); public final ProtocolDirection TO_SERVER = new ProtocolDirection();
private final Class<? extends DefinedPacket>[] packetClasses = new Class[ MAX_PACKET_ID ]; public final ProtocolDirection TO_CLIENT = new ProtocolDirection();
private final Constructor<? extends DefinedPacket>[] packetConstructors = new Constructor[ MAX_PACKET_ID ];
public boolean hasPacket(int id) public class ProtocolDirection
{ {
return id < MAX_PACKET_ID && packetConstructors[id] != null;
}
public final DefinedPacket createPacket(int id) private final TObjectIntMap<Class<? extends DefinedPacket>> packetMap = new TObjectIntHashMap<>( MAX_PACKET_ID );
{ private final Class<? extends DefinedPacket>[] packetClasses = new Class[ MAX_PACKET_ID ];
if ( id > MAX_PACKET_ID ) private final Constructor<? extends DefinedPacket>[] packetConstructors = new Constructor[ MAX_PACKET_ID ];
public boolean hasPacket(int id)
{ {
throw new BadPacketException( "Packet with id " + id + " outside of range " ); return id < MAX_PACKET_ID && packetConstructors[id] != null;
}
if ( packetConstructors[id] == null )
{
throw new BadPacketException( "No packet with id " + id );
} }
try public final DefinedPacket createPacket(int id)
{ {
return packetClasses[id].newInstance(); if ( id > MAX_PACKET_ID )
} catch ( ReflectiveOperationException ex ) {
{ throw new BadPacketException( "Packet with id " + id + " outside of range " );
throw new BadPacketException( "Could not construct packet with id " + id, ex ); }
if ( packetConstructors[id] == null )
{
throw new BadPacketException( "No packet with id " + id );
}
try
{
return packetClasses[id].newInstance();
} catch ( ReflectiveOperationException ex )
{
throw new BadPacketException( "Could not construct packet with id " + id, ex );
}
} }
}
protected final void registerPacket(int id, Class<? extends DefinedPacket> packetClass) protected final void registerPacket(int id, Class<? extends DefinedPacket> packetClass)
{
try
{ {
packetConstructors[id] = packetClass.getDeclaredConstructor(); try
} catch ( NoSuchMethodException ex ) {
{ packetConstructors[id] = packetClass.getDeclaredConstructor();
throw new BadPacketException( "No NoArgsConstructor for packet class " + packetClass ); } catch ( NoSuchMethodException ex )
{
throw new BadPacketException( "No NoArgsConstructor for packet class " + packetClass );
}
packetClasses[id] = packetClass;
packetMap.put( packetClass, id );
} }
packetClasses[id] = packetClass;
packetMap.put( packetClass, id );
}
protected final void unregisterPacket(int id) protected final void unregisterPacket(int id)
{ {
packetMap.remove( packetClasses[id] ); packetMap.remove( packetClasses[id] );
packetClasses[id] = null; packetClasses[id] = null;
packetConstructors[id] = null; packetConstructors[id] = null;
} }
final int getId(Class<? extends DefinedPacket> packet) final int getId(Class<? extends DefinedPacket> packet)
{ {
return packetMap.get( packet ); return packetMap.get( packet );
}
} }
} }

View File

@ -0,0 +1,47 @@
package net.md_5.bungee.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.CorruptedFrameException;
import java.util.List;
public class Varint21FrameDecoder extends ByteToMessageDecoder
{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
{
in.markReaderIndex();
final byte[] buf = new byte[ 3 ];
for ( int i = 0; i < buf.length; i++ )
{
if ( !in.isReadable() )
{
in.resetReaderIndex();
return;
}
buf[i] = in.readByte();
if ( buf[i] >= 0 )
{
int length = DefinedPacket.readVarInt( Unpooled.wrappedBuffer( buf ) );
if ( in.readableBytes() < length )
{
in.resetReaderIndex();
return;
} else
{
out.add( in.readBytes( length ) );
return;
}
}
}
throw new CorruptedFrameException( "length wider than 21-bit" );
}
}

View File

@ -0,0 +1,43 @@
package net.md_5.bungee.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.channel.ChannelHandler;
@ChannelHandler.Sharable
public class Varint21LengthFieldPrepender extends MessageToByteEncoder<ByteBuf>
{
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception
{
int bodyLen = msg.readableBytes();
int headerLen = varintSize( bodyLen );
out.ensureWritable( headerLen + bodyLen );
DefinedPacket.writeVarInt( bodyLen, out );
out.writeBytes( msg, msg.readerIndex(), bodyLen );
}
private static int varintSize(int paramInt)
{
if ( ( paramInt & 0xFFFFFF80 ) == 0 )
{
return 1;
}
if ( ( paramInt & 0xFFFFC000 ) == 0 )
{
return 2;
}
if ( ( paramInt & 0xFFE00000 ) == 0 )
{
return 3;
}
if ( ( paramInt & 0xF0000000 ) == 0 )
{
return 4;
}
return 5;
}
}

View File

@ -48,6 +48,7 @@ import net.md_5.bungee.protocol.packet.EncryptionRequest;
import net.md_5.bungee.protocol.packet.Kick; import net.md_5.bungee.protocol.packet.Kick;
import net.md_5.bungee.api.AbstractReconnectHandler; import net.md_5.bungee.api.AbstractReconnectHandler;
import net.md_5.bungee.api.event.PlayerHandshakeEvent; import net.md_5.bungee.api.event.PlayerHandshakeEvent;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.packet.LoginRequest; import net.md_5.bungee.protocol.packet.LoginRequest;
@RequiredArgsConstructor @RequiredArgsConstructor
@ -166,7 +167,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
forced.ping( pingBack ); forced.ping( pingBack );
} else } else
{ {
// pingBack.done( new ServerPing( bungee.getProtocolVersion(), bungee.getGameVersion(), motd, bungee.getOnlineCount(), listener.getMaxPlayers() ), null ); // pingBack.done( new ServerPing( bungee.getProtocolVersion(), bungee.getGameVersion(), motd, bungee.getOnlineCount(), listener.getMaxPlayers() ), null );
} }
} }
/* /*
@ -201,6 +202,19 @@ public class InitialHandler extends PacketHandler implements PendingConnection
disconnect( bungee.getTranslation( "outdated_client" ) ); disconnect( bungee.getTranslation( "outdated_client" ) );
} }
switch ( handshake.getRequestedProtocol() )
{
case 1:
// Ping
ch.setProtocol( Protocol.STATUS );
break;
case 2:
ch.setProtocol( Protocol.LOGIN );
// Login
break;
default:
throw new IllegalArgumentException( "Cannot request protocol " + handshake.getRequestedProtocol() );
}
} }
@Override @Override

View File

@ -6,6 +6,8 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import lombok.Getter; import lombok.Getter;
import net.md_5.bungee.protocol.MinecraftCodec;
import net.md_5.bungee.protocol.Protocol;
public class ChannelWrapper public class ChannelWrapper
{ {
@ -19,6 +21,11 @@ public class ChannelWrapper
this.ch = ctx.channel(); this.ch = ctx.channel();
} }
public void setProtocol(Protocol protocol)
{
ch.pipeline().get( MinecraftCodec.class ).setProtocol( protocol );
}
public synchronized void write(Object packet) public synchronized void write(Object packet)
{ {
if ( !closed ) if ( !closed )

View File

@ -12,6 +12,8 @@ import net.md_5.bungee.connection.CancelSendSignal;
import net.md_5.bungee.connection.InitialHandler; import net.md_5.bungee.connection.InitialHandler;
import net.md_5.bungee.connection.PingHandler; import net.md_5.bungee.connection.PingHandler;
import net.md_5.bungee.protocol.BadPacketException; import net.md_5.bungee.protocol.BadPacketException;
import net.md_5.bungee.protocol.MinecraftCodec;
import net.md_5.bungee.protocol.Protocol;
/** /**
* This class is a primitive wrapper for {@link PacketHandler} instances tied to * This class is a primitive wrapper for {@link PacketHandler} instances tied to

View File

@ -4,7 +4,6 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelException; import io.netty.channel.ChannelException;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -18,6 +17,8 @@ import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ListenerInfo; import net.md_5.bungee.api.config.ListenerInfo;
import net.md_5.bungee.protocol.MinecraftCodec; import net.md_5.bungee.protocol.MinecraftCodec;
import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.Varint21FrameDecoder;
import net.md_5.bungee.protocol.Varint21LengthFieldPrepender;
public class PipelineUtils public class PipelineUtils
{ {
@ -38,6 +39,7 @@ public class PipelineUtils
} }
BASE.initChannel( ch ); BASE.initChannel( ch );
ch.pipeline().addAfter( FRAME_DECODER, PACKET_CODEC, new MinecraftCodec( Protocol.HANDSHAKE, true ) );
ch.pipeline().get( HandlerBoss.class ).setHandler( new InitialHandler( ProxyServer.getInstance(), ch.attr( LISTENER ).get() ) ); ch.pipeline().get( HandlerBoss.class ).setHandler( new InitialHandler( ProxyServer.getInstance(), ch.attr( LISTENER ).get() ) );
} }
}; };
@ -47,12 +49,12 @@ public class PipelineUtils
protected void initChannel(Channel ch) throws Exception protected void initChannel(Channel ch) throws Exception
{ {
BASE.initChannel( ch ); BASE.initChannel( ch );
ch.pipeline().addAfter( FRAME_DECODER, PACKET_CODEC, new MinecraftCodec( Protocol.LOGIN, false) );
ch.pipeline().get( HandlerBoss.class ).setHandler( new ServerConnector( ProxyServer.getInstance(), ch.attr( USER ).get(), ch.attr( TARGET ).get() ) ); ch.pipeline().get( HandlerBoss.class ).setHandler( new ServerConnector( ProxyServer.getInstance(), ch.attr( USER ).get(), ch.attr( TARGET ).get() ) );
} }
}; };
public static final Base BASE = new Base(); public static final Base BASE = new Base();
private static final ProtobufVarint32FrameDecoder frameDecoder = new ProtobufVarint32FrameDecoder(); private static final Varint21LengthFieldPrepender framePrepender = new Varint21LengthFieldPrepender();
private static final ProtobufVarint32FrameDecoder framePrepender = new ProtobufVarint32FrameDecoder();
public static String TIMEOUT_HANDLER = "timeout"; public static String TIMEOUT_HANDLER = "timeout";
public static String PACKET_CODEC = "packet-codec"; public static String PACKET_CODEC = "packet-codec";
public static String BOSS_HANDLER = "inbound-boss"; public static String BOSS_HANDLER = "inbound-boss";
@ -76,8 +78,7 @@ public class PipelineUtils
} }
ch.pipeline().addLast( TIMEOUT_HANDLER, new ReadTimeoutHandler( BungeeCord.getInstance().config.getTimeout(), TimeUnit.MILLISECONDS ) ); ch.pipeline().addLast( TIMEOUT_HANDLER, new ReadTimeoutHandler( BungeeCord.getInstance().config.getTimeout(), TimeUnit.MILLISECONDS ) );
ch.pipeline().addLast( FRAME_DECODER, frameDecoder ); ch.pipeline().addLast( FRAME_DECODER, new Varint21FrameDecoder() );
ch.pipeline().addLast( PACKET_CODEC, new MinecraftCodec( Protocol.SERVER_HANDSHAKE ) );
ch.pipeline().addLast( FRAME_PREPENDER, framePrepender ); ch.pipeline().addLast( FRAME_PREPENDER, framePrepender );
ch.pipeline().addLast( BOSS_HANDLER, new HandlerBoss() ); ch.pipeline().addLast( BOSS_HANDLER, new HandlerBoss() );
} }