//Channel是socket的连接点
public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparable<Channel>
所有的事件都是由channelPiple来处理的,具体的实现是EventHandler,channelPiple将netty中若干的EventHandler组合起来,通过他产生的链条,来处理所有的io事件以及用户发来的请求
ChannelPipeline pipeline();
返回这个channel注册到其上面的eventLoop对象
EventLoop eventLoop();
返回它的父channel,如果没有就return null
Channel parent();
netty中的所有操作都是异步的,最终的操作结果由channelFuture来获取
在channelPiple父类创建的时候 pipeline对象被创建
public abstract class AbstractChannel extends DefaultAttributeMap implements Channel
private final DefaultChannelPipeline pipeline;
protected AbstractChannel(Channel parent) {
this.parent = parent;
id = newId();
unsafe = newUnsafe();
pipeline = newChannelPipeline();
}
是一串chanelHandler的链表,会处理入栈的事件,以及出去的操作实现了一种高级的拦截过滤器模式 ,传统的过滤器,请求和响应都是一个,而这种高级的将出栈和入栈分开了
具体的事件是由ChannelPipeline中的一个个chanelHandler节点所处理.一个I/O event 要么是由ChannelOutBoundHandler处理要么是由ChannelInBoundHandler处理,一个事件处理完之后会把它扔给与之最近的下一个Handler
public interface ChannelPipeline
extends ChannelInboundInvoker, ChannelOutboundInvoker, Iterable<Entry<String, ChannelHandler>>
* 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) |
* +-------------------------------------------------------------------+