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

本篇文章给大家分享的是有关Netty基础中为什么ChannelOutboundHandler会声明一个read方法,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

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

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

有人在*问了这个问题,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 ChannelOutboundHandler?

  2. why trigger read() method in the fireChannelReadComplete()? What is the design philosophy?

@Override
public ChannelPipeline fireChannelReadComplete() {
    head.fireChannelReadComplete();
    if (channel.config().isAutoRead()) {
        read();
    }
    return this;
}

 ---------------------------------------------------------------------------------------------------------------------------------------------------

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.

Re: Q1) read() is an operation you can issue to tell Netty to continue reading the inbound data from the socket, and that's why it's in an outbound handler.

Re: Q2) You don't usually issue a read() operation because Netty does that for you automatically if autoRead property is set to true. Typical flow when autoRead is on:

  1. Netty triggers an inbound event channelActive when socket is connected, and then issues a read() request to itself (see DefaultChannelPipeline.fireChannelActive())

  2. Netty reads something from the socket in response to the read() request.

  3. If something was read, Netty triggers channelRead().

  4. If there's nothing left to read, Netty triggers channelReadComplete()

  5. Netty issues another read() request to continue reading from the socket.

If autoRead is off, you have to issue a read() request manually. It's sometimes useful to turn autoRead off. For example, you might want to implement a backpressure mechanism by keeping the received data in the kernel space.

shareimprove this answer

answered Mar 13 '14 at 8:19

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

trustin

9,63666 gold badges3333 silver badges4747 bronze badges

  • This reasoning is not convincing. Inbound & Outbound are generally defined based on the direction of data flow, not based on the direction of who is calling whom. It looks to me like a bad design – Ashok Koyi Jul 6 '18 at 9:24

  • Why would you ask a channel outbound handler for reading? Completely counter intuitive/also known as bad design – Ashok Koyi Jul 6 '18 at 9:28

以上就是Netty基础中为什么ChannelOutboundHandler会声明一个read方法,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。