首页  

netty ByteBuf 使用     所属分类 netty 浏览量 1144
OP_READ事件发生后,IO线程在 AbstractNioByteChannel.NioByteUnsafe.read() 方法里 调用 ByteBufAllocator 创建ByteBuf,
将TCP缓冲区的数据读取到Bytebuf中,并调用 pipeline.fireChannelRead(byteBuf) 进入Handler处理链。

ByteBuf 由最后使用者负责释放
ReferenceCountUtil.release(msg);


InBound
ByteBuf处理三种方式
对原消息不做处理,调用 ctx.fireChannelRead(msg) 把消息往下传,此时不做释放
将原消息转化为新的消息并调用 ctx.fireChannelRead(newMsg)往下传,需要把原消息释放掉
如果不再调用ctx.fireChannelRead(msg)传递消息,把原消息释放掉

ChannelPipleline末端有TailHandler,如果每个Handler都把消息往下传,TailHandler会释放掉ReferenceCounted类型的消息


OutBound
ctx.writeAndFlush(msg)  在flush完成后释放


Exception
可以在释放前加上引用计数大于0的判断
可以循环调用reelase()直到返回true

上一篇     下一篇
netty耗时任务处理

netty4 ChannelInboundHandler 使用

ChannelPipeline和ChannelInitializer

dubbo实例

IO模式 Proactor与Reactor

select poll epoll 区别