gpt4 book ai didi

java - encode()什么都没读,但对一条消息进行了解码

转载 作者:行者123 更新时间:2023-12-03 11:58:08 24 4
gpt4 key购买 nike

对于套接字服务器应用程序,我创建了一个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的用户指南上什么也没找到。

最佳答案

我会在Supamiu的答案中添加更多信息。
在许多情况下,将LengthFieldBasedFrameDecoder用作父类是正确的方法,但是没有解释为什么引发异常的原因。

如果产生消息,则还需要从ByteBuf中读取内容。添加此检查是为了捕获由用户解码器错误产生的无限循环。

因此,如果您从原始方法中删除最后一行in.resetReaderIndex();,则异常将消失:

public class PacketFragmenter extends ByteToMessageDecoder {

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
in.skipBytes(1);
//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));
}
}

关于java - encode()什么都没读,但对一条消息进行了解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29129415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com