From b8c9330bd623cc13d0cf9dd54c919b4c1971e569 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 12 Oct 2013 12:01:17 +1100 Subject: [PATCH] Sigh --- .../md_5/bungee/protocol/MinecraftCodec.java | 53 ------------------- .../bungee/protocol/MinecraftDecoder.java | 41 ++++++++++++++ .../bungee/protocol/MinecraftEncoder.java | 24 +++++++++ .../java/net/md_5/bungee/UserConnection.java | 9 ++-- .../bungee/connection/InitialHandler.java | 2 +- .../net/md_5/bungee/netty/ChannelWrapper.java | 6 ++- .../net/md_5/bungee/netty/PipelineUtils.java | 9 ++-- 7 files changed, 80 insertions(+), 64 deletions(-) delete mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/MinecraftCodec.java create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/MinecraftEncoder.java diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftCodec.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftCodec.java deleted file mode 100644 index a8909e6a..00000000 --- a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftCodec.java +++ /dev/null @@ -1,53 +0,0 @@ -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; -import java.util.List; -import lombok.AllArgsConstructor; -import lombok.Setter; - -@AllArgsConstructor -public class MinecraftCodec extends MessageToMessageCodec -{ - - @Setter - private Protocol protocol; - private boolean server; - - @Override - protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, List out) throws Exception - { - Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_CLIENT : protocol.TO_SERVER; - - ByteBuf buf = ctx.alloc().buffer(); - DefinedPacket.writeVarInt( prot.getId( msg.getClass() ), buf ); - msg.write( buf ); - - out.add( buf ); - } - - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception - { - Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT; - ByteBuf copy = msg.copy(); - - int packetId = DefinedPacket.readVarInt( msg ); - - 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 ) ); - } -} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java new file mode 100644 index 00000000..27d9cf40 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java @@ -0,0 +1,41 @@ +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.ByteToMessageDecoder; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Setter; + +@AllArgsConstructor +public class MinecraftDecoder extends ByteToMessageDecoder +{ + + @Setter + private Protocol protocol; + private boolean server; + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception + { + Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT; + ByteBuf copy = in.copy(); + + int packetId = DefinedPacket.readVarInt( in ); + + DefinedPacket packet = null; + if ( prot.hasPacket( packetId ) ) + { + packet = prot.createPacket( packetId ); + packet.read( in ); + if ( in.readableBytes() != 0 ) + { + System.out.println( in.toString( Charsets.UTF_8 ) ); + throw new BadPacketException( "Did not read all bytes from packet " + packet.getClass() + " " + packetId + " Protocol " + protocol + " Direction " + prot ); + } + } + + out.add( new PacketWrapper( packet, copy ) ); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftEncoder.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftEncoder.java new file mode 100644 index 00000000..28689b9f --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftEncoder.java @@ -0,0 +1,24 @@ +package net.md_5.bungee.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; +import lombok.AllArgsConstructor; +import lombok.Setter; + +@AllArgsConstructor +public class MinecraftEncoder extends MessageToByteEncoder +{ + + @Setter + private Protocol protocol; + private boolean server; + + @Override + protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, ByteBuf out) throws Exception + { + Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_CLIENT : protocol.TO_SERVER; + DefinedPacket.writeVarInt( prot.getId( msg.getClass() ), out ); + msg.write( out ); + } +} diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index e6efb419..fa19f25b 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -19,7 +19,6 @@ import lombok.Getter; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.Setter; -import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -32,10 +31,9 @@ import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.HandlerBoss; import net.md_5.bungee.protocol.PacketWrapper; import net.md_5.bungee.netty.PipelineUtils; -import static net.md_5.bungee.netty.PipelineUtils.FRAME_DECODER; -import static net.md_5.bungee.netty.PipelineUtils.PACKET_CODEC; import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.MinecraftCodec; +import net.md_5.bungee.protocol.MinecraftDecoder; +import net.md_5.bungee.protocol.MinecraftEncoder; import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.packet.Chat; import net.md_5.bungee.protocol.packet.ClientSettings; @@ -204,7 +202,8 @@ public final class UserConnection implements ProxiedPlayer protected void initChannel(Channel ch) throws Exception { PipelineUtils.BASE.initChannel( ch ); - ch.pipeline().addAfter( FRAME_DECODER, PACKET_CODEC, new MinecraftCodec( Protocol.HANDSHAKE, false ) ); + ch.pipeline().addAfter( PipelineUtils.FRAME_DECODER, PipelineUtils.PACKET_DECODER, new MinecraftDecoder( Protocol.HANDSHAKE, false ) ); + ch.pipeline().addAfter( PipelineUtils.FRAME_PREPENDER, PipelineUtils.PACKET_ENCODER, new MinecraftEncoder( Protocol.HANDSHAKE, false ) ); ch.pipeline().get( HandlerBoss.class ).setHandler( new ServerConnector( bungee, UserConnection.this, target ) ); } }; diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index 057c5e15..e971e1ac 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -362,7 +362,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection { server = AbstractReconnectHandler.getForcedHost( InitialHandler.this ); } - // userCon.connect( server, true ); + userCon.connect( server, true ); thisState = State.FINISHED; } diff --git a/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java b/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java index 10bb86b1..77ecd687 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java @@ -6,7 +6,8 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import lombok.Getter; -import net.md_5.bungee.protocol.MinecraftCodec; +import net.md_5.bungee.protocol.MinecraftDecoder; +import net.md_5.bungee.protocol.MinecraftEncoder; import net.md_5.bungee.protocol.Protocol; public class ChannelWrapper @@ -23,7 +24,8 @@ public class ChannelWrapper public void setProtocol(Protocol protocol) { - ch.pipeline().get( MinecraftCodec.class ).setProtocol( protocol ); + ch.pipeline().get( MinecraftDecoder.class ).setProtocol( protocol ); + ch.pipeline().get( MinecraftEncoder.class ).setProtocol( protocol ); } public synchronized void write(Object packet) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java index 2295b5d4..af2899a9 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java @@ -14,7 +14,8 @@ import net.md_5.bungee.UserConnection; import net.md_5.bungee.connection.InitialHandler; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ListenerInfo; -import net.md_5.bungee.protocol.MinecraftCodec; +import net.md_5.bungee.protocol.MinecraftDecoder; +import net.md_5.bungee.protocol.MinecraftEncoder; import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.Varint21FrameDecoder; import net.md_5.bungee.protocol.Varint21LengthFieldPrepender; @@ -38,7 +39,8 @@ public class PipelineUtils } BASE.initChannel( ch ); - ch.pipeline().addAfter( FRAME_DECODER, PACKET_CODEC, new MinecraftCodec( Protocol.HANDSHAKE, true ) ); + ch.pipeline().addAfter( FRAME_DECODER, PACKET_DECODER, new MinecraftDecoder( Protocol.HANDSHAKE, true ) ); + ch.pipeline().addAfter( FRAME_PREPENDER, PACKET_ENCODER, new MinecraftEncoder( Protocol.HANDSHAKE, true ) ); System.out.println( ch.pipeline() ); ch.pipeline().get( HandlerBoss.class ).setHandler( new InitialHandler( ProxyServer.getInstance(), ch.attr( LISTENER ).get() ) ); } @@ -46,7 +48,8 @@ public class PipelineUtils public static final Base BASE = new Base(); private static final Varint21LengthFieldPrepender framePrepender = new Varint21LengthFieldPrepender(); public static String TIMEOUT_HANDLER = "timeout"; - public static String PACKET_CODEC = "packet-codec"; + public static String PACKET_DECODER = "packet-decoder"; + public static String PACKET_ENCODER = "packet-encoder"; public static String BOSS_HANDLER = "inbound-boss"; public static String ENCRYPT_HANDLER = "encrypt"; public static String DECRYPT_HANDLER = "decrypt";