private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
// 1.通过反射工厂创建该对象
public NioServerSocketChannel() {
// 1.1 newSocket
// 1.2 this
this(newSocket(DEFAULT_SELECTOR_PROVIDER));
}
// 1.2 构造器
public NioServerSocketChannel(ServerSocketChannel channel) {
// channel = sun.nio.ch.ServerSocketChannelImpl
// 3.调用父类(AbstractNioMessageChannel/AbstractNioChannel/AbstractChannel)的构造器
super(null, channel, SelectionKey.OP_ACCEPT);
config = new NioServerSocketChannelConfig(this, javaChannel().socket());
}
// 1.1 创建:ServerSocketChannel
private static ServerSocketChannel newSocket(SelectorProvider provider) {
try {
return provider.openServerSocketChannel();
} catch (IOException e) {
throw new ChannelException("Failed to open a server socket.", e);
}
} //end newSocket
protected AbstractNioMessageChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
// parent = null
// ch = sun.nio.ch.ServerSocketChannelImpl
// readInterestOp = SelectionKey.OP_ACCEPT(16)
// 4. 调用父类(AbstractNioChannel)的构造器
super(parent, ch, readInterestOp);
}
protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
// parent = null
// ch = sun.nio.ch.ServerSocketChannelImpl
// readInterestOp = SelectionKey.OP_ACCEPT(16)
// 5.调用父类(AbstractChannel)构造器
super(parent);
this.ch = ch;
this.readInterestOp = readInterestOp;
try {
// 设置非阻塞
ch.configureBlocking(false);
} catch (IOException e) {
try {
// 有异常的情况下关闭
ch.close();
} catch (IOException e2) {
logger.warn("Failed to close a partially initialized socket.", e2);
}
throw new ChannelException("Failed to enter non-blocking mode.", e);
} // end catch
}
protected AbstractChannel(Channel parent) {
// parent = null
this.parent = parent;
// 创建ChannelId
id = newId();
// 6.调用AbstractNioMessageChannel.newUnsafe() 创建Channel$Unsafe
unsafe = newUnsafe();
// 创建DefaultChannelPipeline
pipeline = newChannelPipeline();
}
// 创建默认的ChannelPipeline
protected DefaultChannelPipeline newChannelPipeline() {
return new DefaultChannelPipeline(this);
}
// AbstractNioMessageChannel
protected AbstractNioUnsafe newUnsafe() {
return new NioMessageUnsafe();
}
1. 创建:ServerSocketChannel(sun.nio.ch.ServerSocketChannelImpl)
2. 创建:Unsafe对象(NioMessageUnsafe)
3. 创建:DefaultChannelPipeline对象