您当前的位置: 首页 >  nio

恐龙弟旺仔

暂无认证

  • 0浏览

    0关注

    282博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Netty源码解析-NioSocketChannel之connect、read

恐龙弟旺仔 发布时间:2021-12-27 19:14:57 ,浏览量:0

前言:

    前一篇中我们介绍了NioServerSocketChannel,NioServerSocketChannel主要负责两件事:绑定(bind)到本地port,作为一个Endpoint;监听客户端连接事件,将获取到的连接注册到EventLoop中。

    那么数据的读写呢?NioServerSocketChannel不负责数据的读写,那么数据读写都交由NioSocketChannel来负责。

    本文主要讲解NioSocketChannel 处理连接(connect)、读(read)事件。

1.NioSocketChannel类结构图

同样,直接开启Diagrams视角。

与前面NioServerSocketChannel有很多相同的接口和抽象类,AbstractChannel、AbstractNioChannel、SocketChannel等前一篇文章中,都已经有说明,本文不再赘述。

2.AbstractNioByteChannel解析
public abstract class AbstractNioByteChannel extends AbstractNioChannel {
 
    // 构造方法,OP_READ代表NioSocketChannel关注的事件,在注册EventLoop时会将监听事件也传入
    protected AbstractNioByteChannel(Channel parent, SelectableChannel ch) {
        super(parent, ch, SelectionKey.OP_READ);
    }
    
    // 同之前的分析一样,都包含一个NioByteUnsafe
    protected class NioByteUnsafe extends AbstractNioUnsafe {
     
        // 读取对端传入的数据
        @Override
        public final void read() {
            final ChannelConfig config = config();
            // 如果输入流已经关闭,则移除OP_READ监听
            if (shouldBreakReadReady(config)) {
                clearReadPending();
                return;
            }
            final ChannelPipeline pipeline = pipeline();
            final ByteBufAllocator allocator = config.getAllocator();
            final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
            allocHandle.reset(config);

            ByteBuf byteBuf = null;
            boolean close = false;
            try {
                do {
                    // 从ByteBufAllocator分配器中获取ByteBuf
                    byteBuf = allocHandle.allocate(allocator);
                    // doReadBytes()方法负责从channel中读取数据,交由子类NioSocketChannel实现
                    allocHandle.lastBytesRead(doReadBytes(byteBuf));
                    if (allocHandle.lastBytesRead()             
关注
打赏
1655041699
查看更多评论
0.0381s