lhx2008
V2EX  ›  问与答

Netty 中的 ChannelOutboundHandler 接口到底有啥用?

  •  
  •   lhx2008 · Jul 11, 2018 · 3933 views
    This topic created in 3037 days ago, the information mentioned may be changed or developed.
    我看到大部分人用的都是 Inbound 的接口

    但是 outbound 里面有 read() write() close() 这些接口,什么时候会用到呢?

    因为感觉作用和 Inbound 接口是重叠的,调用顺序也基本和 Inbound 重叠,Inbound 链中也可以对出站数据进行处理,传入参数都是 ChannelHandlerContext

    网上的资料和官方 doc 也讲的很玄乎,入站出站,但是 debug 一下就发现和 Inbound 是重叠的,只是名字不一样

    in.channelActive()
    out.read()
    in.channelRead()
    in.channelReadComplete()
    out.close()
    in.channelInactive()
    Supplement 1  ·  Jul 11, 2018
    3 replies  •  2019-02-14 18:02:02 +08:00
    wowo243
    wowo243
    Jul 11, 2018 via Android
    1
    同好奇
    SparkMan
    SparkMan
    Jul 11, 2018
    2
    你先看看 Netty 权威指南,估计你看书后就不会来问这种问题了
    mortonnex
    mortonnex
    Feb 14, 2019❤️1
    3
    挖个坟

    个人觉得 https://stackoverflow.com/questions/22354135/in-netty4-why-read-and-write-both-in-outboundhandler 这里面的回答非常好, outboundHandler 可以理解为人为的外部操作, 而 netty 启动的时候,如果没有设置 autoRead, 那么 netty 不会去 read,所以这时候需要认为"驱动"netty 去 read, 所以 outboundHandler 里面有 read 方法,这个方法传递到最后其实是 header, 然后会注册 interestOps 为 OP_READ,从而监听读事件

    同时注意:netty 在每完成一次读之后,都会 removeReadOp()
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1394 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:50 · PVG 06:50 · LAX 14:50 · JFK 17:50
    ♥ Do have faith in what you're doing.