Refactor packet writes into their own channel handler class.

This commit is contained in:
md_5 2013-04-27 18:29:12 +10:00
parent fa9dd7e27f
commit 6b21fdaaea
10 changed files with 60 additions and 43 deletions

View File

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

View File

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

View File

@ -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<PacketFAPluginMessage> loginMessages;
@Getter
@ -69,7 +70,7 @@ public final class UserConnection implements ProxiedPlayer
public final Scoreboard serverSentScoreboard = new Scoreboard();
public final Set<ServerInfo> pendingConnects = new HashSet<>();
public UserConnection(BungeeCord bungee, Channel channel, PendingConnection pendingConnection, Packet2Handshake handshake, Packet1Login forgeLogin, List<PacketFAPluginMessage> loginMessages)
public UserConnection(BungeeCord bungee, ChannelWrapper channel, PendingConnection pendingConnection, Packet2Handshake handshake, Packet1Login forgeLogin, List<PacketFAPluginMessage> 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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -19,6 +19,7 @@ import net.md_5.bungee.packet.PacketHandler;
public class HandlerBoss extends ChannelInboundMessageHandlerAdapter<byte[]>
{
private ChannelWrapper channel;
private PacketHandler handler;
public void setHandler(PacketHandler handler)
@ -32,7 +33,8 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter<byte[]>
{
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<byte[]>
if ( handler != null )
{
ProxyServer.getInstance().getLogger().log( Level.INFO, "{0} has disconnected", handler );
handler.disconnected( ctx.channel() );
handler.disconnected( channel );
}
}

View File

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