netty学习8:NioSocketChannel源码分析

NioSocketChannel就是nio中的SocketChannel的封装,所以它肯定会提供了SocketChannel对象的创建、连接、端口绑定、关闭等操作,netty客户端和服务端都有用到这个对象。

public NioSocketChannel() {//类被反射加载的时候调用该无参构造函数
        this(DEFAULT_SELECTOR_PROVIDER);
    }

public NioSocketChannel(SelectorProvider provider) {
        this(newSocket(provider));//创建SocketChannel并赋值
    }

public NioSocketChannel(SocketChannel socket) {
        this(null, socket);
    }

public NioSocketChannel(Channel parent, SocketChannel socket) {
        super(parent, socket);
        config = new NioSocketChannelConfig(this, socket.socket());
    }



//创建SocketChannel客户端channel
    private static SocketChannel newSocket(SelectorProvider provider) {
        try {
            //创建nio的SocketChannel对象
            return provider.openSocketChannel();
        } catch (IOException e) {
            throw new ChannelException("Failed to open a socket.", e);
        }
    }

//客户端跟服务端建立连接
    @Override
    protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
        if (localAddress != null) {
            doBind0(localAddress);//绑定地址
        }

        boolean success = false;
        try {
            boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);//跟远程地址建立连接
            if (!connected) {
                selectionKey().interestOps(SelectionKey.OP_CONNECT);
            }
            success = true;
            return connected;
        } finally {
            if (!success) {
                doClose();
            }
        }
    }

private void doBind0(SocketAddress localAddress) throws Exception {
        if (PlatformDependent.javaVersion() >= 7) {
            SocketUtils.bind(javaChannel(), localAddress);//绑定地址
        } else {
            SocketUtils.bind(javaChannel().socket(), localAddress);
        }
    }





public abstract class AbstractNioChannel extends AbstractChannel {

protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
        super(parent);
        this.ch = ch;//在io.netty.channel.socket.nio.NioServerSocketChannel.doReadMessages中调用 在NioSocketChannel实例化时候也会调用
        this.readInterestOp = readInterestOp;//bossGroup是NioServerSocketChannel写死OP_ACCEPT事件 workgroup是AbstractNioByteChannel写死OP_READ事件
        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);
        }
    }
}

总结就是:

1、netty客户端 创建SocketChannel

2、netty客户端 建立远程连接

3、netty服务端获取到客户端SocketChannel之后 包装成NioSocketChannel对象也会用到相关构造函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值