Netty源码(九):接收数据

整体流程
在这里插入图片描述
也是在EvenetLoop的run方法中select到了read事件,开始处理read事件。
进入AbstractNioByteChannel类,调用其中的read方法

		@Override
        public final void read() {
            final ChannelConfig config = config();
            if (shouldBreakReadReady(config)) {
                clearReadPending();
                return;
            }
            final ChannelPipeline pipeline = pipeline();
            // 内存分配器
            final ByteBufAllocator allocator = config.getAllocator();
            // 接收数据测handler
            final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
            allocHandle.reset(config);

            ByteBuf byteBuf = null;
            boolean close = false;
            try {
                do {
                    // 分配内存
                    byteBuf = allocHandle.allocate(allocator);
                    // 开始读取数据
                    allocHandle.lastBytesRead(doReadBytes(byteBuf));
                    if (allocHandle.lastBytesRead() <= 0) {// 读取失败
                        // nothing was read. release the buffer.
                        byteBuf.release();
                        byteBuf = null;
                        close = allocHandle.lastBytesRead() < 0;
                        if (close) {
                            // There is nothing left to read as we received an EOF.
                            readPending = false;
                        }
                        break;
                    }

                    allocHandle.incMessagesRead(1);//读取一次数据
                    readPending = false;
                    pipeline.fireChannelRead(byteBuf);// 将读到的数据传递出去
                    byteBuf = null;
                } while (allocHandle.continueReading());//继续读取

                // 通过当前
                allocHandle.readComplete();//计算下一次的需要分配的空间
                pipeline.fireChannelReadComplete();// 将完成读取的事件传递出去

                if (close) {
                    closeOnRead(pipeline);
                }
            } catch (Throwable t) {
                handleReadException(pipeline, byteBuf, t, close, allocHandle);
            } finally {
                // Check if there is a readPending which was not processed yet.
                // This could be for two reasons:
                // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
                // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
                //
                // See https://github.com/netty/netty/issues/2254
                if (!readPending && !config.isAutoRead()) {
                    removeReadOp();
                }
            }
        }

1. 跟进allocHandle.allocate(allocator)

分配内存时用的是下面两个方法,第一次read会guess方法猜测需要分配的容量的大小,默认是64*1024

		@Override
        public ByteBuf allocate(ByteBufAllocator alloc) {
            // guess猜测我们需要分配的内存大小
            return alloc.ioBuffer(guess());
        }

        @Override
        public int guess() {
            return Math.min(individualReadMax, bytesToRead);// 这两个值默认都是64*1024
        }

2. 跟进doReadBytes(byteBuf)

NioSocketChanneldoReadBytes方法,调用AbstractByteBufwriteBytes方法。

	@Override
    public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
        // 确认可以读取的数据长度
        ensureWritable(length);
        // 读取数据
        int writtenBytes = setBytes(writerIndex, in, length);
        if (writtenBytes > 0) {
            writerIndex += writtenBytes;
        }
        return writtenBytes;
    }

跟进setBytes方法,进入其中一个具体实现类UnpooledDirectByteBufsetBytes方法

	@Override
    public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
        ensureAccessible();
        ByteBuffer tmpBuf = internalNioBuffer();
        // 设置tmpBuf的position和limit
        tmpBuf.clear().position(index).limit(index + length);
        try {
            // 读取channel中的数据到tmpBuf
            return in.read(tmpBuf);//这是jdk的源码
        } catch (ClosedChannelException ignored) {
            return -1;
        }
    }

总结:
1.分配内存是自适应的
2.读取流采用的任然是jdk的channel.red()去读取
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值