Netty基础 - 为什么ChannelOutboundHandler会声明一个read方法

Netty的ChannelOutboundHandler虽然主要处理outbound事件,但它包含read方法用于拦截ChannelHandlerContext.read()操作。这个设计在协议握手时有用,允许在必要时阻止向inbound读取更多数据。read方法的存在是因为它允许应用控制何时继续从socket读取数据,特别是在实现回压机制或手动管理读取时。
摘要由CSDN通过智能技术生成

ChannelOutboundHandler本应该只关注outbound事件,但是它却声明了一个read方法:

/**
* Intercepts {@link ChannelHandlerContext#read()}.
*/
void read(ChannelHandlerContext ctx) throws Exception;

有人在stackoverflow问了这个问题,trustin给出了回答:

Inbound handlers are supposed to handle inbound events. Events are triggered by external stimuli such as data received from a socket. Outbound handlers are supposed to intercept the operations issued by your application.

所以ChannelOutboundHandler上的read方法,如其注释所述,是为了拦截ChannelHandlerContext.read()操作。也就是说,ChannelOutboundHandler可以通过read()方法在必要的时候阻止向inbound读取更多数据的操作。这个设计在处理协议的握手时非常有用。

 

 

I have a question in Netty4, An I/O event is handled by either a ChannelInboundHandler or a ChannelOutboundHandler

  1. The first question is why read and write method both in
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值