lhx2008
V2EX  ›  问与答

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

  •  
  •   lhx2008 · Jul 11, 2018 · 3524 views
    This topic created in 2861 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
        1
    wowo243  
       Jul 11, 2018 via Android
    同好奇
    SparkMan
        2
    SparkMan  
       Jul 11, 2018
    你先看看 Netty 权威指南,估计你看书后就不会来问这种问题了
    mortonnex
        3
    mortonnex  
       Feb 14, 2019   ❤️ 1
    挖个坟

    个人觉得 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   ·   Solana   ·   987 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 20:14 · PVG 04:14 · LAX 13:14 · JFK 16:14
    ♥ Do have faith in what you're doing.