netty ServerBootstrap 之Acceptor

本文说明下ServerBootstrapAcceptor

ServerBootstrapAcceptor  extends ChannelInboundHandlerAdapter

它作为ServerBootStrap 的内部类。

它本身就是个Channelhandler,因为是继承ChannelInboundhandlerAdapter ,它复写了其中的方法channelRead();

且是作为NioServerSocketChannel的ChannelPipeline中的一个handler,它的作用就是在selector.select()后,对SelectionKey.OP_ACCEPT 事件作为一个响应。

主要流程:在NioEventLoop  方法 processSelectedKey(...)  对应策略selectionKey &SelectionKey.ACCEPT != 0时,会调用对应channel内部封装功能Unsafe的read()方法,其流程就是通过pipeline 和ChannelContext 找到已经Add_COMPLEt 的handler,且对应的MARSK 相匹配,也就是ServerBootstrapAcceptor handler上面的channelread()方法。这个时候通过复用,NioEventExecutorGroup 对应的register(channel) 方法,对SocketChannel封装成NIoSocketChannel 且对SocketChannel进行register(selector,op_action,attchment)。因为是对ServerSocketChannel得到的SelectionKey进行iteration,所以,此时会进行多线程操作。对于对应的事件给予响应。

 

当然对应的他会将childHandler 添加到每一个NioSocketChannel的DefaultChannelPipeline中DefaultChannelHandlerContext 的handler上面。 且在注册的时候,给予每个NioSocketChannel 对应的NioEventLoop。

 

posted @ 2020-09-11 18:47  serviceOBJ  阅读(335)  评论(0)    收藏  举报