Stop hardcoding handler names

This commit is contained in:
themode 2020-11-20 04:36:33 +01:00
parent 153f7215b3
commit e8ddf44c39
3 changed files with 34 additions and 12 deletions

View File

@ -31,6 +31,21 @@ public class NettyServer {
private static final long DEFAULT_CHANNEL_WRITE_LIMIT = 600_000L;
private static final long DEFAULT_CHANNEL_READ_LIMIT = 100_000L;
public static final String TRAFFIC_LIMITER_HANDLER_NAME = "traffic-limiter";
public static final String LEGACY_PING_HANDLER_NAME = "legacy-ping";
public static final String COMPRESSOR_HANDLER_NAME = "compressor";
public static final String FRAMER_HANDLER_NAME = "framer";
public static final String ENCRYPT_HANDLER_NAME = "encrypt";
public static final String DECRYPT_HANDLER_NAME = "decrypt";
public static final String DECODER_HANDLER_NAME = "decoder";
public static final String ENCODER_HANDLER_NAME = "encoder";
public static final String CLIENT_HANDLER_NAME = "handler";
private final EventLoopGroup boss, worker;
private final ServerBootstrap bootstrap;
@ -90,22 +105,22 @@ public class NettyServer {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("traffic-limiter", globalTrafficHandler);
pipeline.addLast(TRAFFIC_LIMITER_HANDLER_NAME, globalTrafficHandler);
// First check should verify if the packet is a legacy ping (from 1.6 version and earlier)
// Removed from the pipeline later in LegacyPingHandler if unnecessary (>1.6)
pipeline.addLast("legacy-ping", new LegacyPingHandler());
pipeline.addLast(LEGACY_PING_HANDLER_NAME, new LegacyPingHandler());
// Adds packetLength at start | Reads framed bytebuf
pipeline.addLast("framer", new PacketFramer(packetProcessor));
pipeline.addLast(FRAMER_HANDLER_NAME, new PacketFramer(packetProcessor));
// Reads bytebuf and creating inbound packet
pipeline.addLast("decoder", new PacketDecoder());
pipeline.addLast(DECODER_HANDLER_NAME, new PacketDecoder());
// Writes packet to bytebuf
pipeline.addLast("encoder", new PacketEncoder());
pipeline.addLast(ENCODER_HANDLER_NAME, new PacketEncoder());
pipeline.addLast("handler", new ClientChannel(packetProcessor));
pipeline.addLast(CLIENT_HANDLER_NAME, new ClientChannel(packetProcessor));
}
});
}

View File

@ -39,7 +39,7 @@ public class LoginStartPacket implements ClientPreplayPacket {
// Compression
final int threshold = MinecraftServer.getCompressionThreshold();
if (threshold > 0) {
nettyPlayerConnection.enableCompression(threshold);
nettyPlayerConnection.startCompression();
}
}

View File

@ -2,11 +2,13 @@ package net.minestom.server.network.player;
import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.PlayerSkin;
import net.minestom.server.extras.mojangAuth.Decrypter;
import net.minestom.server.extras.mojangAuth.Encrypter;
import net.minestom.server.extras.mojangAuth.MojangCrypt;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.netty.NettyServer;
import net.minestom.server.network.netty.codec.PacketCompressor;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.login.SetCompressionPacket;
@ -73,21 +75,26 @@ public class NettyPlayerConnection extends PlayerConnection {
public void setEncryptionKey(@NotNull SecretKey secretKey) {
Check.stateCondition(encrypted, "Encryption is already enabled!");
this.encrypted = true;
channel.pipeline().addBefore("framer", "decrypt", new Decrypter(MojangCrypt.getCipher(2, secretKey)));
channel.pipeline().addBefore("framer", "encrypt", new Encrypter(MojangCrypt.getCipher(1, secretKey)));
channel.pipeline().addBefore(NettyServer.FRAMER_HANDLER_NAME, NettyServer.DECRYPT_HANDLER_NAME,
new Decrypter(MojangCrypt.getCipher(2, secretKey)));
channel.pipeline().addBefore(NettyServer.FRAMER_HANDLER_NAME, NettyServer.ENCRYPT_HANDLER_NAME,
new Encrypter(MojangCrypt.getCipher(1, secretKey)));
}
/**
* Enables compression and add a new codec to the pipeline.
*
* @param threshold the threshold for a packet to be compressible
* @throws IllegalStateException if encryption is already enabled for this connection
*/
public void enableCompression(int threshold) {
public void startCompression() {
Check.stateCondition(compressed, "Compression is already enabled!");
final int threshold = MinecraftServer.getCompressionThreshold();
Check.stateCondition(threshold == 0, "Compression cannot be enabled because the threshold is equal to 0");
this.compressed = true;
sendPacket(new SetCompressionPacket(threshold));
channel.pipeline().addAfter("framer", "compressor", new PacketCompressor(threshold));
channel.pipeline().addAfter(NettyServer.FRAMER_HANDLER_NAME, NettyServer.COMPRESSOR_HANDLER_NAME,
new PacketCompressor(threshold));
}
/**