我正在项目中用Netty4替换Netty3。我使用Google协议缓冲区作为数据传输。关闭在Netty3中正常工作的频道时我遇到了异常。
Bellowed是例外情况:
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(0): PooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 4096)
at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:95)
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelInactive(DefaultChannelHandlerContext.java:801)
at io.netty.channel.DefaultChannelHandlerContext.fireChannelInactive(DefaultChannelHandlerContext.java:787)
at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:808)
at io.netty.channel.AbstractChannel$AbstractUnsafe$9.run(AbstractChannel.java:725)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:364)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:326)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:114)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(0): PooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 4096)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1120)
at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:627)
at com.fhq.mathematica.netty.MMADecoder.decode(MMADecoder.java:17)
at io.netty.handler.codec.ByteToMessageDecoder.decodeLast(ByteToMessageDecoder.java:168)
at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:91)
这是编码器:(消息是顶级协议缓冲区的接口。)
public class MessageEncoder extends MessageToByteEncoder<Message>{
@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out)
throws Exception {
byte[] data = msg.toByteArray();
out.writeInt(data.length);
out.writeBytes(data);
}
}
这是解码器:
public class MMADecoder extends ByteToMessageDecoder{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
MessageBuf<Object> out) throws Exception {
in.markReaderIndex();
int dataLength = in.readInt();
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
byte[] decoded = new byte[dataLength];
in.readBytes(decoded);
out.add(MathematicaTask.parseFrom(decoded));
}
}
当我刚刚传输MathematicaTask对象时,它工作正常(客户端和服务器解析消息都正确)。但是当我关闭频道时,解码器会出现异常。
int dataLength = in.readInt();
有人能解决问题吗?蘑菇赞赏!
BTW:我尝试过ObjectEncoder和ObjectDecoder,然后一切都很好。所以我的解码器/编码器一定有问题。
答案 0 :(得分:0)
在调用ByteBuf.readInt()之前,您需要检查是否有足够的字节要读取。
这样的事情:
public class MMADecoder extends ByteToMessageDecoder{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
MessageBuf<Object> out) throws Exception {
if (in.readableBytes < 4) {
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
byte[] decoded = new byte[dataLength];
in.readBytes(decoded);
out.add(MathematicaTask.parseFrom(decoded));
}
}