ChannelHandler的添加与删除

本文详细分析了Netty中ChannelHandler的添加与删除过程。在添加时,涉及验证是否重复添加、创建HandlerContext对象、将context添加到链表及调用回调方法通知成功。删除时,通过remove方法,获取HandlerContext、判断节点位置、执行删除操作并回调通知成功。整个流程清晰明了。
摘要由CSDN通过智能技术生成

上篇文章中,我们对Netty中ChannelPipeline的构造与初始化进行了分析与总结,本篇文章我们将对ChannelHandler的添加与删除操作进行具体的的代码分析;

一、ChannelHandler的添加

下面是Netty官方的一段demo源码,可以看到在服务端初始化时执行了向ChannelPipeline中添加自定义channelHandler的操作。

复制代码

            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
                    .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            if (sslCtx != null) {
                                p.addLast(sslCtx.newHandler(ch.alloc()));
                            }
                            // p.addLast(new LoggingHandler(LogLevel.INFO));
                            // 向ChannelPipeline中添加自定义channelHandler
                            p.addLast(serverHandler);
                        }
                    });

复制代码

我们可以看到上面的代码中调用ChannelPipeline的addLast方法实现了channelHandler的添加,下面我们就分析下addLast方法的具体源码实现

首先看下addLast方方法的具体源码实现

复制代码

    public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) {
        final AbstractChannelHandlerContext newCtx;
        synchronized (this) {
            //判断handler是否被重复添加
            checkMultiplicity(handler);

            //创建ChannelHandlerContext节点  filterName检查名称是否重复
            newCtx = newContext(group, filterName(name, handler), handler);

            //双向链表中增加ChannelHandlerContext
            addLast0(newCtx);

            // If the registered is false it means that the channel was not registered on an eventLoop yet.
            // In this case we add the context to the pipeline and add a task that will call
            // ChannelHandler.handlerAdded(...) once the channel is registered.
            if (!registered) {
                newCtx.setAddPending();
                callHandlerCallbackLater(newCtx, true);
                return this;
            }

            EventExecutor executor = newCtx.executor();
            if (!executor.inEventLoop()) {//判断是否在同一线程中
                callHandlerAddedInEventLoop(newCtx, executor);
                return this;
            }
        }
        callHandlerAdded0(newCtx);
        return this;
    }

复制代码

分析addLast方法代码可以看到,ChannelHandler的添加基本可以分为四步

1、验证ChannelHandl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值