From b0820208e634a62930c012064cb67a88298e2e53 Mon Sep 17 00:00:00 2001 From: md_5 Date: Wed, 13 Mar 2013 20:08:15 +1100 Subject: [PATCH] @normanm @trustin This hack appears to fix all my issues with writing ByteBufs directly to channels. new @lazertester.party(). Others, please note this still isn't even a beta quality build. Due to memory laziness this build will only accept one connection. --- .../bungee/connection/DownstreamBridge.java | 5 +++-- .../md_5/bungee/connection/UpstreamBridge.java | 5 +++-- .../java/net/md_5/bungee/netty/HackEncoder.java | 17 +++++++++++++++++ .../java/net/md_5/bungee/netty/HandlerBoss.java | 6 +++--- .../net/md_5/bungee/netty/PacketDecoder.java | 7 ++++--- .../net/md_5/bungee/netty/PipelineUtils.java | 1 + .../java/net/md_5/bungee/netty/Wrapper.java | 14 ++++++++++++++ .../net/md_5/bungee/packet/PacketHandler.java | 3 ++- 8 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 proxy/src/main/java/net/md_5/bungee/netty/HackEncoder.java create mode 100644 proxy/src/main/java/net/md_5/bungee/netty/Wrapper.java diff --git a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java index 16b8ab54..ce6acde6 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java @@ -15,6 +15,7 @@ import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.netty.Wrapper; import net.md_5.bungee.packet.Packet0KeepAlive; import net.md_5.bungee.packet.Packet3Chat; import net.md_5.bungee.packet.PacketC9PlayerListItem; @@ -50,9 +51,9 @@ public class DownstreamBridge extends PacketHandler } @Override - public void handle(ByteBuf buf) throws Exception + public void handle(Wrapper buf) throws Exception { - EntityMap.rewrite( buf, con.serverEntityId, con.clientEntityId ); + EntityMap.rewrite( buf.buf, con.serverEntityId, con.clientEntityId ); con.ch.write( buf ); } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index 95f0d509..4aab3adb 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -10,6 +10,7 @@ import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.PlayerDisconnectEvent; import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.netty.Wrapper; import net.md_5.bungee.packet.Packet0KeepAlive; import net.md_5.bungee.packet.Packet3Chat; import net.md_5.bungee.packet.PacketFAPluginMessage; @@ -44,9 +45,9 @@ public class UpstreamBridge extends PacketHandler } @Override - public void handle(ByteBuf buf) throws Exception + public void handle(Wrapper buf) throws Exception { - EntityMap.rewrite( buf, con.clientEntityId, con.serverEntityId ); + EntityMap.rewrite( buf.buf, con.clientEntityId, con.serverEntityId ); if ( con.getServer() != null ) { con.getServer().getCh().write( buf ); diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HackEncoder.java b/proxy/src/main/java/net/md_5/bungee/netty/HackEncoder.java new file mode 100644 index 00000000..849c3d5e --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/netty/HackEncoder.java @@ -0,0 +1,17 @@ +package net.md_5.bungee.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +public class HackEncoder extends MessageToByteEncoder +{ + + @Override + protected void encode(ChannelHandlerContext ctx, Wrapper msg, ByteBuf out) throws Exception + { + out.capacity( msg.buf.readableBytes() ); + out.writeBytes( msg.buf ); + msg.buf.release(); + } +} diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index 4e91b396..d19b9117 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -16,7 +16,7 @@ import net.md_5.bungee.packet.PacketHandler; * channels to maintain simple states, and only call the required, adapted * methods when the channel is connected. */ -public class HandlerBoss extends ChannelInboundMessageHandlerAdapter +public class HandlerBoss extends ChannelInboundMessageHandlerAdapter { private PacketHandler handler; @@ -48,11 +48,11 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter } @Override - public void messageReceived(ChannelHandlerContext ctx, ByteBuf msg) throws Exception + public void messageReceived(ChannelHandlerContext ctx, Wrapper msg) throws Exception { if ( handler != null && ctx.channel().isActive() ) { - DefinedPacket packet = DefinedPacket.packet( msg ); + DefinedPacket packet = DefinedPacket.packet( msg.buf ); boolean sendPacket = true; if ( packet != null ) { diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java b/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java index 696f0148..37f2bb2e 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java @@ -17,7 +17,7 @@ import net.md_5.bungee.protocol.netty.PacketReader; * when all needed data is present. */ @AllArgsConstructor -public class PacketDecoder extends ReplayingDecoder +public class PacketDecoder extends ReplayingDecoder { @Getter @@ -25,10 +25,11 @@ public class PacketDecoder extends ReplayingDecoder private int protocol; @Override - protected ByteBuf decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception + protected Wrapper decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { int startIndex = in.readerIndex(); PacketReader.readPacket( in, protocol ); - return in.copy( startIndex, in.readerIndex() - startIndex ); + ByteBuf ret = in.copy( startIndex, in.readerIndex() - startIndex ); + return new Wrapper( ret ); } } 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 5b127292..b8c43148 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 @@ -58,6 +58,7 @@ public class PipelineUtils } ch.pipeline().addLast( "timer", new ReadTimeoutHandler( BungeeCord.getInstance().config.getTimeout(), TimeUnit.MILLISECONDS ) ); ch.pipeline().addLast( "decoder", new PacketDecoder( PacketDefinitions.VANILLA_PROTOCOL ) ); + ch.pipeline().addLast( "encoder", new HackEncoder() ); ch.pipeline().addLast( "handler", new HandlerBoss() ); } }; diff --git a/proxy/src/main/java/net/md_5/bungee/netty/Wrapper.java b/proxy/src/main/java/net/md_5/bungee/netty/Wrapper.java new file mode 100644 index 00000000..9bfa72e8 --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/netty/Wrapper.java @@ -0,0 +1,14 @@ +package net.md_5.bungee.netty; + +import io.netty.buffer.ByteBuf; +import lombok.RequiredArgsConstructor; + +/** + * Hack for Netty. + */ +@RequiredArgsConstructor +public class Wrapper +{ + + public final ByteBuf buf; +} diff --git a/proxy/src/main/java/net/md_5/bungee/packet/PacketHandler.java b/proxy/src/main/java/net/md_5/bungee/packet/PacketHandler.java index f64dfd9a..4a4bd495 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/PacketHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/PacketHandler.java @@ -2,6 +2,7 @@ package net.md_5.bungee.packet; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; +import net.md_5.bungee.netty.Wrapper; public abstract class PacketHandler { @@ -21,7 +22,7 @@ public abstract class PacketHandler { } - public void handle(ByteBuf buf) throws Exception + public void handle(Wrapper buf) throws Exception { }