java netty之ChannelPipeline

来源:互联网 发布:闻战网络歌手 编辑:程序博客网 时间:2024/05/17 02:53

好像在前面的文章中,把netty定义的channel基本上都还算分析了一下吧。。但是channel部分空缺了一个比较重要的东西没有分析,那就是pipeline,所有的操作都是通过pipeline来进行的。。。

还是按照惯例,先来看看ChannelPipeline的继承体系:

这里看到的是三个最顶层的接口,ChannelPipeline同时继承了ChannelInboundInvoker和ChannelOutboundInvoker,

首先我们先来看看ChannelInboundInvoker吧,看这个接口的名字其实基本上就能知道它是干嘛的了,顾名思义就是当channel有数据进来的时候需要用它来处理。。。

//当channel的一些状态发生变化的时候,会调用里面的一些对应的方法interface ChannelInboundInvoker {    /**     * A {@link Channel} was registered to its {@link EventLoop}.     *     * This will result in having the  {@link ChannelStateHandler#channelRegistered(ChannelHandlerContext)} method     * called of the next  {@link ChannelStateHandler} contained in the  {@link ChannelPipeline} of the     * {@link Channel}.     *///当当前的channel注册到eventloop后会调用,最终会调用链表的第一个ChannelStateHandler的channelRegistered函数    ChannelInboundInvoker fireChannelRegistered();    /**     * A {@link Channel} was unregistered from its {@link EventLoop}.     *     * This will result in having the  {@link ChannelStateHandler#channelUnregistered(ChannelHandlerContext)} method     * called of the next  {@link ChannelStateHandler} contained in the  {@link ChannelPipeline} of the     * {@link Channel}.     */    //当所属的channel unregister的时候调用    ChannelInboundInvoker fireChannelUnregistered();    /**     * A {@link Channel} is active now, which means it is connected.     *     * This will result in having the  {@link ChannelStateHandler#channelActive(ChannelHandlerContext)} method     * called of the next  {@link ChannelStateHandler} contained in the  {@link ChannelPipeline} of the     * {@link Channel}.     */    ChannelInboundInvoker fireChannelActive();    /**     * A {@link Channel} is inactive now, which means it is closed.     *     * This will result in having the  {@link ChannelStateHandler#channelInactive(ChannelHandlerContext)} method     * called of the next  {@link ChannelStateHandler} contained in the  {@link ChannelPipeline} of the     * {@link Channel}.     */    ChannelInboundInvoker fireChannelInactive();    /**     * A {@link Channel} received an {@link Throwable} in one of its inbound operations.     *     * This will result in having the  {@link ChannelStateHandler#exceptionCaught(ChannelHandlerContext, Throwable)}     * method  called of the next  {@link ChannelStateHandler} contained in the  {@link ChannelPipeline} of the     * {@link Channel}.     */    ChannelInboundInvoker fireExceptionCaught(Throwable cause);    /**     * A {@link Channel} received an user defined event.     *     * This will result in having the  {@link ChannelStateHandler#userEventTriggered(ChannelHandlerContext, Object)}     * method  called of the next  {@link ChannelStateHandler} contained in the  {@link ChannelPipeline} of the     * {@link Channel}.     */    ChannelInboundInvoker fireUserEventTriggered(Object event);    /**     * A {@link Channel} received bytes which are now ready to read from its inbound buffer.     *     * This will result in having the  {@link ChannelStateHandler#inboundBufferUpdated(ChannelHandlerContext)}     * method  called of the next  {@link ChannelStateHandler} contained in the  {@link ChannelPipeline} of the     * {@link Channel}.     */    //已经有数据到了buffer里面可以读取了,最终会调用inboundBufferUpdated方法,用于处理数据    ChannelInboundInvoker fireInboundBufferUpdated();    /**     * Triggers an {@link ChannelStateHandler#channelReadSuspended(ChannelHandlerContext) channelReadSuspended}     * event to the next {@link ChannelStateHandler} in the {@link ChannelPipeline}.     */    ChannelInboundInvoker fireChannelReadSuspended();}
定义还是很简单,无非都是当channel的状态发生改变的时候,调用channel的stathandler相应的方法。。。这里有一个比较重要的方法:
    //已经有数据到了buffer里面可以读取了,最终会调用inboundBufferUpdated方法,用于处理数据    ChannelInboundInvoker fireInboundBufferUpdated();
它是在当已经有数据读取了,并保存到了buffer之后会调用的方法,用于找到整个handler链的第一个inboundhandler同时也是stathandler的inbountBufferUpdated方法来处理这些数据


接下来来看ChannelOutboundInvoker,顾名思义就是用来处理一些出口数据的,其实也就是一些数据的发送,当然还有connect之类的,这里就不列它的代码了,主要是有一些connect,write,sendfile等方法,其实最终这些都是调用pipeline上的operationhandler来进行具体的操作的。。。。


最后我们来看ChannelPipeline接口的定义,首先它既是inboundinvoker也是outboundinvoker,同时它最终又是一个channelhandler的管理器,在其中的定义的方法主要是对这些handler进行维护的方法,例如:

    //将handler放到最靠前的位置    ChannelPipeline addFirst(String name, ChannelHandler handler);    /**     * Inserts a {@link ChannelHandler} at the first position of this pipeline.     *     * @param group    the {@link EventExecutorGroup} which will be used to execute the {@link ChannelHandler}     *                 methods     * @param name     the name of the handler to insert first     * @param handler  the handler to insert first     *     * @throws IllegalArgumentException     *         if there's an entry with the same name already in the pipeline     * @throws NullPointerException     *         if the specified name or handler is {@code null}     */    //多了一个executor,这里表示handler由这个executor执行    ChannelPipeline addFirst(EventExecutorGroup group, String name, ChannelHandler handler);


每一个channel都会分配一个pipeline对象用于维护当前channel的handler,这些handler又分为两种类型,

(1)inboundhandler,同时也是stathandler,他们主要是在channel的状态发生改变的时候用的,例如当有数据已经读取了,那么会调用相应的方法来处理数据

(2)outboundhandler,同时也是operationhandler,这些handler则是用于具体的数据发送等操作,例如connect,write等。。。。


好了,对pipeline的简单的分析就差不多了。。。可以给它下一个定义:它是数据出入口的处理器,同时还要管理用户定义的handler。。。。