Improved exception handling.

This commit is contained in:
retrooper committed 2022-07-14 10:51:06 +02:00
1 parent b090424510
commit bc356c6daf
5 files changed
+40 -18

No files matched your search

@@ -127,7 +127,9 @@ public class InternalPacketListener extends PacketListenerAbstract {
WrapperHandshakingClientHandshake handshake = new WrapperHandshakingClientHandshake(event);
ConnectionState nextState = handshake.getNextConnectionState();
ClientVersion clientVersion = handshake.getClientVersion();
if (clientVersion == ClientVersion.UNKNOWN) {
return;
}
//Update client version for this event call(and user)
user.setClientVersion(clientVersion);
PacketEvents.getAPI().getLogManager().debug("Processed " + address.getHostString() + ":" + address.getPort() + "'s client version. Client Version: " + clientVersion.getReleaseName());
@@ -26,6 +26,9 @@ public enum ConnectionState {
HANDSHAKING, STATUS, LOGIN, PLAY;
public static ConnectionState getById(int id) {
if (id >= values().length || id < 0) {
return null;
}
return values()[id];
}
}
@@ -32,23 +32,29 @@ public class ServerChannelHandler extends ChannelInboundHandlerAdapter {
public static boolean CHECKED_NETTY_VERSION;
public static PEVersion NETTY_VERSION;
public static final PEVersion MODERN_NETTY_VERSION = new PEVersion(4, 1, 24);
private static PEVersion resolveNettyVersion() {
Map<String, Version> nettyArtifacts = Version.identify();
Version version = nettyArtifacts.getOrDefault("netty-common", nettyArtifacts.get("netty-all"));
if (version != null) {
String stringVersion = version.artifactVersion();
//Let us remove the ".Final" from the version by just removing any words (non numbers or dots)
stringVersion = stringVersion.replaceAll("[^\\d.]", "");
if (stringVersion.endsWith(".")) {
//Remove "." at the end.
stringVersion = stringVersion.substring(0, stringVersion.length() - 1);
}
return new PEVersion(stringVersion);
}
return null;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel channel = (Channel) msg;
//Resolve netty version only once.
if (NETTY_VERSION == null && !CHECKED_NETTY_VERSION) {
Map<String, Version> nettyArtifacts = Version.identify();
Version version = nettyArtifacts.getOrDefault("netty-common", nettyArtifacts.get("netty-all"));
if (version != null) {
String stringVersion = version.artifactVersion();
//Let us remove the ".Final" from the version by just removing any words (non numbers or dots)
stringVersion = stringVersion.replaceAll("[^\\d.]", "");
if (stringVersion.endsWith(".")) {
//Remove "." at the end.
stringVersion = stringVersion.substring(0, stringVersion.length() - 1);
}
NETTY_VERSION = new PEVersion(stringVersion);
}
NETTY_VERSION = resolveNettyVersion();
CHECKED_NETTY_VERSION = true;
}
@@ -64,7 +64,12 @@ public class PacketEventsDecoder extends ByteToMessageDecoder {
try {
boolean doRecompression =
handleCompression(ctx, outputBuffer);
PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player, outputBuffer, true);
try {
PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player, outputBuffer, true);
}
catch (Exception ex) {
throw new PacketProcessException("Failed to process incoming packet", ex);
}
if (outputBuffer.isReadable()) {
if (doRecompression) {
ByteBuf temp = ctx.alloc().buffer();
@@ -114,6 +119,7 @@ public class PacketEventsDecoder extends ByteToMessageDecoder {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
//Check if the minecraft server will already print our exception for us.
//Don't print errors during handshake
if (ExceptionUtil.isException(cause, PacketProcessException.class) && !SpigotReflectionUtil.isMinecraftServerInstanceDebugging()
&& (user == null || user.getConnectionState() != ConnectionState.HANDSHAKING)) {
cause.printStackTrace();
@@ -27,7 +27,6 @@ import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
import io.github.retrooper.packetevents.util.viaversion.CustomPipelineUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
@@ -64,9 +63,14 @@ public class PacketEventsEncoder extends MessageToByteEncoder<Object> {
if (!in.isReadable()) return;
out.writeBytes(in);
}
PacketSendEvent sendEvent = PacketEventsImplHelper.handleClientBoundPacket(ctx.channel(), user, player, out, true, false);
if (sendEvent.hasPostTasks()) {
queuedPostTasks.addAll(sendEvent.getPostTasks());
try {
PacketSendEvent sendEvent = PacketEventsImplHelper.handleClientBoundPacket(ctx.channel(), user, player, out, true, false);
if (sendEvent.hasPostTasks()) {
queuedPostTasks.addAll(sendEvent.getPostTasks());
}
}
catch (Exception ex) {
throw new PacketProcessException("Failed to process outgoing packet", ex);
}
}
@@ -121,6 +125,7 @@ public class PacketEventsEncoder extends MessageToByteEncoder<Object> {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
//Check if the minecraft server will already print our exception for us.
//Don't print errors during handshake
if (ExceptionUtil.isException(cause, PacketProcessException.class) && !SpigotReflectionUtil.isMinecraftServerInstanceDebugging()
&& (user == null || user.getConnectionState() != ConnectionState.HANDSHAKING)) {
cause.printStackTrace();