在前面一期我们讲了Netty服务端启动流程,本期就带大家看看服务端是如何处理客户端连接的流程。
我们直接从#NioEventLoop的processSelectedKeys()方法开始,此方法的被调用处在#NioEventLoop的run()方法中,熟悉NioEvetnLoop的朋友应该知道,所有的Io事件和任务队列中的任务,都是在run()方法中被执行到,所以run()是NioEvent Loop的核心方法,不熟悉的朋友也没事,后面会有单独篇章进行讲解,在这里只需要知道run()里面处理了什么即可。
#NioEventLoop的processSelectedKeys()
private void processSelectedKeys() {
if (selectedKeys != null) {
processSelectedKeysOptimized();
} else {
processSelectedKeysPlain(selector.selectedKeys());
}
}
private void processSelectedKeysOptimized() {
for (int i = 0; i < selectedKeys.size; ++i) {
final SelectionKey k = selectedKeys.keys[i];
selectedKeys.keys[i] = null;
final Object a = k.attachment();
if (a instanceof AbstractNioChannel) {
processSelectedKey(k, (AbstractNioChannel) a);
} else {
@SuppressWarnings("unchecked")
NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
processSelectedKey(k, task);
}
if (needsToSelectAgain) {
selectedKeys.reset(i + 1);
selectAgain();
i = -1;
}
}
}
private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
try {
int readyOps = k.readyOps();
if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
int ops = k.interestOps();
ops &= ~SelectionKey.OP_CONNECT;
k.interestOps(ops);
unsafe.finishConnect();
}
if ((readyOps & SelectionKey.OP_WRITE) != 0) {
ch.unsafe().forceFlush();
}
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
unsafe.read();
}
} catch (CancelledKeyException ignored) {
unsafe.close(unsafe.voidPromise());
}
}
#NioMessageUnsafe的read()
private final List<Object> readBuf = new ArrayList<Object>();
@Override
public void read() {
assert eventLoop().inEventLoop();
final ChannelConfig config = config();
final ChannelPipeline pipeline = pipeline();
final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
allocHandle.reset(config);
boolean closed = false;
Throwable exception = null;
try {
try {
do {
int localRead = doReadMessages(readBuf);
if (localRead == 0) {
break;
}
if (localRead < 0) {
closed = true;
break;
}
allocHandle.incMessagesRead(localRead);
} while (allocHandle.continueReading());
} catch (Throwable t) {
exception = t;
}
int size = readBuf.size();
for (int i = 0; i < size; i ++) {
readPending = false;
pipeline.fireChannelRead(readBuf.get(i));
}
readBuf.clear();
allocHandle.readComplete();
pipeline.fireChannelReadComplete();
if (exception != null) {
closed = closeOnReadError(exception);
pipeline.fireExceptionCaught(exception);
}
if (closed) {
inputShutdown = true;
if (isOpen()) {
close(voidPromise());
}
}
} finally {
if (!readPending && !config.isAutoRead()) {
removeReadOp();
}
}
}
#NioServerSocketChannel的doReadMessages()
@Override
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;
}
通过查看#NioMessageUnsafe的read()方法可以很清楚的知道,首先处理了接收事件,并将原生的SocketChannel包装为NioSocketChannel存放到readBuf中,然后向服务端通道(NioServerSocketChannel)传播channelRead事件。
这里一定要弄清楚服务端通道(NioServerSocketChannel)和客户端通道(NioSocketChannel)的区别,目前我们是处理客户端连接事件,用的是bossGroup,并且在NioServerSocketChannel中传播了正在连接的NioSocketChannel。
在服务端启动流程那一章里面,NioEventLoop执行的第二个任务大家还有印象吗?
ch.eventLoop().execute(new Runnable() {
@Override
public void run() {
pipeline.addLast(new ServerBootstrapAcceptor(ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
这里用示意图来描述(由于ServerBootstrapAcceptor是入站处理器,此处没有画出站箭头)
NioServerSocketChannel内部有一个pipeline,pipeline中有默认的HeadContext和TailContext,后来由NioEventLoop向pipeline中添加了一个处理器。
再回到#NioMessageUnsafe的read()方法,处理完accept事件后向通道传播channelRead事件,这是一个入站类型的事件,所以处理流程是HeadContext->ServerBootstrapAcceptor->TailContext,分别看这三个类的channelRead(),可以知道处理客户端连接事件的逻辑在ServerBootstrapAcceptor中。
#ServerBootstrapAcceptor的channelRead()
@Override
@SuppressWarnings("unchecked")
public void channelRead(ChannelHandlerContext ctx, Object msg) {
final Channel child = (Channel) msg;
child.pipeline().addLast(childHandler);
setChannelOptions(child, childOptions, logger);
setAttributes(child, childAttrs);
try {
childGroup.register(child).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
forceClose(child, future.cause());
}
}
});
} catch (Throwable t) {
forceClose(child, t);
}
}
看到这里,我们可以明白为什么说boss是处理客户端连接,worker是处理客户端消息的原因了。
childGroup.register(child)的流程和第一章的注册流程大致相同,在这里就简单的梳理一下。这个register最终调用到了#AbstractUnsafe的register()方法中,对服务端启动流程比较熟悉的朋友肯定有印象了,这里和客户端通道注册到workerGroup的NioEventLoop中是一样的。
@Override
public final void register(EventLoop eventLoop, final ChannelPromise promise) {
ObjectUtil.checkNotNull(eventLoop, "eventLoop");
if (isRegistered()) {
promise.setFailure(new IllegalStateException("registered to an event loop already"));
return;
}
if (!isCompatible(eventLoop)) {
promise.setFailure(
new IllegalStateException("incompatible event loop type: " + eventLoop.getClass().getName()));
return;
}
AbstractChannel.this.eventLoop = eventLoop;
if (eventLoop.inEventLoop()) {
register0(promise);
} else {
try {
eventLoop.execute(new Runnable() {
@Override
public void run() {
register0(promise);
}
});
} catch (Throwable t) {
logger.warn(
"Force-closing a channel whose registration task was not accepted by an event loop: {}",
AbstractChannel.this, t);
closeForcibly();
closeFuture.setClosed();
safeSetFailure(promise, t);
}
}
}
private void register0(ChannelPromise promise) {
try {
if (!promise.setUncancellable() || !ensureOpen(promise)) {
return;
}
boolean firstRegistration = neverRegistered;
doRegister();
neverRegistered = false;
registered = true;
pipeline.invokeHandlerAddedIfNeeded();
safeSetSuccess(promise);
pipeline.fireChannelRegistered();
if (isActive()) {
if (firstRegistration) {
pipeline.fireChannelActive();
} else if (config().isAutoRead()) {
beginRead();
}
}
} catch (Throwable t) {
closeForcibly();
closeFuture.setClosed();
safeSetFailure(promise, t);
}
}
#AbstractNioChannel的doRegister()
@Override
protected void doRegister() throws Exception {
boolean selected = false;
for (;;) {
try {
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
return;
} catch (CancelledKeyException e) {
if (!selected) {
eventLoop().selectNow();
selected = true;
} else {
throw e;
}
}
}
}
到这里就完成了worker与NioSocketChannel的绑定。然后向NioSocketChannel传播注册成功事件和channelActive事件。
在上一章服务端启动流程里,在NioServerSocketChannel中传播channelActive事件是提交了一个任务,在NioSocketChannel中是直接执行。
在客户端的pipeline里,还是看HeadContext的channelActive()。
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.fireChannelActive();
readIfIsAutoRead();
}
private void readIfIsAutoRead() {
if (channel.config().isAutoRead()) {
channel.read();
}
}
再回到HeadContext的read()方法中
@Override
public void read(ChannelHandlerContext ctx) {
unsafe.beginRead();
}
beginRead()最终会调用到#AbstractUnsafe里面
@Override
public final void beginRead() {
assertEventLoop();
if (!isActive()) {
return;
}
try {
doBeginRead();
} catch (final Exception e) {
invokeLater(new Runnable() {
@Override
public void run() {
pipeline.fireExceptionCaught(e);
}
});
close(voidPromise());
}
}
doBeginRead()方法也会有一系列的调用流程,最终来到#AbstractNioChannel的doBeginRead()
@Override
protected void doBeginRead() throws Exception {
final SelectionKey selectionKey = this.selectionKey;
if (!selectionKey.isValid()) {
return;
}
readPending = true;
final int interestOps = selectionKey.interestOps();
if ((interestOps & readInterestOp) == 0) {
selectionKey.interestOps(interestOps | readInterestOp);
}
}
由于现在是客户端连接流程,readInterestOp可以在#NioServerSocketChannel的父类构造中找到赋值
public NioSocketChannel(Channel parent, SocketChannel socket) {
super(parent, socket);
config = new NioSocketChannelConfig(this, socket.socket());
}
#AbstractNioByteChannel
protected AbstractNioByteChannel(Channel parent, SelectableChannel ch) {
super(parent, ch, SelectionKey.OP_READ);
}
到这里客户端的连接流程就已经分析完毕,在这一章里主要是要弄清服务端通道和客户端通道的区别,在找到boss与worker交互的地方,后面的客户端注册逻辑就和服务端注册逻辑基本一致,下面还是给个流程图来方便大家理解。
以上就是Netty服务端处理客户端连接的整体流程,后续还会继续更新Netty相关的知识。
|