Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Netty4,why read and write both in OutboundHandler

Tags:

netty

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;
}
like image 708
xiang min Avatar asked Mar 12 '14 14:03

xiang min


1 Answers

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.

like image 77
trustin Avatar answered Sep 22 '22 19:09

trustin