From 94cc2412e7987157e3b9a91c37f6258048a9e3a2 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 4 Jul 2013 11:11:57 +1000 Subject: [PATCH] Flush pending messages when manipulating pipeline - fixes forge support --- .../main/java/net/md_5/bungee/ServerConnector.java | 4 ++-- .../net/md_5/bungee/connection/InitialHandler.java | 4 ++-- .../java/net/md_5/bungee/netty/ChannelWrapper.java | 11 +++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java index 75c54352..863bbf4d 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -220,7 +220,7 @@ public class ServerConnector extends PacketHandler ch.write( new PacketFCEncryptionResponse( shared, token ) ); Cipher encrypt = EncryptionUtil.getCipher( Cipher.ENCRYPT_MODE, secretkey ); - ch.getHandle().pipeline().addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.ENCRYPT_HANDLER, new CipherEncoder( encrypt ) ); + ch.addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.ENCRYPT_HANDLER, new CipherEncoder( encrypt ) ); thisState = State.ENCRYPT_RESPONSE; } else @@ -235,7 +235,7 @@ public class ServerConnector extends PacketHandler Preconditions.checkState( thisState == State.ENCRYPT_RESPONSE, "Not expecting ENCRYPT_RESPONSE" ); Cipher decrypt = EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, secretkey ); - ch.getHandle().pipeline().addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) ); + ch.addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) ); ch.write( user.getPendingConnection().getForgeLogin() ); 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 f5b99af7..e06c3a98 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 @@ -241,7 +241,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection sharedKey = EncryptionUtil.getSecret( encryptResponse, request ); Cipher decrypt = EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, sharedKey ); - ch.getHandle().pipeline().addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) ); + ch.addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) ); if ( BungeeCord.getInstance().config.isOnlineMode() ) { @@ -319,7 +319,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection try { Cipher encrypt = EncryptionUtil.getCipher( Cipher.ENCRYPT_MODE, sharedKey ); - ch.getHandle().pipeline().addBefore( PipelineUtils.DECRYPT_HANDLER, PipelineUtils.ENCRYPT_HANDLER, new CipherEncoder( encrypt ) ); + ch.addBefore( PipelineUtils.DECRYPT_HANDLER, PipelineUtils.ENCRYPT_HANDLER, new CipherEncoder( encrypt ) ); } catch ( GeneralSecurityException ex ) { disconnect( "Cipher error: " + Util.exception( ex ) ); 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 b6ffdb8e..df8d88ad 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 @@ -1,6 +1,8 @@ package net.md_5.bungee.netty; +import com.google.common.base.Preconditions; import io.netty.channel.Channel; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.MessageList; import lombok.Getter; @@ -53,6 +55,15 @@ public class ChannelWrapper } } + public void addBefore(String baseName, String name, ChannelHandler handler) + { + Preconditions.checkState( ch.eventLoop().inEventLoop(), "cannot add handler outside of event loop" ); + boolean oldFlush = flushNow; + flushNow( true ); + ch.pipeline().addBefore( baseName, name, handler ); + flushNow( oldFlush ); + } + public Channel getHandle() { return ch;