From 6b21fdaaeab3bdccd1ac63f425db0fbbf5ed3473 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 27 Apr 2013 18:29:12 +1000 Subject: [PATCH] Refactor packet writes into their own channel handler class. --- .../net/md_5/bungee/ServerConnection.java | 14 +++++------- .../java/net/md_5/bungee/ServerConnector.java | 11 +++++----- .../java/net/md_5/bungee/UserConnection.java | 11 +++++----- .../bungee/connection/DownstreamBridge.java | 3 ++- .../bungee/connection/InitialHandler.java | 22 ++++++++----------- .../md_5/bungee/connection/PingHandler.java | 4 ++-- .../bungee/connection/UpstreamBridge.java | 5 +++-- .../net/md_5/bungee/netty/ChannelWrapper.java | 21 ++++++++++++++++++ .../net/md_5/bungee/netty/HandlerBoss.java | 6 +++-- .../net/md_5/bungee/packet/PacketHandler.java | 6 ++--- 10 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java index 815762ef..e8ca2f4d 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java @@ -7,6 +7,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; import net.md_5.bungee.api.connection.Server; +import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.packet.Packet1Login; import net.md_5.bungee.packet.PacketFAPluginMessage; import net.md_5.bungee.packet.PacketFFKick; @@ -16,7 +17,7 @@ public class ServerConnection implements Server { @Getter - private final Channel ch; + private final ChannelWrapper ch; @Getter private final BungeeServerInfo info; @Getter @@ -34,20 +35,15 @@ public class ServerConnection implements Server @Override public synchronized void disconnect(String reason) { - disconnect( ch, reason ); - } - - static void disconnect(final Channel ch, String reason) - { - if ( ch.isActive() ) + if ( ch.getHandle().isActive() ) { ch.write( new PacketFFKick( reason ) ); - ch.eventLoop().schedule( new Runnable() + ch.getHandle().eventLoop().schedule( new Runnable() { @Override public void run() { - ch.close(); + ch.getHandle().close(); } }, 100, TimeUnit.MILLISECONDS ); } 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 7242281f..15221db9 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -3,14 +3,12 @@ package net.md_5.bungee; import com.google.common.base.Preconditions; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; -import io.netty.channel.Channel; import java.util.Objects; import java.util.Queue; import lombok.RequiredArgsConstructor; 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.config.TexturePackInfo; import net.md_5.bungee.api.event.ServerConnectedEvent; import net.md_5.bungee.api.event.ServerKickEvent; import net.md_5.bungee.api.scoreboard.Objective; @@ -18,6 +16,7 @@ import net.md_5.bungee.api.scoreboard.Team; import net.md_5.bungee.connection.CancelSendSignal; import net.md_5.bungee.connection.DownstreamBridge; import net.md_5.bungee.netty.HandlerBoss; +import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.packet.DefinedPacket; import net.md_5.bungee.packet.Packet1Login; import net.md_5.bungee.packet.Packet9Respawn; @@ -34,7 +33,7 @@ public class ServerConnector extends PacketHandler { private final ProxyServer bungee; - private Channel ch; + private ChannelWrapper ch; private final UserConnection user; private final BungeeServerInfo target; private State thisState = State.ENCRYPT_REQUEST; @@ -46,7 +45,7 @@ public class ServerConnector extends PacketHandler } @Override - public void connected(Channel channel) throws Exception + public void connected(ChannelWrapper channel) throws Exception { this.ch = channel; @@ -125,7 +124,7 @@ public class ServerConnector extends PacketHandler } // TODO: Fix this? - if ( !user.ch.isActive() ) + if ( !user.ch.getHandle().isActive() ) { server.disconnect( "Quitting" ); // Silly server admins see stack trace and die @@ -139,7 +138,7 @@ public class ServerConnector extends PacketHandler user.pendingConnects.remove( target ); user.setServer( server ); - ch.pipeline().get( HandlerBoss.class ).setHandler( new DownstreamBridge( bungee, user, server ) ); + ch.getHandle().pipeline().get( HandlerBoss.class ).setHandler( new DownstreamBridge( bungee, user, server ) ); } thisState = State.FINISHED; 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 d5b3f0a2..a3c7948d 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -30,6 +30,7 @@ import net.md_5.bungee.api.event.PermissionCheckEvent; import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.scoreboard.Scoreboard; import net.md_5.bungee.netty.HandlerBoss; +import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.packet.*; @@ -38,7 +39,7 @@ public final class UserConnection implements ProxiedPlayer public final Packet2Handshake handshake; private final ProxyServer bungee; - public final Channel ch; + public final ChannelWrapper ch; final Packet1Login forgeLogin; final List loginMessages; @Getter @@ -69,7 +70,7 @@ public final class UserConnection implements ProxiedPlayer public final Scoreboard serverSentScoreboard = new Scoreboard(); public final Set pendingConnects = new HashSet<>(); - public UserConnection(BungeeCord bungee, Channel channel, PendingConnection pendingConnection, Packet2Handshake handshake, Packet1Login forgeLogin, List loginMessages) + public UserConnection(BungeeCord bungee, ChannelWrapper channel, PendingConnection pendingConnection, Packet2Handshake handshake, Packet1Login forgeLogin, List loginMessages) { this.bungee = bungee; this.ch = channel; @@ -182,11 +183,11 @@ public final class UserConnection implements ProxiedPlayer @Override public synchronized void disconnect(String reason) { - if ( ch.isActive() ) + if ( ch.getHandle().isActive() ) { bungee.getLogger().log( Level.INFO, "[" + getName() + "] disconnected with: " + reason ); ch.write( new PacketFFKick( reason ) ); - ch.close(); + ch.getHandle().close(); if ( server != null ) { server.disconnect( "Quitting" ); @@ -225,7 +226,7 @@ public final class UserConnection implements ProxiedPlayer @Override public InetSocketAddress getAddress() { - return (InetSocketAddress) ch.remoteAddress(); + return (InetSocketAddress) ch.getHandle().remoteAddress(); } @Override 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 15430dde..86be51ef 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 @@ -21,6 +21,7 @@ import net.md_5.bungee.api.scoreboard.Objective; import net.md_5.bungee.api.scoreboard.Position; import net.md_5.bungee.api.scoreboard.Score; import net.md_5.bungee.api.scoreboard.Team; +import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.packet.Packet0KeepAlive; import net.md_5.bungee.packet.Packet3Chat; import net.md_5.bungee.packet.PacketC9PlayerListItem; @@ -55,7 +56,7 @@ public class DownstreamBridge extends PacketHandler } @Override - public void disconnected(Channel channel) throws Exception + public void disconnected(ChannelWrapper channel) throws Exception { // We lost connection to the server server.getInfo().removePlayer( con ); 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 1e39b033..55e75a10 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 @@ -3,13 +3,8 @@ package net.md_5.bungee.connection; import com.google.common.base.Preconditions; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.Response; -import io.netty.channel.Channel; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; import java.math.BigInteger; import java.net.InetSocketAddress; -import java.net.URL; import java.net.URLEncoder; import java.security.GeneralSecurityException; import java.security.MessageDigest; @@ -36,6 +31,7 @@ import net.md_5.bungee.api.event.PostLoginEvent; import net.md_5.bungee.api.event.ProxyPingEvent; import net.md_5.bungee.netty.CipherCodec; import net.md_5.bungee.netty.HandlerBoss; +import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.PacketDecoder; import net.md_5.bungee.packet.Packet1Login; import net.md_5.bungee.packet.Packet2Handshake; @@ -53,7 +49,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection { private final ProxyServer bungee; - private Channel ch; + private ChannelWrapper ch; @Getter private final ListenerInfo listener; private Packet1Login forgeLogin; // TODO: Remove for now? @@ -71,7 +67,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection } @Override - public void connected(Channel channel) throws Exception + public void connected(ChannelWrapper channel) throws Exception { this.ch = channel; } @@ -89,7 +85,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection Preconditions.checkState( forgeLogin == null, "Already received FORGE LOGIN" ); forgeLogin = login; - ch.pipeline().get( PacketDecoder.class ).setProtocol( PacketDefinitions.FORGE_PROTOCOL ); + ch.getHandle().pipeline().get( PacketDecoder.class ).setProtocol( PacketDefinitions.FORGE_PROTOCOL ); } @Override @@ -205,7 +201,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection Cipher encrypt = EncryptionUtil.getCipher( Cipher.ENCRYPT_MODE, sharedKey ); Cipher decrypt = EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, sharedKey ); ch.write( new PacketFCEncryptionResponse() ); - ch.pipeline().addBefore( "decoder", "cipher", new CipherCodec( encrypt, decrypt ) ); + ch.getHandle().pipeline().addBefore( "decoder", "cipher", new CipherCodec( encrypt, decrypt ) ); thisState = InitialHandler.State.LOGIN; } catch ( GeneralSecurityException ex ) { @@ -226,7 +222,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection UserConnection userCon = new UserConnection( (BungeeCord) bungee, ch, this, handshake, forgeLogin, loginMessages ); bungee.getPluginManager().callEvent( new PostLoginEvent( userCon ) ); - ch.pipeline().get( HandlerBoss.class ).setHandler( new UpstreamBridge( bungee, userCon ) ); + ch.getHandle().pipeline().get( HandlerBoss.class ).setHandler( new UpstreamBridge( bungee, userCon ) ); ServerInfo server = bungee.getReconnectHandler().getServer( userCon ); userCon.connect( server, true ); @@ -238,10 +234,10 @@ public class InitialHandler extends PacketHandler implements PendingConnection @Override public synchronized void disconnect(String reason) { - if ( ch.isActive() ) + if ( ch.getHandle().isActive() ) { ch.write( new PacketFFKick( reason ) ); - ch.close(); + ch.getHandle().close(); disconnected = true; } } @@ -267,7 +263,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection @Override public InetSocketAddress getAddress() { - return (InetSocketAddress) ch.remoteAddress(); + return (InetSocketAddress) ch.getHandle().remoteAddress(); } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/connection/PingHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/PingHandler.java index 9f9ba980..61c87d0c 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/PingHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/PingHandler.java @@ -1,10 +1,10 @@ package net.md_5.bungee.connection; -import io.netty.channel.Channel; import lombok.RequiredArgsConstructor; import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.ServerPing; import net.md_5.bungee.api.config.ServerInfo; +import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.packet.PacketFFKick; import net.md_5.bungee.packet.PacketHandler; @@ -20,7 +20,7 @@ public class PingHandler extends PacketHandler }; @Override - public void connected(Channel channel) throws Exception + public void connected(ChannelWrapper channel) throws Exception { channel.write( pingBuf ); } 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 a74b3788..95d597eb 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 @@ -11,6 +11,7 @@ import net.md_5.bungee.api.config.TexturePackInfo; 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.ChannelWrapper; import net.md_5.bungee.packet.Packet0KeepAlive; import net.md_5.bungee.packet.Packet3Chat; import net.md_5.bungee.packet.PacketCCSettings; @@ -31,7 +32,7 @@ public class UpstreamBridge extends PacketHandler } @Override - public void connected(Channel channel) throws Exception + public void connected(ChannelWrapper channel) throws Exception { BungeeCord.getInstance().connections.put( con.getName(), con ); bungee.getTabListHandler().onConnect( con ); @@ -45,7 +46,7 @@ public class UpstreamBridge extends PacketHandler } @Override - public void disconnected(Channel channel) throws Exception + public void disconnected(ChannelWrapper channel) throws Exception { // We lost connection to the client PlayerDisconnectEvent event = new PlayerDisconnectEvent( con ); 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 new file mode 100644 index 00000000..852db26e --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java @@ -0,0 +1,21 @@ +package net.md_5.bungee.netty; + +import io.netty.channel.Channel; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class ChannelWrapper +{ + + private final Channel ch; + + public void write(Object packet) + { + ch.write( packet ); + } + + public Channel getHandle() + { + return ch; + } +} 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 df8b7985..9e311d58 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 @@ -19,6 +19,7 @@ import net.md_5.bungee.packet.PacketHandler; public class HandlerBoss extends ChannelInboundMessageHandlerAdapter { + private ChannelWrapper channel; private PacketHandler handler; public void setHandler(PacketHandler handler) @@ -32,7 +33,8 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter { if ( handler != null ) { - handler.connected( ctx.channel() ); + channel = new ChannelWrapper( ctx.channel() ); + handler.connected( channel ); ProxyServer.getInstance().getLogger().log( Level.INFO, "{0} has connected", handler ); } } @@ -43,7 +45,7 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter if ( handler != null ) { ProxyServer.getInstance().getLogger().log( Level.INFO, "{0} has disconnected", handler ); - handler.disconnected( ctx.channel() ); + handler.disconnected( channel ); } } 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 1e0a543e..d2bc6033 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 @@ -1,6 +1,6 @@ package net.md_5.bungee.packet; -import io.netty.channel.Channel; +import net.md_5.bungee.netty.ChannelWrapper; public abstract class PacketHandler { @@ -8,11 +8,11 @@ public abstract class PacketHandler @Override public abstract String toString(); - public void connected(Channel channel) throws Exception + public void connected(ChannelWrapper channel) throws Exception { } - public void disconnected(Channel channel) throws Exception + public void disconnected(ChannelWrapper channel) throws Exception { }