From a355eecb2c14123964c6ae2402a0933d57ae9736 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 20 Jan 2014 16:02:26 -0500 Subject: [PATCH] Add in error logging for outbound messages. Fix bug with indexedcodec NPE --- .../fml/common/network/FMLEventChannel.java | 13 +++++---- .../FMLIndexedMessageToMessageCodec.java | 29 ++++++++++++++++--- .../handshake/FMLHandshakeClientState.java | 13 +++++---- .../handshake/FMLHandshakeServerState.java | 11 +++---- .../SimpleChannelHandlerWrapper.java | 3 +- .../simpleimpl/SimpleNetworkWrapper.java | 11 +++---- 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/main/java/cpw/mods/fml/common/network/FMLEventChannel.java b/src/main/java/cpw/mods/fml/common/network/FMLEventChannel.java index 6872c42da9..793db47944 100644 --- a/src/main/java/cpw/mods/fml/common/network/FMLEventChannel.java +++ b/src/main/java/cpw/mods/fml/common/network/FMLEventChannel.java @@ -1,5 +1,6 @@ package cpw.mods.fml.common.network; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import java.util.EnumMap; import net.minecraft.client.network.NetHandlerPlayClient; @@ -48,7 +49,7 @@ public class FMLEventChannel { if (event.reply != null) { ctx.channel().attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.REPLY); - ctx.writeAndFlush(event.reply); + ctx.writeAndFlush(event.reply).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } } } @@ -62,33 +63,33 @@ public class FMLEventChannel { public void sendToAll(FMLProxyPacket pkt) { channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL); - channels.get(Side.SERVER).writeAndFlush(pkt); + channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } public void sendTo(FMLProxyPacket pkt, EntityPlayerMP player) { channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER); channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player); - channels.get(Side.SERVER).writeAndFlush(pkt); + channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } public void sendToAllAround(FMLProxyPacket pkt, NetworkRegistry.TargetPoint point) { channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT); channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point); - channels.get(Side.SERVER).writeAndFlush(pkt); + channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } public void sendToDimension(FMLProxyPacket pkt, int dimensionId) { channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION); channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(dimensionId); - channels.get(Side.SERVER).writeAndFlush(pkt); + channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } public void sendToServer(FMLProxyPacket pkt) { channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER); - channels.get(Side.CLIENT).writeAndFlush(pkt); + channels.get(Side.CLIENT).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } } diff --git a/src/main/java/cpw/mods/fml/common/network/FMLIndexedMessageToMessageCodec.java b/src/main/java/cpw/mods/fml/common/network/FMLIndexedMessageToMessageCodec.java index 408b98f660..0989bdad57 100644 --- a/src/main/java/cpw/mods/fml/common/network/FMLIndexedMessageToMessageCodec.java +++ b/src/main/java/cpw/mods/fml/common/network/FMLIndexedMessageToMessageCodec.java @@ -7,7 +7,10 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageCodec; +import io.netty.util.AttributeKey; import java.util.List; +import org.apache.logging.log4j.Level; +import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.network.internal.FMLProxyPacket; @Sharable @@ -18,7 +21,15 @@ public abstract class FMLIndexedMessageToMessageCodec extends MessageToMessag /** * Make this accessible to subclasses */ - protected final ThreadLocal inboundPacket = new ThreadLocal(); + + protected static final AttributeKey> INBOUNDPACKETTRACKER = new AttributeKey>("fml:inboundpacket"); + + @Override + public void handlerAdded(ChannelHandlerContext ctx) throws Exception + { + super.handlerAdded(ctx); + ctx.attr(INBOUNDPACKETTRACKER).set(new ThreadLocal()); + } public FMLIndexedMessageToMessageCodec addDiscriminator(int discriminator, Class type) { @@ -38,8 +49,11 @@ public abstract class FMLIndexedMessageToMessageCodec extends MessageToMessag buffer.writeByte(discriminator); encodeInto(ctx, msg, buffer); FMLProxyPacket proxy = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); - FMLProxyPacket old = inboundPacket.get(); - proxy.setDispatcher(old.getDispatcher()); + FMLProxyPacket old = ctx.attr(INBOUNDPACKETTRACKER).get().get(); + if (old != null) + { + proxy.setDispatcher(old.getDispatcher()); + } out.add(proxy); } @@ -57,7 +71,7 @@ public abstract class FMLIndexedMessageToMessageCodec extends MessageToMessag throw new NullPointerException("Undefined message for discriminator " + discriminator + " in channel " + msg.channel()); } A newMsg = clazz.newInstance(); - inboundPacket.set(msg); + ctx.attr(INBOUNDPACKETTRACKER).get().set(msg); decodeInto(ctx, payload.slice(), newMsg); out.add(newMsg); } @@ -70,4 +84,11 @@ public abstract class FMLIndexedMessageToMessageCodec extends MessageToMessag protected void testMessageValidity(FMLProxyPacket msg) { } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception + { + FMLLog.log(Level.ERROR, cause, "FMLIndexedMessageCodec exception caught"); + super.exceptionCaught(ctx, cause); + } } diff --git a/src/main/java/cpw/mods/fml/common/network/handshake/FMLHandshakeClientState.java b/src/main/java/cpw/mods/fml/common/network/handshake/FMLHandshakeClientState.java index bd3df960b5..09deb866bb 100644 --- a/src/main/java/cpw/mods/fml/common/network/handshake/FMLHandshakeClientState.java +++ b/src/main/java/cpw/mods/fml/common/network/handshake/FMLHandshakeClientState.java @@ -1,5 +1,6 @@ package cpw.mods.fml.common.network.handshake; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.Loader; @@ -48,8 +49,8 @@ enum FMLHandshakeClientState implements IHandshakeState } FMLLog.info("Server protocol version %x", ((FMLHandshakeMessage.ServerHello)msg).protocolVersion()); - ctx.writeAndFlush(new FMLHandshakeMessage.ClientHello()); - ctx.writeAndFlush(new FMLHandshakeMessage.ModList(Loader.instance().getActiveModList())); + ctx.writeAndFlush(new FMLHandshakeMessage.ClientHello()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + ctx.writeAndFlush(new FMLHandshakeMessage.ModList(Loader.instance().getActiveModList())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); return WAITINGSERVERDATA; } }, @@ -66,7 +67,7 @@ enum FMLHandshakeClientState implements IHandshakeState dispatcher.rejectHandshake(result); return ERROR; } - ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())); + ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); if (!ctx.channel().attr(NetworkDispatcher.IS_LOCAL).get()) { return WAITINGSERVERCOMPLETE; @@ -89,7 +90,7 @@ enum FMLHandshakeClientState implements IHandshakeState dispatcher.rejectHandshake("Fatally missing blocks and items"); return ERROR; } - ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())); + ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); return PENDINGCOMPLETE; } }, @@ -98,7 +99,7 @@ enum FMLHandshakeClientState implements IHandshakeState @Override public FMLHandshakeClientState accept(ChannelHandlerContext ctx, FMLHandshakeMessage msg) { - ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())); + ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); return COMPLETE; } }, @@ -111,7 +112,7 @@ enum FMLHandshakeClientState implements IHandshakeState dispatcher.completeClientHandshake(); FMLMessage.CompleteHandshake complete = new FMLMessage.CompleteHandshake(Side.CLIENT); ctx.fireChannelRead(complete); - ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())); + ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); return DONE; } }, diff --git a/src/main/java/cpw/mods/fml/common/network/handshake/FMLHandshakeServerState.java b/src/main/java/cpw/mods/fml/common/network/handshake/FMLHandshakeServerState.java index 79315fd288..2badf7968f 100644 --- a/src/main/java/cpw/mods/fml/common/network/handshake/FMLHandshakeServerState.java +++ b/src/main/java/cpw/mods/fml/common/network/handshake/FMLHandshakeServerState.java @@ -1,5 +1,6 @@ package cpw.mods.fml.common.network.handshake; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.Loader; @@ -18,8 +19,8 @@ enum FMLHandshakeServerState implements IHandshakeState { NetworkDispatcher dispatcher = ctx.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(); dispatcher.serverInitiateHandshake(); - ctx.writeAndFlush(FMLHandshakeMessage.makeCustomChannelRegistration(NetworkRegistry.INSTANCE.channelNamesFor(Side.SERVER))); - ctx.writeAndFlush(new FMLHandshakeMessage.ServerHello()); + ctx.writeAndFlush(FMLHandshakeMessage.makeCustomChannelRegistration(NetworkRegistry.INSTANCE.channelNamesFor(Side.SERVER))).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + ctx.writeAndFlush(new FMLHandshakeMessage.ServerHello()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); return HELLO; } }, @@ -55,9 +56,9 @@ enum FMLHandshakeServerState implements IHandshakeState { if (!ctx.channel().attr(NetworkDispatcher.IS_LOCAL).get()) { - ctx.writeAndFlush(new FMLHandshakeMessage.ModIdData(GameData.buildItemDataList())); + ctx.writeAndFlush(new FMLHandshakeMessage.ModIdData(GameData.buildItemDataList())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } - ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())); + ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); NetworkRegistry.INSTANCE.fireNetworkHandshake(ctx.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(), Side.SERVER); return COMPLETE; } @@ -68,7 +69,7 @@ enum FMLHandshakeServerState implements IHandshakeState public FMLHandshakeServerState accept(ChannelHandlerContext ctx, FMLHandshakeMessage msg) { // Poke the client - ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())); + ctx.writeAndFlush(new FMLHandshakeMessage.HandshakeAck(ordinal())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); FMLMessage.CompleteHandshake complete = new FMLMessage.CompleteHandshake(Side.SERVER); ctx.fireChannelRead(complete); return DONE; diff --git a/src/main/java/cpw/mods/fml/common/network/simpleimpl/SimpleChannelHandlerWrapper.java b/src/main/java/cpw/mods/fml/common/network/simpleimpl/SimpleChannelHandlerWrapper.java index 10d47c5a39..741069c8a6 100644 --- a/src/main/java/cpw/mods/fml/common/network/simpleimpl/SimpleChannelHandlerWrapper.java +++ b/src/main/java/cpw/mods/fml/common/network/simpleimpl/SimpleChannelHandlerWrapper.java @@ -6,6 +6,7 @@ import com.google.common.base.Throwables; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.relauncher.Side; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; @@ -31,7 +32,7 @@ public class SimpleChannelHandlerWrapper