V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lhx2008
V2EX  ›  问与答

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

  •  
  •   lhx2008 · 2018-07-11 11:53:26 +08:00 · 3126 次点击
    这是一个创建于 2127 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我看到大部分人用的都是 Inbound 的接口

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

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

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

    in.channelActive()
    out.read()
    in.channelRead()
    in.channelReadComplete()
    out.close()
    in.channelInactive()
    第 1 条附言  ·  2018-07-11 12:35:44 +08:00
    3 条回复    2019-02-14 18:02:02 +08:00
    wowo243
        1
    wowo243  
       2018-07-11 12:04:54 +08:00 via Android
    同好奇
    SparkMan
        2
    SparkMan  
       2018-07-11 13:34:00 +08:00
    你先看看 Netty 权威指南,估计你看书后就不会来问这种问题了
    mortonnex
        3
    mortonnex  
       2019-02-14 18:02:02 +08:00   ❤️ 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()
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1096 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:11 · PVG 03:11 · LAX 12:11 · JFK 15:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.