一、生成serversocketchannel
ServerBootstrap设置channel类型 bootstrap.channel(NioServerSocketChannel.class)时,ServerBootstrap的父类AbstractBootstrap的初始ChannelFactory的对象,ChannelFactory的作用是生成ServerSocketChannel对象,channel方法代码:
public B channel(Class<? extends C> channelClass) {
if (channelClass == null) {
throw new NullPointerException("channelClass");
}
return channelFactory(new BootstrapChannelFactory<C>(channelClass));
}
BootstrapChannelFactory为AbstractBootstrap的内部类。
二、将serverchannel注册到selector
ServerBootstrap在绑定端口(bootstrap.bind(8080))时,调用AbstractBootstrap的initAndRegister方法:
final ChannelFuture initAndRegister() { final Channel channel = channelFactory().newChannel();//(1) try { init(channel);//(2) } catch (Throwable t) { channel.unsafe().closeForcibly(); return channel.newFailedFuture(t); } ChannelPromise regPromise = channel.newPromise();//(3) group().register(channel, regPromise);//(4) if (regPromise.cause() != null) { if (channel.isRegistered()) { channel.close(); } else { channel.unsafe().closeForcibly(); } } return regPromise; }
1、调用ChannelFactory对象生成ServerBootstrap 的channel(Class<? extends C> channelClass)方法中设置的channelClass对象即NioServerSocketChannel对象,也就是Netty重新封装过的ServerSocketChannel对象,至于Netty为什么要封装ServerSocketChannel后面章节再写。
2、 初化NioServerSocketChannel对象,将我们在创建ServerBootstrap对象中设置的option 和attr值设置到NioServerSocketChannel对象中的config和arrs属性中。
3、生成ChannelPromise对象,这个对象主要是对channel的注册状态进行监听
4、 取eventLoop设置到channel中,并调用AbstractChannel$AbstractUnsafe的register0方法奖channel注册到eventLoop中的selector,并将ChannelPromise状态设置为成功:
private void register0(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 if (!ensureOpen(promise)) { return; } doRegister(); //将serverchannel注册到selector中 registered = true; promise.setSuccess(); pipeline.fireChannelRegistered(); if (isActive()) { pipeline.fireChannelActive(); } } catch (Throwable t) { // Close the channel directly to avoid FD leak. closeForcibly(); closeFuture.setClosed(); if (!promise.tryFailure(t)) { logger.warn( "Tried to fail the registration promise, but it is complete already. " + "Swallowing the cause of the registration failure:", t); } } }
三、绑定端口
通过initAndRegister方法将serverchannel注册到selector后调用doBind0方法注册端口
private static void doBind0( final ChannelFuture regFuture, final Channel channel, final SocketAddress localAddress, final ChannelPromise promise) { // This method is invoked before channelRegistered() is triggered. Give user handlers a chance to set up // the pipeline in its channelRegistered() implementation. channel.eventLoop().execute(new Runnable() { //(1) @Override public void run() { if (regFuture.isSuccess()) { channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE); } else { promise.setFailure(regFuture.cause()); } } }); }
相关推荐
一个netty的入门教程以及源码分析视频,适合刚学习的人
美团基础架构部闪电侠老师的netty源码分析视频
netty源码解析PDF,网络编程
Netty4.x源码分析详解
netty4 sources 源码
netty4所有的jar包和源码,资源丰富,值得下载。
Netty4.1的源码,欢迎大家下载。.............................................................................................................................................................................
netty源码
NIO客户端13 3.Netty源码分析16 3.1. 服务端创建16 3.1.1. 服务端启动辅助类ServerBootstrap16 3.1.2. NioServerSocketChannel 的注册21 3.1.3. 新的客户端接入25 3.2. 客户端创建28 3.2.1. 客户端连接辅助类...
netty_learn_netty_源码
netty4.1源码
基于EchoServer对Netty4.1源码进行分析,水平有限,如果有问题大家可以提出来共同分享。
NETTY架构源码剖析
Netty4&Netty5源码导入Eclipse会报错,原因为缺少Jar包,提提供给爱学习的你!有分给点,没分单密我,白送~
Netty权威指南源码
Netty5.0架构剖析和源码解读.pdf是一本比较全的讲解netty5的书籍,任何对java的nio技术感兴趣的人都值得一看。
Netty权威指南第二版官方源码及netty5源码,
Netty源码分析总结.rar
共分两大章:第1 章:深入浅出Netty源码剖析,第2 章:NIO+Netty5各种RPC架构实战演练,以及课程资料,希望对象学习netty的童靴有用。
Netty4源码深入剖析(高清视频教程) .rar