Netty 4:如何手动调用处理程序?

时间:2013-12-19 21:14:35

标签: java netty

情况:DelimiterBasedFrameDecoder extends ByteToMessageDecoderByteToMessageDecoder确实将未处理的字节保存在名为ByteBuf的{​​{1}}中。 我想在另一个处理程序中手动调用此处理程序来清空此cumulation cumulation

我尝试了什么:

ByteBuf

问题:我尝试的东西不起作用,因为没有下一个处理程序,所以Netty告诉我字节丢失了:

  

废弃的入站邮件UnpooledHeapByteBuf(ridx:0,widx:57,cap:   57)到达管道的尾部。请检查你的   管道配置。

2 个答案:

答案 0 :(得分:3)

您可以将处理程序包装在EmbeddedChannel中,并使用writeInbound()和readInbound()。检查我们的单元测试以获取用法示例以及javadoc。

答案 1 :(得分:0)

诺曼让我走上正轨,但这就是我需要做的事情:

PublicCumulationDelimiterBasedFrameDecoder frameDecoder = (PublicCumulationDelimiterBasedFrameDecoder) cp.get("frameDecoder");

ByteBuf bytesNotProcessed = frameDecoder.internalBuffer();

if (bytesNotProcessed != Unpooled.EMPTY_BUFFER) {                                   PublicCumulationDelimiterBasedFrameDecoder tmpDelimiterBasedFrameDecoder = new PublicCumulationDelimiterBasedFrameDecoder(2048, false, true, Unpooled.wrappedBuffer(new byte[] { 0x03 }));

    EmbeddedChannel ec = new EmbeddedChannel(tmpDelimiterBasedFrameDecoder, ... other handlers...);

    ByteBuf wrapper = Unpooled.buffer();
    wrapper.writeBytes(bytesNotProcessed);

    ec.writeInbound(wrapper);// make it process the bytes

    // put the remaining bytes (if any) back into the original buffer
    bytesNotProcessed.clear();
    bytesNotProcessed.writeBytes(tmpDelimiterBasedFrameDecoder.internalBuffer());
}

其中PublicCumulationDelimiterBasedFrameDecoder就是这样:

public class PublicCumulationDelimiterBasedFrameDecoder extends DelimiterBasedFrameDecoder {

    public PublicCumulationDelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, boolean failFast, ByteBuf delimiter) {
        super(maxFrameLength, stripDelimiter, failFast, delimiter);
    }

    public ByteBuf internalBuffer() {
        return super.internalBuffer();
    }
}