Skip to content

簡介

  • NioServerSocketChannel

內容

NioServerSocketChannel create and start

  • io.netty.bootstrap.AbstractBootstrap#doBind()
  • final ChannelFuture regFuture = initAndRegister();
    • channel = channelFactory.newChannel();
    • pipeline
    • init(channel)
    • options0
    • attrs0()
    • currentChildOptions
    • currentChildAttrs
    • pipeline.addLast(new ServerBootstrapAcceptor(ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
    • registor
    • eventloop
    • registor0
      • doRegister
      • invokeHandlerAddedIfNeeded
      • fireChannelRegistered
    • AbstractUnsafe.doBind
    • doBind
      • javaChannel.bind
    • pipeline.fireChannelActived

ServerBootstrap.bind

netty netty netty netty netty netty


NioEventLoop

netty - ThreadPerTaskExecutor(每次執行都會建立新的線程) - DefaultThreadFactory - FastThreadLocalThread(NioEventLoop的線程,內含有一個InternalThreadLocalMap) - InternalThreadLocalMap - InternalThreadLocalMap

netty netty netty

NioEventLoop啟動

netty

服務器啟動觸發

netty

accept(0 -> OP_ACCEPT)觸發

  • netty netty netty

註冊新連接(與accept(OP_ACCEPT->OP_CONNECT)共用邏輯)

netty netty - NioEventLoop#processSelectedKey netty netty

新连接NioEventLoop

  • 創建NioSocketChannel(pipline unsafe)
  • 綁定selector(attch=NioSocketChannel)
  • 註冊OP_READ netty netty

pipeline

  • 有多個ChannelHanderContext
  • head
  • tail

ChannelHanderContext

  • 有一個channel
  • 有一個hander
  • 有一個EventExecutor(NioEventLoop)
  • 有多個EvemtFire netty netty netty

參考資料