From 0ce02251d4780d5197b5451a4649296688ff4a94 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 9 Mar 2013 16:35:59 +1100 Subject: [PATCH] Start work on a little spit and polish, with cleaner exceptions. --- .../java/net/md_5/bungee/BungeeServerInfo.java | 17 ++++++++++++++++- .../java/net/md_5/bungee/ServerConnector.java | 8 +++++++- .../java/net/md_5/bungee/UserConnection.java | 5 +++++ .../bungee/connection/DownstreamBridge.java | 8 ++++++++ .../md_5/bungee/connection/InitialHandler.java | 6 ++++++ .../net/md_5/bungee/connection/PingHandler.java | 8 ++++++++ .../md_5/bungee/connection/UpstreamBridge.java | 6 ++++++ .../java/net/md_5/bungee/netty/HandlerBoss.java | 13 ++++++++++++- .../net/md_5/bungee/packet/PacketHandler.java | 3 +++ .../net/md_5/bungee/tablist/GlobalTabList.java | 2 +- .../bungee/tablist/ServerUniqueTabList.java | 2 +- 11 files changed, 73 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java index 5cd5ca33..8f5ca6bb 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java @@ -1,6 +1,9 @@ package net.md_5.bungee; import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelOption; import io.netty.channel.socket.nio.NioSocketChannel; import java.net.InetSocketAddress; import java.util.Queue; @@ -48,8 +51,20 @@ public class BungeeServerInfo extends ServerInfo .channel( NioSocketChannel.class ) .group( BungeeCord.getInstance().eventLoops ) .handler( PipelineUtils.BASE ) + .option( ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000 ) // TODO: Configurable .remoteAddress( getAddress() ) .connect() - .channel().pipeline().get( HandlerBoss.class ).setHandler( new PingHandler( callback ) ); + .addListener( new ChannelFutureListener() + { + @Override + public void operationComplete(ChannelFuture future) throws Exception + { + if ( !future.isSuccess() ) + { + callback.done( null, future.cause() ); + } + } + } ) + .channel().pipeline().get( HandlerBoss.class ).setHandler( new PingHandler( this, callback ) ); } } 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 5b47bf0b..1b1594fd 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -99,7 +99,7 @@ public class ServerConnector extends PacketHandler target.addPlayer( user ); user.setServer( server ); - ch.pipeline().get( HandlerBoss.class ).setHandler( new DownstreamBridge( bungee, user ) ); + ch.pipeline().get( HandlerBoss.class ).setHandler( new DownstreamBridge( bungee, user, server ) ); } thisState = State.FINISHED; @@ -119,4 +119,10 @@ public class ServerConnector extends PacketHandler { throw new KickException( kick.message ); } + + @Override + public String toString() + { + return "[" + user.getName() + "] <-> ServerConnector [" + target.getName() + "]"; + } } 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 28c1b7b6..58288ef5 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -7,6 +7,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; import io.netty.channel.socket.nio.NioSocketChannel; import java.net.InetSocketAddress; import java.util.Collection; @@ -114,6 +115,7 @@ public final class UserConnection implements ProxiedPlayer ch.pipeline().get( HandlerBoss.class ).setHandler( new ServerConnector( bungee, UserConnection.this, target ) ); } } ) + .option( ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000 ) // TODO: Configurable .remoteAddress( target.getAddress() ) .connect().addListener( new ChannelFutureListener() { @@ -128,6 +130,9 @@ public final class UserConnection implements ProxiedPlayer { sendMessage( ChatColor.RED + "Could not connect to target server, you have been moved to the default server" ); connect( def, false ); + } else + { + disconnect( "Server down, could not connect to default!" ); } } } 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 1922e444..aa9a7ee4 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 @@ -11,6 +11,7 @@ import net.md_5.bungee.Util; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.packet.Packet0KeepAlive; @@ -26,6 +27,7 @@ public class DownstreamBridge extends PacketHandler private final ProxyServer bungee; private final UserConnection con; + private final Server server; @Override public void handle(ByteBuf buf) throws Exception @@ -196,4 +198,10 @@ public class DownstreamBridge extends PacketHandler con.disconnect( "[Kicked] " + kick.message ); throw new CancelSendSignal(); } + + @Override + public String toString() + { + return "[" + con.getName() + "] <-> DownstreamBridge <-> [" + server.getInfo().getName() + "]"; + } } 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 c4b76fb4..8ed41ee2 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 @@ -240,4 +240,10 @@ public class InitialHandler extends PacketHandler implements PendingConnection { return (InetSocketAddress) ch.remoteAddress(); } + + @Override + public String toString() + { + return "[" + ( ( getName() != null ) ? getName() : getAddress() ) + "] <-> InitialHandler"; + } } 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 ff4af4d0..3ad97380 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 @@ -6,6 +6,7 @@ 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.packet.PacketFFKick; import net.md_5.bungee.packet.PacketHandler; @@ -13,6 +14,7 @@ import net.md_5.bungee.packet.PacketHandler; public class PingHandler extends PacketHandler { + private final ServerInfo target; private final Callback callback; private static final ByteBuf pingBuf = Unpooled.wrappedBuffer( new byte[] { @@ -38,4 +40,10 @@ public class PingHandler extends PacketHandler ServerPing ping = new ServerPing( Byte.parseByte( split[1] ), split[2], split[3], Integer.parseInt( split[4] ), Integer.parseInt( split[5] ) ); callback.done( ping, null ); } + + @Override + public String toString() + { + return "[Ping Handler] -> " + target.getName(); + } } 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 6a807df7..f099ce46 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 @@ -75,4 +75,10 @@ public class UpstreamBridge extends PacketHandler throw new CancelSendSignal(); } } + + @Override + public String toString() + { + return "[" + con.getName() + "] -> UpstreamBridge"; + } } 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 9e97f9e2..01c031cd 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 @@ -4,6 +4,9 @@ import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandlerAdapter; +import io.netty.handler.timeout.ReadTimeoutException; +import java.util.logging.Level; +import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.connection.CancelSendSignal; import net.md_5.bungee.packet.DefinedPacket; import net.md_5.bungee.packet.PacketHandler; @@ -30,6 +33,7 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter if ( handler != null ) { handler.connected( ctx.channel() ); + ProxyServer.getInstance().getLogger().log( Level.INFO, "{0} has connected", handler ); } } @@ -38,6 +42,7 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter { if ( handler != null ) { + ProxyServer.getInstance().getLogger().log( Level.INFO, "{0} has disconnected", handler ); handler.disconnected( ctx.channel() ); } } @@ -69,9 +74,15 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - cause.printStackTrace(); if ( ctx.channel().isActive() ) { + if ( cause instanceof ReadTimeoutException ) + { + ProxyServer.getInstance().getLogger().log( Level.WARNING, handler + " - read timed out" ); + } else + { + ProxyServer.getInstance().getLogger().log( Level.SEVERE, handler + " - encountered exception", cause ); + } ctx.close(); } } 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 f487f641..f64dfd9a 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 @@ -6,6 +6,9 @@ import io.netty.channel.Channel; public abstract class PacketHandler { + @Override + public abstract String toString(); + public void connected(Channel channel) throws Exception { } diff --git a/proxy/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java b/proxy/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java index eb8ae0e9..576f8874 100644 --- a/proxy/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java +++ b/proxy/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java @@ -22,7 +22,7 @@ public class GlobalTabList implements TabListHandler UserConnection con = (UserConnection) player; for ( ProxiedPlayer p : ProxyServer.getInstance().getPlayers() ) { - con.packetQueue.add( new PacketC9PlayerListItem( p.getDisplayName(), true, p.getPing() ) ); + con.sendPacket(new PacketC9PlayerListItem( p.getDisplayName(), true, p.getPing() ) ); } BungeeCord.getInstance().broadcast( new PacketC9PlayerListItem( player.getDisplayName(), true, player.getPing() ) ); } diff --git a/proxy/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java b/proxy/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java index 091d5cad..3c17aab5 100644 --- a/proxy/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java +++ b/proxy/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java @@ -40,7 +40,7 @@ public class ServerUniqueTabList implements TabListHandler { for ( String username : usernames ) { - ( (UserConnection) player ).packetQueue.add( new PacketC9PlayerListItem( username, false, 9999 ) ); + ( (UserConnection) player ).sendPacket(new PacketC9PlayerListItem( username, false, 9999 ) ); } usernames.clear(); }