在Channel创建的时候Pipeline被创建出来,一个Channel对应一个Pipeline。
1
2
3
4
5
6
7
8
9
10
11
12
//AbstractChannel.java->DefaultChannelPipeline.java
protected DefaultChannelPipeline(Channel channel) {
this.channel = ObjectUtil.checkNotNull(channel, "channel");
succeededFuture = new SucceededChannelFuture(channel, null);
voidPromise = new VoidChannelPromise(channel, true);
tail = new TailContext(this);
head = new HeadContext(this);
head.next = tail;
tail.prev = head;
}
Pipeline是双向链表的结构。有两个节点
共同继承的抽象类。有删减
tail
1
2
3
4
5
6
//AbstractChannelHandlerContext.java
TailContext(DefaultChannelPipeline pipeline) {
super(pipeline, null, TAIL_NAME, true, false);
setAddComplete();
}
head
1
2
3
4
5
6
//AbstractChannelHandlerContext.java
HeadContext(DefaultChannelPipeline pipeline) {
super(pipeline, null, HEAD_NAME, false, true);
unsafe = pipeline.channel().unsafe();
setAddComplete();
}
1
2
3
4
5
6
7
8
9
10
//AbstractChannelHandlerContext.java
AbstractChannelHandlerContext(DefaultChannelPipeline pipeline, EventExecutor executor, String name,
boolean inbound, boolean outbound) {
this.name = ObjectUtil.checkNotNull(name, "name");
this.pipeline = pipeline;
this.executor = executor;
this.inbound = inbound;
this.outbound = outbound;
ordered = executor == null || executor instanceof OrderedEventExecutor;
}
tail和head都是pipeline中的数据节点,它们用布尔值在父类构造函数中来标识是inbound还是outbound。