0

我是 netty 的新手,我尝试从服务器(非 netty)接收字节响应。但是,我相信我误解了引用计数。在阅读回复时,我遇到了IllegalReferenceCountException: refCnt:0

我所做的并不那么复杂。我设置了客户端,然后我发送了一条消息,我正在等待回复。

我的 ResponseHandler 看起来像这样:

public class ResponseHandler extends DriverResponseHandler
{
    private ByteBuf received;


    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception
    {
        super.handlerAdded(ctx);
        received = ctx.alloc().buffer(32);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
    {
        super.channelRead(ctx, msg);
        received.writeBytes((ByteBuf) msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
    {
        super.channelReadComplete(ctx);
        if (received.writerIndex() == received.capacity() || received.readerIndex() == received.capacity()) {
            received.clear();
            received = ctx.alloc().buffer(32);
        }
        processResponse(received);
    }
}

在读取channelRead异常中的响应时发生。为什么会发生。有人可以向我解释吗?

4

1 回答 1

0

好的,我解决了这个问题。我需要做的就是添加 ByteToMessageDedcoder,否则将永远不会分配传入的响应,因此将无法访问它。

于 2021-06-19T11:26:22.647 回答