前面看了channel定义,以及接口定义,可知道实现类。
* I/O Request
* via {@link Channel} or
* {@link ChannelHandlerContext}
* |
* +---------------------------------------------------+---------------+
* | ChannelPipeline | |
* | \|/ |
* | +----------------------------------------------+----------+ |
* | | ChannelHandler N | |
* | +----------+-----------------------------------+----------+ |
* | /|\ | |
* | | \|/ |
* | +----------+-----------------------------------+----------+ |
* | | ChannelHandler N-1 | |
* | +----------+-----------------------------------+----------+ |
* | /|\ . |
* | . . |
* | ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
* | [method call] [method call] |
* | . . |
* | . \|/ |
* | +----------+-----------------------------------+----------+ |
* | | ChannelHandler 2 | |
* | +----------+-----------------------------------+----------+ |
* | /|\ | |
* | | \|/ |
* | +----------+-----------------------------------+----------+ |
* | | ChannelHandler 1 | |
* | +----------+-----------------------------------+----------+ |
* | /|\ | |
* +---------------+-----------------------------------+---------------+
* | \|/
* +---------------+-----------------------------------+---------------+
* | | | |
* | [ Socket.read() ] [ Socket.write() ] |
* | |
* | Netty Internal I/O Threads (Transport Implementation) |
* +-------------------------------------------------------------------+
在看下接口方法
ChannelPipeline 主要是 inbound和outbound方法
inbound方法一般是内部定义的模板方法,继承实现之后就期待调用即可
outbound方法一般是对方提供api接口,比如bind connect read write等
DefaultChannelPipeline
然后我们看下channelPipeline的实现类,如何构造pipe的 final AbstractChannel channel;
final AbstractChannelHandlerContext head;
final AbstractChannelHandlerContext tail;
private final Map<String, AbstractChannelHandlerContext> name2ctx =
new HashMap<String, AbstractChannelHandlerContext>(4);
/**
* @see #findInvoker(EventExecutorGroup)
*/
private Map<EventExecutorGroup, ChannelHandlerInvoker> childInvokers;
DefaultChannelPipeline(AbstractChannel channel) {
if (channel == null) {
throw new NullPointerException("channel");
}
this.channel = channel;
tail = new TailContext(this);
head = new HeadContext(this);
head.next = tail;
tail.prev = head;
}
关键的两个类tailContext和headcontext,这样的话就扯出我们下一个接口 ChannelHandler