netty---------write flush两个方法到底做了什么?

上一篇已经看到:unsafe的read方法,把channel中的数据read到byteBuff中的byteBuffer里。那么根据猜想,下面要进行的应该是nio 的 channel的write(byteBuffer),将用户空间的数据,写到内核空间。那么按照这个逻辑,其实nio的write方法对应的netty应该是flush方法,看看是不是这样。

@Override
-----------ChannelHandlerContext的write方法其实是调用的pipeline的write方法,到最后调用的是tail的write方法,
public final ChannelFuture write(Object msg) { return tail.write(msg); }
--------------------这里根据传入的boolean值,决定是write还是writeAndflush,先看write
private
void write(Object msg, boolean flush, ChannelPromise promise) {
-----------------因为这里执行的是tail的write方法, 所以要看tail的findContextOutbound的逻辑,tail这个context在初始化的时候inbound是true,但是outbound属性是false,所以要从tail往前,找outboundHandler,而head的outbound属性是true的,看它的write方
法,调用的是unsafe的write方法,是AbstractChannel的write方法。看下一段
AbstractChannelHandlerContext next
= findContextOutbound(); final Object m = pipeline.touch(msg, next); EventExecutor executor = next.executor(); if (executor.inEventLoop()) { if (flush) { next.invokeWriteAndFlush(m, promise); } else { next.invokeWrite(m, promise); } } else { AbstractWriteTask task; if (flush) { task = WriteAndFlushTask.newInstance(next, m, promise); } else { task = WriteTask.newInstance(next, m, promise); } safeExecute(executor, task, promise, m); } }

 

@Override
-----------------unsafe的write方法
public final void write(Object msg, ChannelPromise promise) { assertEventLoop();
----------------ChannelOutboundBuffer这个类,维护这一个由他自己的内部类entry组成的一个单链表,entry有着指向byteBuffer和byteBuffer数组的指针,本方法的最下面的addMessage,就是新建一个entry,然后放到链表里,ChannelOutboundBuffer自己有三个属性:
----------------flushedEntry :指向第一个已经被flush的entry
----------------unflushedEntry :指向第一个没有被flush的entry
----------------tailEntry : 队尾的指针。这三个其实很简单
ChannelOutboundBuffer outboundBuffer
= this.outboundBuffer; if (outboundBuffer == null) { safeSetFailure(promise, WRITE_CLOSED_CHANNEL_EXCEPTION); ReferenceCountUtil.release(msg); return; } int size; try { msg = filterOutboundMessage(msg); size = pipeline.estimatorHandle().size(msg); if (size < 0) { size = 0; } } catch (Throwable t) { safeSetFailure(promise, t); ReferenceCountUtil.release(msg); return; } outboundBuffer.addMessage(msg, size, promise); }

 所以可以看到,所谓的netty的channelHandlerContext的write方法,其实并不是向内核写入数据,而是把msg放入一个链表中,等待flush,而flush方法也是在headContext中,调用的unsafe的flush方法

@Override
        public final void flush() {
            assertEventLoop();
            ChannelOutboundBuffer outboundBuffer = this.outboundBuffer;
            if (outboundBuffer == null) {
                return;
            }
------------这个方法是把flushedEntry指针置空,把unFlushedEntry指针放到队列头部 outboundBuffer.addFlush();
------------下文 flush0(); }
@Override
-----------------从flush0到这一步的逻辑比较复杂,涉及到各种判断,简单起见,直接看这一步NioSocketChannel的doWriteBytes方法,netty的写,对应的是buf的readBytes,这其实只是方法名字取的问题。回忆之前的doReadBytes对应的是buf的writeBytes方法,
当时是把channel的数据读到nio
的ByteBuffer中,现在应该是把byteBuffer中的数据write到channel中了,写到内核
protected int doWriteBytes(ByteBuf buf) throws Exception { final int expectedWrittenBytes = buf.readableBytes(); return buf.readBytes(javaChannel(), expectedWrittenBytes); }
---------------还是找到之前的那个byteBuf的实现类,可以看出,还是调用nio的socketChannel的write方法,把tmpBuf(一个Nio的ByteBuffer)写到channel中。
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException { ensureAccessible(); if (length == 0) { return 0; } ByteBuffer tmpBuf; if (internal) { tmpBuf = internalNioBuffer(); } else { tmpBuf = buffer.duplicate(); } tmpBuf.clear().position(index).limit(index + length); return out.write(tmpBuf); }

 

转载于:https://www.cnblogs.com/chuliang/p/8328556.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值