NioServerSocketChannel

io.netty.channel.socket.nio.NioServerSocketChannel

继承图

NioServerSocketChannel_java

NioServerSocketChannel 方法

NioServerSocketChannel_ide_02

构造函数

public NioServerSocketChannel() {
this(newSocket(DEFAULT_SELECTOR_PROVIDER));
}

newSocket

打开ServerSocketChannel

private static ServerSocketChannel newSocket(SelectorProvider provider) {
try {
/**
* Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in
* {@link SelectorProvider#provider()} which is called by each ServerSocketChannel.open() otherwise.
*
* See <a href="https://github.com/netty/netty/issues/2308">#2308</a>.
*/
return provider.openServerSocketChannel();
} catch (IOException e) {
throw new ChannelException(
"Failed to open a server socket.", e);
}
}

javaChannel

private final SelectableChannel ch;
protected SelectableChannel javaChannel() {
return ch;
}
protected ServerSocketChannel javaChannel() {
return (ServerSocketChannel) super.javaChannel();
}

bind

protected void doBind(SocketAddress localAddress) throws Exception {
if (PlatformDependent.javaVersion() >= 7) {
javaChannel().bind(localAddress, config.getBacklog());
} else {
javaChannel().socket().bind(localAddress, config.getBacklog());
}
}

doClose()

protected void doClose() throws Exception {
javaChannel().close();
}

doReadMessages

protected int doReadMessages(List<Object> buf) throws Exception {
SocketChannel ch = SocketUtils.accept(javaChannel());

try {
if (ch != null) {
buf.add(new NioSocketChannel(this, ch));
return 1;
}
} catch (Throwable t) {
logger.warn("Failed to create a new channel from an accepted socket.", t);

try {
ch.close();
} catch (Throwable t2) {
logger.warn("Failed to close a socket.", t2);
}
}

return 0;
}