Skip to content

Commit

Permalink
log incomplete packet reads in NetworkHandler (#2288)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 authored and serenibyss committed Dec 17, 2023
1 parent d2d4420 commit c86e116
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/main/java/gregtech/core/network/internal/NetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gregtech.api.network.INetworkHandler;
import gregtech.api.network.IPacket;
import gregtech.api.network.IServerExecutor;
import gregtech.api.util.GTLog;
import gregtech.core.CoreModule;

import net.minecraft.client.network.NetHandlerPlayClient;
Expand All @@ -26,8 +27,11 @@
import net.minecraftforge.fml.relauncher.SideOnly;

import io.netty.buffer.Unpooled;
import org.jetbrains.annotations.NotNull;

public class NetworkHandler implements INetworkHandler {
import java.lang.reflect.InvocationTargetException;

public final class NetworkHandler implements INetworkHandler {

private static final NetworkHandler INSTANCE = new NetworkHandler();

Expand Down Expand Up @@ -106,7 +110,7 @@ public void sendToServer(IPacket packet) {

@SubscribeEvent
@SideOnly(Side.CLIENT)
public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent event) throws Exception {
public void onClientPacket(FMLNetworkEvent.@NotNull ClientCustomPacketEvent event) throws Exception {
IPacket packet = toGTPacket(event.getPacket());
if (IClientExecutor.class.isAssignableFrom(packet.getClass())) {
IClientExecutor executor = (IClientExecutor) packet;
Expand All @@ -121,7 +125,7 @@ public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent event) throws
}

@SubscribeEvent
public void onServerPacket(FMLNetworkEvent.ServerCustomPacketEvent event) throws Exception {
public void onServerPacket(FMLNetworkEvent.@NotNull ServerCustomPacketEvent event) throws Exception {
IPacket packet = toGTPacket(event.getPacket());
if (IServerExecutor.class.isAssignableFrom(packet.getClass())) {
IServerExecutor executor = (IServerExecutor) packet;
Expand All @@ -135,17 +139,27 @@ public void onServerPacket(FMLNetworkEvent.ServerCustomPacketEvent event) throws
}
}

private FMLProxyPacket toFMLPacket(IPacket packet) {
private @NotNull FMLProxyPacket toFMLPacket(@NotNull IPacket packet) {
PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
buf.writeVarInt(packetHandler.getPacketId(packet.getClass()));
packet.encode(buf);
return new FMLProxyPacket(buf, GTValues.MODID);
}

private IPacket toGTPacket(FMLProxyPacket proxyPacket) throws Exception {
private @NotNull IPacket toGTPacket(@NotNull FMLProxyPacket proxyPacket) throws NoSuchMethodException,
InvocationTargetException,
InstantiationException,
IllegalAccessException {
PacketBuffer payload = (PacketBuffer) proxyPacket.payload();
IPacket packet = packetHandler.getPacketClass(payload.readVarInt()).newInstance();
var clazz = packetHandler.getPacketClass(payload.readVarInt());
IPacket packet = clazz.getConstructor().newInstance();
packet.decode(payload);

if (payload.readableBytes() != 0) {
GTLog.logger.error(
"NetworkHandler failed to finish reading packet with class {} and {} bytes remaining",
clazz.getName(), payload.readableBytes());
}
return packet;
}
}

0 comments on commit c86e116

Please sign in to comment.