我在我的项目中用 Netty4 替换了 Netty3。我正在使用 Google-protocol-buffer 作为数据传输。关闭在 Netty3 中运行良好的通道时出现异常。
以下是异常详细信息:
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();
谁能找出问题所在?穆什赞赏!
顺便说一句:我尝试了 ObjectEncoder 和 ObjectDecoder,然后一切都很好。所以我的解码器/编码器肯定有问题。