公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了Decode()没有读取任何内容,但解码了一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于套接字服务器应用程序,我创建了一个PacketFragmenter,它读取包的长度(在包的第二个字节中),然后将包发送回管道。

这是我写的代码:

public class PacketFragmenter extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in,
            List<Object> out) throws Exception {
        //I read a byte just to make the reader index go to second byte
        in.readByte();

        //in the second byte i get the content's length
        int length = in.readByte();

        //if my content is smaller than the readableBytes, there's a problem, so i return
        if (in.readableBytes() < length) {
            return;
        }

        //If everything is good, i reset the reader index to be able to write the whole packet in the out buffer (because i need the first byte in next handler, same for the size)
        in.resetReaderIndex();
        //I send my packet to the next handler
        out.add(in.readBytes(length +2));
        //and i reset the rederIndex to be able to read another packet
        in.resetReaderIndex();
    }

}

我在测试中得到了这个堆栈:

io.netty.handler.codec.DecoderException: PacketFragmenter.decode() did not read anything but decoded a message.
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:334)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:229)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:339)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:324)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:847)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
    at java.lang.Thread.run(Thread.java:745)

但一切正常,我收到了连续的两个包,但它们拼接得很好,下一个操作员正在做他的工作。

所以我不知道应该处理此异常还是忽略它?或者我可以做一件简单的事情来修复它,我根本不是一个网络专家(一周前开始的),所以这应该很容易修复。但我在Netty的用户指南上什么也没有找到。

推荐答案

public class PacketFrameDecoder extends LengthFieldBasedFrameDecoder {

    private static final int MAX_PACKET_LENGTH = 8192 * 2;
    private static final int LENGTH_FIELD_OFFSET = 1;
    private static final int LENGTH_FIELD_LENGTH = 1;
    private static final int LENGTH_FIELD_ADJUSTMENT = 0;
    private static final int INITIAL_BYTES_TO_STRIP = 0;

    public PacketFrameDecoder()
    {
        super(MAX_PACKET_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_FIELD_ADJUSTMENT, INITIAL_BYTES_TO_STRIP);
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception 
    {
        return super.decode(ctx, in);
    }
}

以下是我如何修复它的,事实上有一个LengthFieldBasedFrameDecoder为它做了,不需要扩展ByteToMessageDecoder。

这篇关于Decode()没有读取任何内容,但解码了一条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:decode 内容 消息 解码 读取
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定