Add in error logging for outbound messages. Fix bug with indexedcodec NPE

This commit is contained in:
Christian 2014-01-20 16:02:26 -05:00
parent 87a45d83fb
commit 9fb9942498
6 changed files with 53 additions and 27 deletions

View file

@ -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);
}
}

View file

@ -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<A> extends MessageToMessag
/**
* Make this accessible to subclasses
*/
protected final ThreadLocal<FMLProxyPacket> inboundPacket = new ThreadLocal<FMLProxyPacket>();
protected static final AttributeKey<ThreadLocal<FMLProxyPacket>> INBOUNDPACKETTRACKER = new AttributeKey<ThreadLocal<FMLProxyPacket>>("fml:inboundpacket");
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
{
super.handlerAdded(ctx);
ctx.attr(INBOUNDPACKETTRACKER).set(new ThreadLocal<FMLProxyPacket>());
}
public FMLIndexedMessageToMessageCodec<A> addDiscriminator(int discriminator, Class<? extends A> type)
{
@ -38,8 +49,11 @@ public abstract class FMLIndexedMessageToMessageCodec<A> 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<A> 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<A> 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);
}
}

View file

@ -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<FMLHandshakeClientState>
}
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<FMLHandshakeClientState>
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<FMLHandshakeClientState>
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<FMLHandshakeClientState>
@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<FMLHandshakeClientState>
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;
}
},

View file

@ -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<FMLHandshakeServerState>
{
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<FMLHandshakeServerState>
{
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<FMLHandshakeServerState>
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;

View file

@ -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<REQ extends IMessage, REPLY extends IMe
REPLY result = messageHandler.onMessage(msg, context);
if (result != null)
{
ctx.writeAndFlush(result);
ctx.writeAndFlush(result).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
}

View file

@ -1,5 +1,6 @@
package cpw.mods.fml.common.network.simpleimpl;
import io.netty.channel.ChannelFutureListener;
import java.util.EnumMap;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetHandler;
@ -105,7 +106,7 @@ public class SimpleNetworkWrapper {
public void sendToAll(IMessage message)
{
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
channels.get(Side.SERVER).writeAndFlush(message);
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
/**
@ -119,7 +120,7 @@ public class SimpleNetworkWrapper {
{
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(message);
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
/**
@ -133,7 +134,7 @@ public class SimpleNetworkWrapper {
{
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(message);
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
/**
@ -147,7 +148,7 @@ public class SimpleNetworkWrapper {
{
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(message);
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
/**
@ -159,6 +160,6 @@ public class SimpleNetworkWrapper {
public void sendToServer(IMessage message)
{
channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
channels.get(Side.CLIENT).writeAndFlush(message);
channels.get(Side.CLIENT).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
}