Netty 系列目录(http://www.javashuo.com/article/p-hskusway-em.html)html
chnnel 初始化完成后就须要将其注册到对应的 NioEventLoop 上。java
(1) NioEventLoopGroup 注册promise
// NioEventLoopGroup -> MultithreadEventLoopGroup public ChannelFuture register(Channel channel) { return next().register(channel); }
group() 返回的是上文配置的 childGroup 对象,即 NioEventLoopGroup,每一个 NioEventLoopGroup 持有多个 NioEventLoop,next 依次返回一个 NioEventLoop。缓存
(2) NioEventLoop 注册多线程
// NioEventLoop -> SingleThreadEventLoop public ChannelFuture register(Channel channel) { return register(new DefaultChannelPromise(channel, this)); } public ChannelFuture register(final ChannelPromise promise) { ObjectUtil.checkNotNull(promise, "promise"); promise.channel().unsafe().register(this, promise); return promise; }
能够看到最终调用对应 channel 的 unsafe 进行注册,下面看一下 unsafe 是如何注册的。app
(3) Unsafe 注册ide
NioServerSocketChannel 的 unsafe = new NioMessageUnsafe(),而 NioMessageUnsafe 继承自 AbstractUnsafe。oop
// NioServerSocketChannel -> AbstractChannel.AbstractUnsafe public final void register(EventLoop eventLoop, final ChannelPromise promise) { // 省略... AbstractChannel.this.eventLoop = eventLoop; // 同一个 channel 的注册、读、写等都在 eventLoop 完成,避免多线程的锁竞争 if (eventLoop.inEventLoop()) { // 将 channel 注册到 eventLoop 上 register0(promise); } else { try { eventLoop.execute(new Runnable() { @Override public void run() { register0(promise); } }); } catch (Throwable t) { // 省略... } } } private void register0(ChannelPromise promise) { // 1. 确保 channel 的状态是 open,最终调用 ch.isOpen() if (!promise.setUncancellable() || !ensureOpen(promise)) { return; } boolean firstRegistration = neverRegistered; // 2. channel 注册到 eventLoop 上 doRegister(); neverRegistered = false; registered = true; // 3. 此时 channel 已经注册到 eventLoop 上,此时须要将注册的 handler 绑定到 channel 上。eg: ChannelInitializer pipeline.invokeHandlerAddedIfNeeded(); safeSetSuccess(promise); pipeline.fireChannelRegistered(); // 4. channel 状态为 active 就须要触发 ChannelActive 事件或准备读 if (isActive()) { if (firstRegistration) { pipeline.fireChannelActive(); } else if (config().isAutoRead()) { beginRead(); } } }
register 中最重要的方法是调用 doRegister 进行注册,该方法调用了 JDK 底层的代码进行注册。this
(4) doRegister线程
// AbstractNioChannel protected void doRegister() throws Exception { boolean selected = false; for (;;) { try { // 1. 调用 JDK NIO 将 channel 注册到 eventLoop 的 selector 上 selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this); return; } catch (CancelledKeyException e) { if (!selected) { // 2. 调用 selectNow 将注册的 channel 移除,否则有可能被缓存没有删除 // 移除后继续注册 eventLoop().selectNow(); selected = true; } else { // 3. JDK 提供的文档此时不会出现该异常,实际... JDK bug throw e; } } } }
AbstractNioChannel#javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
register 方法注册 Java 原生 NIO 的 Channel 对象到 Selector 对象。可是为何感兴趣事件的是 0 呢?正常状况下,对于服务端来讲,须要注册 SelectionKey.OP_ACCEPT 事件。
(1) 注册方式是多态的,它便可以被 NIOServerSocketChannel 用来监听客户端的链接接入,也能够注册 SocketChannel 用来监听冉莹颖读或者写操做。
(2) 经过 SelectionKey#interestOps(int ops) 方法能够方便地修改监听操做位。因此,此处注册须要获取 SelectionKey 并给 AbstractNIOChannel 的成员变量 selectionKey 赋值。
// AbstractChannel.AbstractUnsafe public final void beginRead() { assertEventLoop(); if (!isActive()) { return; } try { doBeginRead(); } catch (final Exception e) { invokeLater(new Runnable() { @Override public void run() { pipeline.fireExceptionCaught(e); } }); close(voidPromise()); } } // AbstractNioChannel protected void doBeginRead() throws Exception { // Channel.read() or ChannelHandlerContext.read() was called final SelectionKey selectionKey = this.selectionKey; if (!selectionKey.isValid()) { return; } readPending = true; // 从新注册感兴趣的事件类型,readInterestOp 是 channel 初始化的时候传进来的 final int interestOps = selectionKey.interestOps(); if ((interestOps & readInterestOp) == 0) { selectionKey.interestOps(interestOps | readInterestOp); } }
到此 NioServerSocketChannel 就从新注册上了 OP_ACCEPT 事件
天天用心记录一点点。内容也许不重要,但习惯很重要!