handler 与 pipeline
文章目录
- handler 与 pipeline
- 前言
- 一、pipeline与handler的关系
- 1. pipeline下所有的handler
- 二.客户端channel连进来
-
- 2.1回调搭配到自己在serverchannel端注册的 inboundhandler的channelRead方法。
- 2.2给client channel对象的pipeline添加childhandler。
- 2.3在添加完handler后,不忘给client channel分配一个eventloop进行注册
- 2.4最后调用的用户写的ChannelInitializer的initChannel方法将handler添加到client channel上
- 2.5然后从pipeline上寻找inbounhandler类型的handler对象调用channelRegistered方法
- 2.6然后从pipeline上寻找inbounhandler类型的handler对象调用channelActive()方法
- 2.6最后给client channel设置自动读取,然后从pipeline上寻找outbounhandler类型的handler对象调用 read方法
- 2.7然后当前client channel对应eventloop又开始进入到轮询队列看一下队列里是否有新的任务供它执行。
- 2.8然后进入反复自动化读取数据到一个字节容器里面,当数据读完没有可以读的时候就退出了死循环。
- 三.总结
前言
接着继续学习,pipeline与handler的关系以及handler的生命周期,以及关键的接口方法在什么条件下被触发
一、pipeline与handler的关系
有啥比源码的注释说的更明白–
/**
* A list of {@link ChannelHandler}s which handles or intercepts inbound events and outbound operations of a
* {@link Channel}. {@link ChannelPipeline} implements an advanced form of the
* <a href="http://www.oracle.com/technetwork/java/interceptingfilter-142169.html">Intercepting Filter</a> pattern
* to give a user full control over how an event is handled and how the {@link ChannelHandler}s in a pipeline
* interact with each other.
*
* <h3>Creation of a pipeline</h3>
*
* Each channel has its own pipeline and it is created automatically when a new channel is created.
*
* <h3>How an event flows in a pipeline</h3>
*
* The following diagram describes how I/O events are processed by {@link ChannelHandler}s in a {@link ChannelPipeline}
* typically. An I/O event is handled by either a {@link ChannelInboundHandler} or a {@link ChannelOutboundHandler}
* and be forwarded to its closest handler by calling the event propagation methods defined in
* {@link ChannelHandlerContext}, such as {@link ChannelHandlerContext#fireChannelRead(Object)} and
* {@link ChannelHandlerContext#write(Object)}.
*
* <pre>
* I/O Request
* via {@link Channel} or
* {@link ChannelHandlerContext}
* |
* +---------------------------------------------------+---------------+
* | ChannelPipeline | |
* | \|/ |
* | +---------------------+ +-----------+----------+ |
* | | Inbound Handler N | | Outbound Handler 1 | |
* | +----------+----------+ +-----------+----------+ |
* | /|\ | |
* | | \|/ |
* | +----------+----------+ +-----------+----------+ |
* | | Inbound Handler N-1 | | Outbound Handler 2 | |
* | +----------+----------+ +-----------+----------+ |
* | /|\ . |
* | . . |
* | ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
* | [ method call] [method call] |
* | . . |
* | . \|/ |
* | +----------+----------+ +-----------+----------+ |
* | | Inbound Handler 2 | | Outbound Handler M-1 | |
* | +----------+----------+ +-----------+----------+ |
* | /|\ | |
* | | \|/ |
* | +----------+----------+ +-----------+----------+ |
* | | Inbound Handler 1 | | Outbound Handler M | |
* | +----------+----------+ +-----------+----------+ |
* | /|\ | |
* +---------------+-----------------------------------+---------------+
* | \|/
* +---------------+-----------------------------------+---------------+
* | | | |
* | [ Socket.read() ] [ Socket.write() ] |
* | |
* | Netty Internal I/O Threads (Transport Implementation) |
* +-------------------------------------------------------------------+
* </pre>
* An inbound event is handled by the inbound handlers in the bottom-up direction as shown on the left side of the
* diagram. An inbound handler usually handles the inbound data generated by the I/O thread on the bottom of th