溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Netty基礎(chǔ)中為什么ChannelOutboundHandler會(huì)聲明一個(gè)read方法

發(fā)布時(shí)間:2021-10-21 10:48:35 來(lái)源:億速云 閱讀:273 作者:柒染 欄目:大數(shù)據(jù)

本篇文章給大家分享的是有關(guān)Netty基礎(chǔ)中為什么ChannelOutboundHandler會(huì)聲明一個(gè)read方法,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

ChannelOutboundHandler本應(yīng)該只關(guān)注outbound事件,但是它卻聲明了一個(gè)read方法:

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

有人在stackoverflow問(wèn)了這個(gè)問(wèn)題,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()操作。也就是說(shuō),ChannelOutboundHandler可以通過(guò)read()方法在必要的時(shí)候阻止向inbound讀取更多數(shù)據(jù)的操作。這個(gè)設(shè)計(jì)在處理協(xié)議的握手時(shí)非常有用。

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基礎(chǔ)中為什么ChannelOutboundHandler會(huì)聲明一個(gè)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基礎(chǔ)中為什么ChannelOutboundHandler會(huì)聲明一個(gè)read方法,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI