I've the following Problem. Each time when I send a packet from my Client to my Server with the Same en- and Decoder, I get the following error in my Server console:
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(72) + length(8) exceeds writerIndex(77): UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf(ridx: 72, widx: 77, cap: 1024)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:461)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:356)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:342)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:335)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1294)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:356)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:342)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(72) + length(8) exceeds writerIndex(77): UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf(ridx: 72, widx: 77, cap: 1024)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1168)
at io.netty.buffer.AbstractByteBuf.readLong(AbstractByteBuf.java:651)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:27)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at net.rinet.rnclo.network.encoding.PacketDecoder.decode(PacketDecoder.java:39)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:491)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:430)
... 16 more
My Source Looks like this:
Encoder:
import net.rinet.rnclo.network.Packet;
import net.rinet.rnclo.network.PacketRegistry;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
/**
* JavaDoc this file!
*
* @author Pascal
* @version 29.10.2017
*/
@AllArgsConstructor
@NoArgsConstructor
public class PacketEncoder extends MessageToByteEncoder<Packet> {
private PacketRegistry packetRegistry;
@Override
protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf output) throws Exception {
long id = this.packetRegistry.getPacketIdByClass(packet.getClass());
output.writeLong(id);
packet.write(output);
}
}
And my Decoder Looks like this:
package net.rinet.rnclo.network.encoding;
import net.rinet.rnclo.network.Packet;
import net.rinet.rnclo.network.PacketRegistry;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* JavaDoc this file!
*
* @author Pascal
* @version 29.10.2017
*/
@AllArgsConstructor
@NoArgsConstructor
public class PacketDecoder extends ByteToMessageDecoder {
private PacketRegistry packetRegistry;
@Override
public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> outputList) throws Exception {
if(byteBuf.isReadable(2)) {
long id = byteBuf.readLong();
if(packetRegistry.checkPacketId(id)) {
Class<? extends Packet> packetClass = packetRegistry.getPacketClassById(id);
Packet receivedPacket = packetClass.newInstance();
try {
receivedPacket.read(byteBuf);
} catch (Throwable t) {
t.printStackTrace();
}
outputList.add(receivedPacket);
} else {
decode(channelHandlerContext, byteBuf, outputList);
}
}
}
}
My Packet Looks like this: https://hastebin.com/atirenevuw.java
I'm really glad about every helpful answer! Pascal
You’re checking to see if you can read 2 bytes, but then you read 8 bytes.
Change this
if(byteBuf.isReadable(2)) {
long id = byteBuf.readLong();
To
if(byteBuf.isReadable(8)) {
long id = byteBuf.readLong();