一. 接上一篇
http://blog.51cto.com/483181/2121265
我們繼續分析doBind0(regFuture, channel, localAddress, promise)
private ChannelFuture doBind(final SocketAddress localAddress) {
final ChannelFuture regFuture = initAndRegister();
final Channel channel = regFuture.channel();
if (regFuture.cause() != null) {
return regFuture;
}
if (regFuture.isDone()) {
// At this point we know that the registration was complete and successful.
ChannelPromise promise = channel.newPromise();
doBind0(regFuture, channel, localAddress, promise); //3. 我們這篇要分析的內容
return promise;
} else {
...
return promise;
}
}
二. doBind0
2.1 4個參數
如上面代碼,doBind0有4個參數regFuture, channel, localAddress, promise,它們的類型如下:
regFuture: DefaultChannelPromise
channel:NioServerSocketChannel
localAddress:SocketAddress
promise:DefaultChannelPromise
那繼續往下面看代碼,
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() {
@Override
public void run() {
if (regFuture.isSuccess()) {
channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
} else {
promise.setFailure(regFuture.cause());
}
}
});
}
doBind0()代碼很簡單,調用channel.eventloop()執行了一個Runnable。channel.eventloop()調用的是AbstractChannle.eventloop()
@Override
public EventLoop eventLoop() {
EventLoop eventLoop = this.eventLoop;
if (eventLoop == null) {
throw new IllegalStateException("channel not registered to an event loop");
}
return eventLoop;
}
而this.eventLoop初始化是在register的時候,具體可以參考上一篇bind初始化的分析
http://blog.51cto.com/483181/2121265
@Override
public final void register(EventLoop eventLoop, final ChannelPromise promise) {
....
AbstractChannel.this.eventLoop = eventLoop;
...
}
它的類型是一個NioEventLoop,所以它就是往自己的線程池裏面丟了一個Runnable任務
NioEventLoop的繼承關係圖如下:
我們可以看一下NioEventLoop.execute(Runnable )方法.
2.2 execute(Runnable)方法
這個方法的實現是在SingleThreadEventExecutor.java裏面。
private final Queue<Runnable> taskQueue;
@Override
public void execute(Runnable task) {
if (task == null) {
throw new NullPointerException("task");
}
boolean inEventLoop = inEventLoop();
addTask(task);
if (!inEventLoop) {
startThread();
if (isShutdown() && removeTask(task)) {
reject();
}
}
if (!addTaskWakesUp && wakesUpForTask(task)) {
wakeup(inEventLoop);
}
}
protected void addTask(Runnable task) {
if (task == null) {
throw new NullPointerException("task");
}
if (!offerTask(task)) {
reject(task);
}
}
final boolean offerTask(Runnable task) {
if (isShutdown()) {
reject();
}
return taskQueue.offer(task);
}
它是把傳入的Runnable對象放到一個taskQueue隊列裏面。
那我們繼續看Runnable裏面的實現,channel.bind(xxxx)
channel.eventLoop().execute(new Runnable() {
@Override
public void run() {
...
channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}
});
2.3 channel.bind
Channel的類型是NioServerSocketChannel,而bind方法的實現類是在AbstractChannel.java裏面.
@Override
public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
return pipeline.bind(localAddress, promise);
}
調用的是pipeline.bind(xxx),pipeline我們知道,它鏈接了ChannelHandler的Context,有head和tail。head是outbound,tail是inbound.
2.4 pipe.bind(xxx)
DefaultChannelPipeline.java
@Override
public final ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
return tail.bind(localAddress, promise);
}
直接調用的是tail.bind,繼續往下面看
AbstractChannelHandlerContext.java
@Override
public ChannelFuture bind(final SocketAddress localAddress, final ChannelPromise promise) {
final AbstractChannelHandlerContext next = findContextOutbound();
EventExecutor executor = next.executor();
if (executor.inEventLoop()) {
next.invokeBind(localAddress, promise);
} else {
safeExecute(executor, new Runnable() {
@Override
public void run() {
next.invokeBind(localAddress, promise);
}
}, promise, null);
}
return promise;
}
private AbstractChannelHandlerContext findContextOutbound() {
AbstractChannelHandlerContext ctx = this;
do {
ctx = ctx.prev;
} while (!ctx.outbound);
return ctx;
}
它調用findContextOutbound(),然後調用它的bind方法,從以前的分析可以知道,outbound講的是head。所以,我們來到head.bind方法
2.5 head.bind
DefaultChannelPipeline.java
@Override
public void bind(
ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise)
throws Exception {
unsafe.bind(localAddress, promise);
}
調用的是unsafe.bind
2.6 unsafe.bind
AbstractChannel.java
@Override
public final void bind(final SocketAddress localAddress, final ChannelPromise promise) {
assertEventLoop();
...
boolean wasActive = isActive();
try {
doBind(localAddress);
} catch (Throwable t) {
...
}
if (!wasActive && isActive()) {
invokeLater(new Runnable() {
@Override
public void run() {
pipeline.fireChannelActive();
}
});
}
safeSetSuccess(promise);
}
繼續看doBind()
2.7 doBind()
doBind是在NioServerSocketChannel裏面
@Override
protected void doBind(SocketAddress localAddress) throws Exception {
if (PlatformDependent.javaVersion() >= 7) {
javaChannel().bind(localAddress, config.getBacklog());
} else {
javaChannel().socket().bind(localAddress, config.getBacklog());
}
}
這樣就調用了Java的接口綁定了我們傳入的端口。
這樣bind邏輯整個就分析完了,我們來分析一下整個流程以及類之間的關係。
三. 總結
3.1 各個類之間的關係
- EventLoopGroup裏面包含若干個EventLoop,具體數目我們可以手動指定,如果不指定,一般默認就是cpu x 2個。
- EventLoop繼承自SingleThreadEventLoop,表示一個線程,裏面有個隊列queue,用來存丟過來的Runnable任務.
- 我們一般實例化兩個EventLoopGroup,一個是bossGroup,一個是workGroup,bossGroup用來接收客戶端連接,連接可以用channel來描述。然後就把channel丟給workGroup,由workGroup具體負責這個channel後面的操作。
3.2 對應關係
- 一個EventLoopGroup包含多個EventLoop
- 一個EventLoop對應多個channel,一個channel只對應一個EventLoop。EventLoop一般比channel少,好比飯店裏面服務員一般比客人少。所以,也可以想象如果某一個channel上EventLoop做的業務邏輯比較複雜的話,有可能造成來不及相應其他channel的請求。
- 一個NioServerSocketChannel裏面包含一個pipeline,一個unsafe對象