gpt4 book ai didi

java - Netty 示例中 ctx.read() 与 ctx.channel.read() 的使用

转载 作者:行者123 更新时间:2023-12-02 12:21:57 26 4
gpt4 key购买 nike

Netty 的管道(即 ctx.foo()ctx.channel.foo())之前已在 StackOverflow 上解释过两次:

  1. Any difference between ctx.write() and ctx.channel().write() in netty?
  2. In Netty 4, what's the difference between ctx.close and ctx.channel.close?

但是,我不明白 Netty 示例背后关于何时使用不同方法的直觉:

public void channelActive(ChannelHandlerContext ctx) {
ctx.read(); // <----- HERE
}

public void channelRead(final ChannelHandlerContext ctx, Object msg) {
inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
ctx.channel().read(); // <----- HERE
} else {
future.channel().close();
}
}
});
}

<强> View source on GitHub

<小时/>

为什么在代理实现的 channelActive 处理程序中使用“从该处理程序向下”样式 read,而使用“从顶部”样式 read channelRead 中?

最佳答案

当使用ChannelHandlerContext.read()时,它将从ChannelPipelineChannelHandler所在的点开始。当您使用 Channel.read() 时,它将从 ChannelPipeline 的尾部开始,因此需要以更差的方式遍历整个 ChannelPipeline。

此示例在 channelActive(...) 中使用 ctx.read() 而在ChannelFutureListener 是因为 ChannelFutureListener 不是 ChannelHandler 的一部分,因此它需要从 ChannelPipeline 的尾部开始>。另请注意,此处的Channel有所不同。

关于java - Netty 示例中 ctx.read() 与 ctx.channel.read() 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45702133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com