Netty源码剖析——bind()绑定端口的分析-上(三十)

1.服务器就是在这个bind()里启动完成的

2.Bind()代码,追踪到创建了一个端口对象,并做了一些空判断,核心代码 doBind

public ChannelFuture bind(SocketAddress localAddress){
validate();
if(localAddress=null){
throw new NullPointerException("localAddress");
return doBind(localAddress);
}

3.doBind源码里,核心是两个方法 initAndRegister() 和 doBind0()

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();
//===============================
//说明:执行 doBind方法,完成对端口的绑定
//===============================
doBind0(regFuture,channel,localAddress,promise);
retum promise;
}else{
//Registration future is almost alwaysfulfilled already,but just in case it's not.
final PendingRegistrationPromise promise=new PendingRegistrationPromise(channel);
regFuture.addListener(new ChannelFutureListener(){
@Override
public void operationComplete(ChannelFuture future)throws Exception{
Throwable cause-future.cause();
if(cause!=null){
//Registration on the EventLoop failed sofail the ChannelPromise directly to not cause an
// IllegalStateException once we try toaccess the EventLoop of the Channel. 
promise.setFailure(cause);
}else{
//Registration was successful,so set the correct executor to use. 
//See https://github.com/netty/netty/issues/2586
promise.registered();
doBind0(regFuture,channel,localAddress,promise);
}
}
});
return promise;
}
}

相关推荐

  1. React16: React中event事件监听实现

    2024-03-13 10:14:01       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-13 10:14:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-13 10:14:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-13 10:14:01       87 阅读
  4. Python语言-面向对象

    2024-03-13 10:14:01       96 阅读

热门阅读

  1. c# DbHelper的封装

    2024-03-13 10:14:01       38 阅读
  2. 比特币如何运作?区块链、网络、交易

    2024-03-13 10:14:01       64 阅读
  3. 华为OD机试真题-5G网络建设

    2024-03-13 10:14:01       38 阅读
  4. list排序根据某个字段去重

    2024-03-13 10:14:01       43 阅读
  5. vsto快速在excel中查找某个字符串

    2024-03-13 10:14:01       41 阅读
  6. C++通过循环删除字典中的元素及UB

    2024-03-13 10:14:01       41 阅读
  7. ·xss文件上传漏洞

    2024-03-13 10:14:01       41 阅读
  8. 在 debian 虚拟机里如何设置 iso 文件为本地安装源

    2024-03-13 10:14:01       46 阅读
  9. Zookeeper集群搭建

    2024-03-13 10:14:01       41 阅读
  10. 大语言模型(LLM)Token 概念

    2024-03-13 10:14:01       42 阅读
  11. 自然语言处理概念以及发展

    2024-03-13 10:14:01       44 阅读
  12. C#编程技巧--2

    2024-03-13 10:14:01       38 阅读