{/**
* @param parent the parent of this channel. {@code null} if there's no parent.
*/protectedAbstractChannel(Channel parent){this.parent = parent;//1.创建ChannelId对象,对应Channel编号
id =newId();//2.创建Unsafe对象,使用在Channel的生命周期
unsafe =newUnsafe();//3.创建ChannelPipeline对象,是子类DefaultChannelPipeline的实例
pipeline =newChannelPipeline();}/**
* @param parent the parent of this channel. {@code null} if there's no parent.
*/protectedAbstractChannel(Channel parent, ChannelId id){this.parent = parent;this.id = id;
unsafe =newUnsafe();
pipeline =newChannelPipeline();}}
@Overridepublicfinal SocketAddress localAddress(){returnlocalAddress0();}@Overridepublicfinal SocketAddress remoteAddress(){returnremoteAddress0();}/**
* Returns the {@link SocketAddress} which is bound locally.
* 返回本地绑定的SocketAddress
*/protectedabstract SocketAddress localAddress0();/**
* Return the {@link SocketAddress} which the {@link Channel} is connected to.
* 返回Channel连接的远程地址
*/protectedabstract SocketAddress remoteAddress0();
@Overridepublicfinalvoidregister(EventLoop eventLoop,final ChannelPromise promise){//1.校验传入的eventLoop参数吗,不能为空if(eventLoop == null){thrownewNullPointerException("eventLoop");}//2.校验未注册,已经注册了就返回if(isRegistered()){
promise.setFailure(newIllegalStateException("registered to an event loop already"));return;}//3.校验Channel和eventLoop匹配兼容,isCompatible方法需要子类实现if(!isCompatible(eventLoop)){
promise.setFailure(newIllegalStateException("incompatible event loop type: "+ eventLoop.getClass().getName()));return;}//4.设置Channel的eventLoop属性,将二者关联起来
AbstractChannel.this.eventLoop = eventLoop;//5.在EventLoop中执行注册逻辑,如果当前线程就在EventLoop中,就直接注册,否则使用eventLoop线程池注册if(eventLoop.inEventLoop()){//6.直接注册register0(promise);}else{//7.线程池注册try{
eventLoop.execute(newRunnable(){@Overridepublicvoidrun(){
System.out.println(Thread.currentThread()+": register");register0(promise);}});}catch(Throwable t){//异常处理
logger.warn("Force-closing a channel whose registration task was not accepted by an event loop: {}", AbstractChannel.this, t);closeForcibly();
closeFuture.setClosed();safeSetFailure(promise, t);}}}
register0方法
privatevoidregister0(ChannelPromise promise){try{// check if the channel is still open as it could be closed in the mean time when the register// call was outside of the eventLoop//1.判断channel仍然是打开的,反之就返回if(!promise.setUncancellable()// TODO 1001 Promise||!ensureOpen(promise)){return;}//2.记录是否为首次注册boolean firstRegistration = neverRegistered;//3.执行注册逻辑,原语操作,交给子类去实现doRegister();//4.标记首次注册为false
neverRegistered =false;//5.标记Channel为已注册
registered =true;// Ensure we call handlerAdded(...) before we actually notify the promise. This is needed as the// user may already fire events through the pipeline in the ChannelFutureListener.//6.在通知promise之前调用handlerAdded,因为用户有可能在ChannelFutureListener通过pipeline fire事件(现在还不甚理解)
pipeline.invokeHandlerAddedIfNeeded();//7.回调通知promise执行成功safeSetSuccess(promise);//8.触发通知已注册事件
pipeline.fireChannelRegistered();//9.首次注册的话,就fire一个channelActive,这样如果是注销再注册的话就不会触发多个channel actives事件// Only fire a channelActive if the channel has never been registered. This prevents firing// multiple channel actives if the channel is deregistered and re-registered.if(isActive()){if(firstRegistration){
pipeline.fireChannelActive();}elseif(config().isAutoRead()){// This channel was registered before and autoRead() is set. This means we need to begin read// again so that we process inbound data.//// See https://github.com/netty/netty/issues/4805beginRead();}}}catch(Throwable t){//异常处理// Close the channel directly to avoid FD leak.closeForcibly();
closeFuture.setClosed();safeSetFailure(promise, t);}}