@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.

This commit is contained in:
md_5 2013-03-13 20:08:15 +11:00
parent bc0a076e4b
commit b0820208e6
8 changed files with 47 additions and 11 deletions

View File

@ -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.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.event.PluginMessageEvent; 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.Packet0KeepAlive;
import net.md_5.bungee.packet.Packet3Chat; import net.md_5.bungee.packet.Packet3Chat;
import net.md_5.bungee.packet.PacketC9PlayerListItem; import net.md_5.bungee.packet.PacketC9PlayerListItem;
@ -50,9 +51,9 @@ public class DownstreamBridge extends PacketHandler
} }
@Override @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 ); con.ch.write( buf );
} }

View File

@ -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.ChatEvent;
import net.md_5.bungee.api.event.PlayerDisconnectEvent; import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PluginMessageEvent; 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.Packet0KeepAlive;
import net.md_5.bungee.packet.Packet3Chat; import net.md_5.bungee.packet.Packet3Chat;
import net.md_5.bungee.packet.PacketFAPluginMessage; import net.md_5.bungee.packet.PacketFAPluginMessage;
@ -44,9 +45,9 @@ public class UpstreamBridge extends PacketHandler
} }
@Override @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 ) if ( con.getServer() != null )
{ {
con.getServer().getCh().write( buf ); con.getServer().getCh().write( buf );

View File

@ -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<Wrapper>
{
@Override
protected void encode(ChannelHandlerContext ctx, Wrapper msg, ByteBuf out) throws Exception
{
out.capacity( msg.buf.readableBytes() );
out.writeBytes( msg.buf );
msg.buf.release();
}
}

View File

@ -16,7 +16,7 @@ import net.md_5.bungee.packet.PacketHandler;
* channels to maintain simple states, and only call the required, adapted * channels to maintain simple states, and only call the required, adapted
* methods when the channel is connected. * methods when the channel is connected.
*/ */
public class HandlerBoss extends ChannelInboundMessageHandlerAdapter<ByteBuf> public class HandlerBoss extends ChannelInboundMessageHandlerAdapter<Wrapper>
{ {
private PacketHandler handler; private PacketHandler handler;
@ -48,11 +48,11 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter<ByteBuf>
} }
@Override @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() ) if ( handler != null && ctx.channel().isActive() )
{ {
DefinedPacket packet = DefinedPacket.packet( msg ); DefinedPacket packet = DefinedPacket.packet( msg.buf );
boolean sendPacket = true; boolean sendPacket = true;
if ( packet != null ) if ( packet != null )
{ {

View File

@ -17,7 +17,7 @@ import net.md_5.bungee.protocol.netty.PacketReader;
* when all needed data is present. * when all needed data is present.
*/ */
@AllArgsConstructor @AllArgsConstructor
public class PacketDecoder extends ReplayingDecoder<ByteBuf> public class PacketDecoder extends ReplayingDecoder<Void>
{ {
@Getter @Getter
@ -25,10 +25,11 @@ public class PacketDecoder extends ReplayingDecoder<ByteBuf>
private int protocol; private int protocol;
@Override @Override
protected ByteBuf decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception protected Wrapper decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception
{ {
int startIndex = in.readerIndex(); int startIndex = in.readerIndex();
PacketReader.readPacket( in, protocol ); PacketReader.readPacket( in, protocol );
return in.copy( startIndex, in.readerIndex() - startIndex ); ByteBuf ret = in.copy( startIndex, in.readerIndex() - startIndex );
return new Wrapper( ret );
} }
} }

View File

@ -58,6 +58,7 @@ public class PipelineUtils
} }
ch.pipeline().addLast( "timer", new ReadTimeoutHandler( BungeeCord.getInstance().config.getTimeout(), TimeUnit.MILLISECONDS ) ); ch.pipeline().addLast( "timer", new ReadTimeoutHandler( BungeeCord.getInstance().config.getTimeout(), TimeUnit.MILLISECONDS ) );
ch.pipeline().addLast( "decoder", new PacketDecoder( PacketDefinitions.VANILLA_PROTOCOL ) ); ch.pipeline().addLast( "decoder", new PacketDecoder( PacketDefinitions.VANILLA_PROTOCOL ) );
ch.pipeline().addLast( "encoder", new HackEncoder() );
ch.pipeline().addLast( "handler", new HandlerBoss() ); ch.pipeline().addLast( "handler", new HandlerBoss() );
} }
}; };

View File

@ -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;
}

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.packet;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import net.md_5.bungee.netty.Wrapper;
public abstract class PacketHandler 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
{ {
} }