UML类图——Netty原理
2018-03-25 12:46:07 0 举报
Netty3 源码分析
作者其他创作
大纲/内容
Binder
- volatile ChannelHandler parentHandler
public ServerBootstrap()public ServerBootstrap(ChannelFactory channelFactory)+ void setFactory(ChannelFactory factory) + void setPipelineFactory(ChannelPipelineFactory pipelineFactory)+ Channel bind(final SocketAddress localAddress) + ChannelFuture bindAsync(final SocketAddress localAddress)
NioWorker
AbstractNioWorkerPool
-final AtomicBoolean initialized; -- 是否初始化Worker数组,避免重复-Executor workerExecutor; --worker线程-AbstractNioWorker[] workers; --Worker数组-AtomicInteger workerIndex = new AtomicInteger(); -- Worker 数组轮询选定
NioSelector
BossPool
+ E nextBoss() 获取下一个Boss
AbstractNioWorker 讲解
AbstractNioSelector
-static final AtomicInteger nextId = new AtomicInteger()-Executor execute; --线程用于执行run方法+protected volatile Thread thread; Refer to 启动Run的线程- CountDownLatch startupLatch = new CountDownLatch(1)+volatile Selector selector --多路复用的Selector nio+protected final AtomicBoolean wakenUp = new AtomicBoolean(); 避免selector.select() 堵塞 select 操作,例如 reg+ Queue taskQueue = new ConcurrentLinkedQueue() -- run 方法中执行的Task
ChannelPipeline
ServerBootstrap
DefaultChannelPipeline ---------------------------------------------------------------------------------------------- private DefaultChannelHandlerContext getActualUpstreamContext(DefaultChannelHandlerContext ctx) if (ctx == null) { return null; } DefaultChannelHandlerContext realCtx = ctx; while (!realCtx.canHandleUpstream()) { realCtx = realCtx.next; if (realCtx == null) { return null; } } return realCtx; -------------------------------------------------------------------------------------------------private DefaultChannelHandlerContext getActualDownstreamContext(DefaultChannelHandlerContext ctx) if (ctx == null) { return null; } DefaultChannelHandlerContext realCtx = ctx; while (!realCtx.canHandleDownstream()) { realCtx = realCtx.prev; if (realCtx == null) { return null; } } return realCtx;
DefaultChannelHandlerContext
- volatile DefaultChannelHandlerContext next;- volatile DefaultChannelHandlerContext prev;- final String name; - final ChannelHandler handler; - final boolean canHandleUpstream; - final boolean canHandleDownstream; - volatile Object attachment;
RegisterTask implements Runnable -- 在NioWorker Class 内部
private final NioServerSocketChannel channel;private final ChannelFuture future;private final SocketAddress localAddress;
AbstractNioChannelSink
ThreadRenamingRunnable----------------------------------------------------------------------------------------------run() final Thread currentThread = Thread.currentThread(); final String oldThreadName = currentThread.getName(); final String newThreadName = getNewThreadName(oldThreadName); currentThread.setName(newThreadName); try { runnable.run(); - 启动run 方法 } finally { if (renamed) { // Revert the name back if the current thread was renamed. // We do not check the exception here because we know it works. // 值得大家学习 currentThread.setName(oldThreadName); } }
AbstractNioBossPool
-final AtomicBoolean initialized; -- 是否初始化Boss数组,避免重复-Executor bossExecutor; --Boss 线程-Boss[] bosses; --Boss数组-AtomicInteger bossIndex = new AtomicInteger(); -- Boss 数组轮询选定
ServerBootstrap---------------------------------------------------------------------------------------------- public Channel bind(final SocketAddress localAddress) ChannelFuture future = bindAsync(localAddress); // Wait for the future. future.awaitUninterruptibly(); if (!future.isSuccess()) { future.getChannel().close().awaitUninterruptibly(); throw new ChannelException(\"Failed to bind to: \
ChannelSink
Runnable
+ run()
Boss
NioServerSocketChannelFactory
- WorkerPool workerPool- NioServerSocketPipelineSink sink- BossPool bossPool
NioServerBoss
DefaultChannelPipeline
serverBootstrap.bind(new InetSocketAddress(port))
ServerBootstrap serverBootstrap = new ServerBootstrap();
private final NioSocketChannel channel;private final ChannelFuture future;private final boolean server;
serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {\t\t\tpublic ChannelPipeline getPipeline() throws Exception {\t\t\t\tChannelPipeline channelPipeline = Channels.pipeline();\t\t\t\tchannelPipeline.addLast(\"Decoder\
AbstractNioSelector类内部讲解
NioServerSocketPipelineSink
AbstractNioWorker
Bootstrap
protected Bootstrap()protected Bootstrap(ChannelFactory channelFactory)+ void setFactory(ChannelFactory factory)+ void setPipelineFactory(ChannelPipelineFactory pipelineFactory)
ChannelHandlerContext
+ Channel getChannel()+ ChannelPipeline getPipeline()+ ChannelHandler getHandler()+ boolean canHandleUpstream()+ boolean canHandleDownstream()+ void sendUpstream(ChannelEvent e)+ void sendDownstream(ChannelEvent e)+ void setAttachment(Object attachment)
AbstractChannelSink
NioWorker
private final SocketReceiveBufferAllocator recvBufferPool = new SocketReceiveBufferAllocator()
NioWorkerPool
// use for name the threads, if you not want to set one explicit.-final ThreadNameDeterminer determiner;
Worker
+ void executeInIoThread(Runnable task)
WorkerPool
+ void executeInIoThread(Runnable task)
NioServerBossPool
ServerSocketChannelFactory
+ ServerSocketChannel newChannel(ChannelPipeline pipeline)
ThreadRenamingRunnable
- final Runnable runnable- final String proposedThreadName- final ThreadNameDeterminer determiner
ChannelFactory
+ Channel newChannel(ChannelPipeline pipeline)+ void shutdown()
收藏
0 条评论
下一页