netty pipeline.addLast

本文深入解析Netty中DefaultChannelPipeline的addLast方法,探讨如何在处理器链尾部添加ChannelHandler,以及添加过程中涉及的关键步骤和技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pipeline有一个主要的实现类 DefaultChannelPipeline ,addLast顾名思义,就是在处理器链的最后添加一个channelHandler。

代码如下:@Override    public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) {

final AbstractChannelHandlerContext newCtx;
        synchronized (this) {
            checkMultiplicity(handler);
------------用channelHandlerContext包装这个handler
            newCtx = newContext(group, filterName(name, handler), handler);
------------这个方法其实没啥,简单的添加到后头
            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) {
---------------把ChannelHandlerContext中的handlerState这个volatile属性,从INIT状态设置成ADD_PENDING,意思是表明这个handler的状态从初始变成等待添加 newCtx.setAddPending(); callHandlerCallbackLater(newCtx,
true); return this; } EventExecutor executor = newCtx.executor(); if (!executor.inEventLoop()) { newCtx.setAddPending();
--------------将任务添加到NioEventLoop的队列中,等待执行,异步?
executor.execute(
new Runnable() { @Override public void run() {
--------------这个方法,其实有两个逻辑:第一,处理handler被添加到处理器链的时候,要做的准备工作,因为只有做完了这些准备工作,才能处理数据。第二,把HandlerContext的handlerState设置成ADD_COMPLETE,不过,异步完成,会不会逻辑有问题?或者在channel开始
处理数据之前,会检查判断?在注册之后会有一个invokeHandlerAddedIfNeeded,执行所有放入队列的相关任务。
--------------handlerAdded这个名字的意思并不是添加handler,添加handler这个步骤在addlast0里做过了,而是让handler做被添加之后的准备工作
--------------handlerContext的fire/invoke channelRegistered/active/inactive/unregister/read 都是handlerContext对channel时间的相应,比如channel被生成,初始化了,他所属的handler要被add,然后执行handleradded方法,为channel的处理做准备
,接下来,channel被registered了,又要用context执行channelRegistered方法,这些逻辑其实有的都是重复的,都是为了channel的工作做准备。
callHandlerAdded0(newCtx); } });
return this; } } callHandlerAdded0(newCtx); return this; }

 

转载于:https://www.cnblogs.com/chuliang/p/8308855.html

public class TlaChannelInitializer extends ChannelInitializer<AbstractChannel> { private final TlaClientConfig config; private final TlaSessionContext channelSession; public TlaChannelInitializer(TlaClientConfig config, TlaSessionContextWrapper.TlaSessionCallback callback) { if (config == null) { throw new IllegalArgumentException("The TlaClientConfig config must be non-null"); } this.config = config; TlaSessionContextWrapper nocSessionContext = config.getNocSessionContext(); if (nocSessionContext != null) { nocSessionContext.setSessionCallback(callback); } TlaSessionContextWrapper spake2pSessionContext = config.getSpake2pSessionContext(); if (spake2pSessionContext != null) { spake2pSessionContext.setSessionCallback(callback); } this.channelSession = new TlaSessionContextProxy(generateTlaSessionContextWrapper(config)); } private List<TlaSessionContextWrapper> generateTlaSessionContextWrapper(TlaClientConfig config) { List<TlaSessionContextWrapper> tlaSessionContextWrapperList = new ArrayList<>(); List<String> authTypePriorityList = config.getAuthTypePriorityList(); if (config.getAuthTypePriorityList() == null || config.getAuthTypePriorityList().isEmpty()) { return tlaSessionContextWrapperList; } for (String authType : authTypePriorityList) { TlaSessionContextWrapper tlaSessionContextWrapper = getTlaSessionContextWrapper(authType, config); if (tlaSessionContextWrapper != null) { tlaSessionContextWrapperList.add(tlaSessionContextWrapper); } } return tlaSessionContextWrapperList; } private TlaSessionContextWrapper getTlaSessionContextWrapper(String authType, TlaClientConfig config) { if (authType == null || authType.isEmpty()) { return null; } if (TlaClientConfig.AUTH_NOC.equals(authType)) { return config.getNocSessionContext(); } else { return config.getSpake2pSessionContext(); } } @Override protected void initChannel(AbstractChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // 添加日志处理器 pipeline.addLast(new LoggingHandler(TlaClient.class, LogLevel.INFO)); SslContextBuilder sslContextBuilder = SslContextBuilder.forClient(); // 蓝牙属于无tls层 if (ch instanceof NioSocketChannel) { if (config.getTlsCertificate() == null) { sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE); } else { sslContextBuilder.trustManager(new TlaTrustManager(config.getTlsCertificate(), config.getTlsServerId())); } SslContext sslContext = sslContextBuilder.build(); pipeline.addLast(sslContext.newHandler(ch.alloc())); } // 添加编解码器 pipeline.addLast(new TslpPacketDecoder()); pipeline.addLast(new TslpSessionDecoder()); pipeline.addLast(new TslpPacketEncoder()); pipeline.addLast(new TslpSessionEncoder()); pipeline.addLast(new TlaChannelHandler(channelSession)); } public TlaClientConfig getConfig() { return config; } public TlaSessionContext getChannelSession() { return channelSession; } } 这些代码逻辑是什么
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值