use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project MinecraftForge by MinecraftForge.
the class FMLIndexedMessageToMessageCodec method decode.
@Override
protected final void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
testMessageValidity(msg);
ByteBuf payload = msg.payload().duplicate();
if (payload.readableBytes() < 1) {
FMLLog.log(Level.ERROR, "The FMLIndexedCodec has received an empty buffer on channel %s, likely a result of a LAN server issue. Pipeline parts : %s", ctx.channel().attr(NetworkRegistry.FML_CHANNEL), ctx.pipeline().toString());
}
byte discriminator = payload.readByte();
Class<? extends A> clazz = discriminators.get(discriminator);
if (clazz == null) {
throw new NullPointerException("Undefined message for discriminator " + discriminator + " in channel " + msg.channel());
}
A newMsg = clazz.newInstance();
ctx.attr(INBOUNDPACKETTRACKER).get().set(new WeakReference<FMLProxyPacket>(msg));
decodeInto(ctx, payload.slice(), newMsg);
out.add(newMsg);
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project MinecraftForge by MinecraftForge.
the class PacketLoggingHandler method register.
public static void register(NetworkManager manager) {
ChannelPipeline pipeline = manager.channel().pipeline();
final EnumPacketDirection direction = manager.getDirection();
if (manager.isLocalChannel()) {
pipeline.addBefore("packet_handler", "splitter", new SimpleChannelInboundHandler<Packet<?>>() {
String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: C->S" : "CLIENT: S->C");
@Override
protected void channelRead0(ChannelHandlerContext ctx, Packet<?> msg) throws Exception {
PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
msg.writePacketData(buf);
FMLLog.log(Level.DEBUG, "%s %s:\n%s", prefix, msg.getClass().getSimpleName(), ByteBufUtils.getContentDump(buf));
ctx.fireChannelRead(msg);
}
});
pipeline.addBefore("splitter", "prepender", new ChannelOutboundHandlerAdapter() {
String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: S->C" : "CLIENT: C->S");
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof Packet<?>) {
PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
((Packet<?>) msg).writePacketData(buf);
FMLLog.log(Level.DEBUG, "%s %s:\n%s", prefix, msg.getClass().getSimpleName(), ByteBufUtils.getContentDump(buf));
}
ctx.write(msg, promise);
}
});
} else {
pipeline.replace("splitter", "splitter", new NettyVarint21FrameDecoder() {
String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: C->S" : "CLIENT: S->C");
@Override
protected void decode(ChannelHandlerContext context, ByteBuf input, List<Object> output) throws Exception {
super.decode(context, input, output);
Iterator<Object> itr = output.iterator();
while (itr.hasNext()) {
ByteBuf pkt = (ByteBuf) itr.next();
pkt.markReaderIndex();
FMLLog.log(Level.DEBUG, "%s:\n%s", prefix, ByteBufUtils.getContentDump(pkt));
pkt.resetReaderIndex();
}
}
});
pipeline.replace("prepender", "prepender", new NettyVarint21FrameEncoder() {
String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: S->C" : "CLIENT: C->S");
@Override
protected void encode(ChannelHandlerContext context, ByteBuf input, ByteBuf output) throws Exception {
input.markReaderIndex();
FMLLog.log(Level.DEBUG, "%s:\n%s", prefix, ByteBufUtils.getContentDump(input));
input.resetReaderIndex();
super.encode(context, input, output);
}
});
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project MinecraftForge by MinecraftForge.
the class TestNetStuff method testByteBufUtilsStrings.
@Test
public void testByteBufUtilsStrings() {
String test = new String("test");
ByteBuf buf = Unpooled.buffer(20, 20);
ByteBufUtils.writeUTF8String(buf, test);
assertArrayEquals("String bytes", new byte[] { 4, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, buf.array());
String repeat = Strings.repeat("test", 100);
buf = Unpooled.buffer(420, 420);
ByteBufUtils.writeUTF8String(buf, repeat);
assertArrayEquals("String repeat bytes", new byte[] { -112, 3, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, buf.array());
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project MinecraftForge by MinecraftForge.
the class TestNetStuff method testByteBufUtilsByteArrays.
@Test
public void testByteBufUtilsByteArrays() {
ByteBuf buf = Unpooled.buffer(5, 5);
ByteBufUtils.writeVarInt(buf, 1, 1);
assertArrayEquals("1 as byte[] is [1]", new byte[] { 1, 0, 0, 0, 0 }, buf.array());
buf.clear();
ByteBufUtils.writeVarInt(buf, 127, 1);
assertArrayEquals("127 as byte[] is [127]", new byte[] { 127, 0, 0, 0, 0 }, buf.array());
buf.clear();
ByteBufUtils.writeVarInt(buf, 128, 2);
assertArrayEquals("128 as byte[] is [-128, 1]", new byte[] { -128, 1, 0, 0, 0 }, buf.array());
buf.clear();
ByteBufUtils.writeVarInt(buf, 16383, 2);
assertArrayEquals("16383 as byte[] is [-1, 127]", new byte[] { -1, 127, 0, 0, 0 }, buf.array());
buf.clear();
ByteBufUtils.writeVarInt(buf, 16384, 3);
assertArrayEquals("16384 as byte[] is [-1, -128, 1]", new byte[] { -128, -128, 1, 0, 0 }, buf.array());
buf.clear();
ByteBufUtils.writeVarInt(buf, 2097151, 3);
assertArrayEquals("2097151 as byte[] is [-1, -1, 127]", new byte[] { -1, -1, 127, 0, 0 }, buf.array());
buf.clear();
ByteBufUtils.writeVarInt(buf, 2097152, 4);
assertArrayEquals("16384 as byte[] is [-128, -128, 1]", new byte[] { -128, -128, -128, 1, 0 }, buf.array());
buf.clear();
ByteBufUtils.writeVarInt(buf, 268435455, 4);
assertArrayEquals("268435455 as byte[] is [-1, -1, -1, 127]", new byte[] { -1, -1, -1, 127, 0 }, buf.array());
buf.clear();
ByteBufUtils.writeVarInt(buf, 268435456, 5);
assertArrayEquals("268435456 as byte[] is [-1, -128, 1]", new byte[] { -128, -128, -128, -128, 1 }, buf.array());
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project MinecraftForge by MinecraftForge.
the class TestNetStuff method testVarShort.
@Test
public void testVarShort() {
ByteBuf buf = Unpooled.buffer(3, 3);
ByteBufUtils.writeVarShort(buf, 32766);
assertArrayEquals("Two byte short written", new byte[] { 127, -2, 0 }, buf.array());
assertEquals("Two byte short written", 2, buf.readableBytes());
buf.clear();
buf.writeZero(3);
buf.clear();
buf.writeShort(32766);
assertArrayEquals("Two byte short written", new byte[] { 127, -2, 0 }, buf.array());
int val = ByteBufUtils.readVarShort(buf);
assertEquals("Two byte short read correctly", 32766, val);
buf.clear();
buf.writeZero(3);
buf.clear();
ByteBufUtils.writeVarShort(buf, 32768);
assertArrayEquals("Three byte short written", new byte[] { -128, 0, 1 }, buf.array());
assertEquals("Three byte short written", 3, buf.readableBytes());
buf.clear();
buf.writeZero(3);
buf.clear();
buf.writeShort(-32768);
buf.writeByte(1);
val = ByteBufUtils.readVarShort(buf);
assertEquals("Three byte short read correctly", 32768, val);
buf.clear();
buf.writeZero(3);
buf.clear();
ByteBufUtils.writeVarShort(buf, 8388607);
assertArrayEquals("Three byte short written", new byte[] { -1, -1, -1 }, buf.array());
assertEquals("Three byte short written", 3, buf.readableBytes());
buf.clear();
buf.writeZero(3);
buf.clear();
buf.writeShort(-1);
buf.writeByte(-1);
val = ByteBufUtils.readVarShort(buf);
assertEquals("Three byte short read correctly", 8388607, val);
}
Aggregations