Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log incomplete packet reads in NetworkHandler #2288

Merged
merged 2 commits into from
Dec 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading