Skip to content

Instantly share code, notes, and snippets.

@boq
Created October 21, 2014 20:44
Show Gist options
  • Select an option

  • Save boq/0f46a45b9120f31cc2c8 to your computer and use it in GitHub Desktop.

Select an option

Save boq/0f46a45b9120f31cc2c8 to your computer and use it in GitHub Desktop.
// code used for logging
package debug;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableSet;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
public class InboundChannelLogger extends ChannelInboundHandlerAdapter {
private final String source;
public InboundChannelLogger(String source) {
this.source = source;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FMLProxyPacket) {
final FMLProxyPacket proxy = (FMLProxyPacket)msg;
if (proxy.channel().equals("REGISTER")) {
byte[] data = new byte[proxy.payload().readableBytes()];
proxy.payload().copy().readBytes(data);
String channels = new String(data, Charsets.UTF_8);
String[] split = channels.split("\0");
FMLLog.info("%s > %s (channel: %s, register: %s)", source, msg.getClass(), proxy.channel(), ImmutableSet.copyOf(split));
} else {
FMLLog.info("%s > %s (channel: %s)", source, msg.getClass(), proxy.channel());
}
} else {
FMLLog.info("%s > %s", source, msg.getClass());
}
ctx.fireChannelRead(msg);
}
}
// code used for logging
public NetworkDispatcher(NetworkManager manager)
{
super(Packet.class, false);
this.manager = manager;
this.scm = null;
this.side = Side.CLIENT;
this.handshakeChannel = new EmbeddedChannel(new HandshakeInjector(this),
new InboundChannelLogger("CLIENT-RAW"), new OutboundChannelLogger("CLIENT-RAW"),
new ChannelRegistrationHandler(),
new FMLHandshakeCodec(),
new InboundChannelLogger("CLIENT-DECODED"), new OutboundChannelLogger("CLIENT-DECODED"),
new HandshakeMessageHandler<FMLHandshakeClientState>(FMLHandshakeClientState.class));
this.handshakeChannel.attr(FML_DISPATCHER).set(this);
this.handshakeChannel.attr(NetworkRegistry.CHANNEL_SOURCE).set(Side.SERVER);
this.handshakeChannel.attr(NetworkRegistry.FML_CHANNEL).set("FML|HS");
this.handshakeChannel.attr(IS_LOCAL).set(manager.isLocalChannel());
}
public NetworkDispatcher(NetworkManager manager, ServerConfigurationManager scm)
{
super(Packet.class, false);
this.manager = manager;
this.scm = scm;
this.side = Side.SERVER;
this.handshakeChannel = new EmbeddedChannel(new HandshakeInjector(this),
new InboundChannelLogger("SERVER-RAW"), new OutboundChannelLogger("SERVER-RAW"),
new ChannelRegistrationHandler(),
new FMLHandshakeCodec(),
new InboundChannelLogger("SERVER-DECODED"), new OutboundChannelLogger("SERVER-DECODED"),
new HandshakeMessageHandler<FMLHandshakeServerState>(FMLHandshakeServerState.class));
this.handshakeChannel.attr(FML_DISPATCHER).set(this);
this.handshakeChannel.attr(NetworkRegistry.CHANNEL_SOURCE).set(Side.CLIENT);
this.handshakeChannel.attr(NetworkRegistry.FML_CHANNEL).set("FML|HS");
this.handshakeChannel.attr(IS_LOCAL).set(manager.isLocalChannel());
}
// code used for logging
package debug;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableSet;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
public class OutboundChannelLogger extends ChannelOutboundHandlerAdapter {
private final String sink;
public OutboundChannelLogger(String sink) {
this.sink = sink;
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof FMLProxyPacket) {
final FMLProxyPacket proxy = (FMLProxyPacket)msg;
if (proxy.channel().equals("REGISTER")) {
byte[] data = new byte[proxy.payload().readableBytes()];
proxy.payload().copy().readBytes(data);
String channels = new String(data, Charsets.UTF_8);
String[] split = channels.split("\0");
FMLLog.info("%s < %s (channel: %s, register: %s)", sink, msg.getClass(), proxy.channel(), ImmutableSet.copyOf(split));
} else {
FMLLog.info("%s < %s (channel: %s)", sink, msg.getClass(), proxy.channel());
}
} else {
FMLLog.info("%s < %s", sink, msg.getClass());
}
ctx.write(msg, promise);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment