首页 文章

ReplayingDecoder在解码时抛出异常

提问于
浏览
1

我创建了一个解码器来处理客户端发送的字节 . 就这个

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

public class MessageDecoder extends ReplayingDecoder<DecoderState> {

    private int length;

    public MessageDecoder()
    {
        super(DecoderState.READ_LENGTH);
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception{
        System.out.println(buf.readableBytes());
        switch(state()){
            case READ_LENGTH:
                length=buf.readInt();
                System.out.println("length is: "+length);
                checkpoint(DecoderState.READ_CONTENT);
            case READ_CONTENT:
                ByteBuf frame = buf.readBytes(length);
                checkpoint(DecoderState.READ_LENGTH);
                out.add(frame);
                break;
            default:
                throw new Error("Shouldn't reach here");
        }
    }
}

当客户端发送字节时,它会抛出下一个错误

io.netty.handler.codec.DecoderException:java.lang.IllegalArgumentException异常:minimumReadableBytes:-603652096(预期:> = 0)在io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:431)在IO . netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:245)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:962)at io.netty.channel.nio.AbstractNioByteChannel $ NioByteUnsafe.read(AbstractNioByteChannel.java:131)at io.netty.channel.nio.NioEventLoop.processSelectedKey (NioEventLoop.java:528)在io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:485)在io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:399)在io.netty . channel.nio.NioEventLoop.run(NioEve ntLoop.java:371)在io.netty.util.concurrent.SingleThreadEventExecutor $ 2.run(SingleThreadEventExecutor.java:112)在io.netty.util.concurrent.DefaultThreadFactory $ DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)在爪哇 . lang.Thread.run(未知来源)

该代码来自官方文档http://netty.io/4.0/api/io/netty/handler/codec/ReplayingDecoder.html所以我真的不明白为什么它不起作用

1 回答

  • 1

    远程对等体可能会将int写为unsigned . 也许你想要的是使用readUnsignedInt()?

相关问题