本文介紹了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()沒有讀取任何內容,但解碼了一條消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,