前言

Netty 源码剖析之 unSafe.read 方法 一文中,我们研究了 read 方法的实现,这是读取内容到容器,再看看 Netty 是如何将内容从容器输出 Channel 的吧。

1. ctx.writeAndFlush 方法

当我们调用此方法时,会从当前节点找上一个 outbound 节点,进行,并调用下个节点的 write 方法。具体看代码:

@1
public ChannelFuture writeAndFlush(Object msg) {
return writeAndFlush(msg, newPromise()); // 创建了一个默认的 DefaultChannelPromise 实例,返回的就是这个实例。 @2
public ChannelFuture writeAndFlush(Object msg, ChannelPromise promise) {
if (isNotValidPromise(promise, true)) {//判断 promise 有效性
ReferenceCountUtil.release(msg);// 释放内存
return promise;
} write(msg, true, promise); return promise;
} @3
private void write(Object msg, boolean flush, ChannelPromise promise) {
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);
}
}

最终调用的就是 @3 方法。找到上一个 outbound 节点,判断他的节点是否时当前线程。如果是,则会直接调用,泛着,将后面的工作封装成一个任务放进 mpsc 队列,供当前线程稍后执行。这个 任务的 run 方法如下:

public final void run() {
try {
if (ESTIMATE_TASK_SIZE_ON_SUBMIT) {
ctx.pipeline.decrementPendingOutboundBytes(size);
}
write(ctx, msg, promise);
} finally {
ctx = null;
msg = null;
promise = null;
handle.recycle(this);
}
} protected void write(AbstractChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
ctx.invokeWrite(msg, promise);
}

最终执行的是 invokeWrite 方法。

我们看看如果直接执行会如何处理。

private void invokeWriteAndFlush(Object msg, ChannelPromise promise) {
if (invokeHandler()) {
invokeWrite0(msg, promise);
invokeFlush0();
} else {
writeAndFlush(msg, promise);
}
}

先执行 invokeWrite0 方法进行 write,然后 flush。

最终执行的是 unSafe 的 write 方法。注意:当前节点已经到了 Head 节点。

详细说说该方法。

2. unSafe 的 write 方法

public final void write(Object msg, ChannelPromise promise) {
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);
}

方法步骤如下:

  1. 判断 outboundBuffer 有效性。
  2. 将 ByteBuf 过滤成池化或者线程局部直接内存(如果不是直接内存的话)。
  3. 预估当前 ByteBuf 大小(就是可读字节数)。
  4. 将 ByteBuf 包装成一个 Entry 节点放入到 outboundBuffer 的单向链表中。

这里有一个地方需要注意一下, filterOutboundMessage 方法。

如果是直接内存的话,就直接返回了 ,反之调用 newDirectBuffer 方法。我们猜想肯定是重新包装成直接内存,利用直接内存令拷贝的特性,提升性能。

看看该方法内部逻辑:

protected final ByteBuf newDirectBuffer(ByteBuf buf) {
final int readableBytes = buf.readableBytes();
if (readableBytes == 0) {
ReferenceCountUtil.safeRelease(buf);
return Unpooled.EMPTY_BUFFER;
} final ByteBufAllocator alloc = alloc();
if (alloc.isDirectBufferPooled()) {
ByteBuf directBuf = alloc.directBuffer(readableBytes);
directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
ReferenceCountUtil.safeRelease(buf);
return directBuf;
} final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
if (directBuf != null) {
directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
ReferenceCountUtil.safeRelease(buf);
return directBuf;
} // Allocating and deallocating an unpooled direct buffer is very expensive; give up.
return buf;
}
  1. 首先判断可读字节数,如果是0,之际返回一个空的 Buffer。
  2. 获取该 Channel 的 ByteBufAllocator ,如果是直接内存且池化,则分配一个直接内存,将旧的 Buffer 写入到新的中,释放旧的 Buffer。返回新的直接内存 Buffer。
  3. 反之,从 FastThreadLocal 中返回一个可重用的直接内存 Buffer,后面和上面的操作一样,写入,删除旧的,返回新的。注意,这里返回的可重用的 Buffer,当调用他的 release 方法的时候,实际上是归还到了 FastThreadLocal 中。对象池的最佳实践。

关于 addMessage 方法,我们将在另一篇文章Netty 出站缓冲区 ChannelOutboundBuffer 源码解析(isWritable 属性的重要性)详细阐述,这里只需要知道,他放入了一个 出站缓存中就行了。

3. unSafe 的 flush 方法

重点是 outboundBuffer.addFlush() 方法和 flush0 方法。

这两个方法在 Netty 出站缓冲区 ChannelOutboundBuffer 源码解析(isWritable 属性的重要性)

这里只需要知道,addFlush 方法将刚刚添加进出站 buffer 的数据进行检查,并准备写入 Socket。

flush0 做真正的写入操作,其中,调用了 JDK 的 Socket 的 write 方法,将 ByteBuf 封装的 ByteBuffer 写到 Socket 中。

总结

可以看到,数据真正的写出还是调用了 head 的节点中 unsafe 的 write 方法和 flush 方法,其中,write 只是将数据写入到了出站缓冲区,并且,write 方法可以调用多次,flush 才是真正的写入到 Socket。而更详细的细节,可以查看我的另一篇文章Netty 出站缓冲区 ChannelOutboundBuffer 源码解析(isWritable 属性的重要性)

good luck !!!!

Netty 源码剖析之 unSafe.write 方法的更多相关文章

  1. Netty 源码剖析之 unSafe.read 方法

    目录: NioSocketChannel$NioSocketChannelUnsafe 的 read 方法 首先看 ByteBufAllocator 再看 RecvByteBufAllocator.H ...

  2. Netty源码剖析-断开连接

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! ----主线: ----源码: 在NioEventLoop的unsa ...

  3. Netty源码剖析-接受数据

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! ----主线:worker thread ①多路复用器(Select ...

  4. Netty源码剖析-构建链接

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! ----主线: 和启动一样也是有两个线程完成的,boss threa ...

  5. Netty源码剖析-启动服务

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! --1主线分两步: 一:首先在our thread里,如果写在mai ...

  6. Netty源码剖析-业务处理

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! ----主线:worker thread 触发pipeline.fi ...

  7. Netty源码剖析-关闭服务

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! ----主线:  ----源码: 先在服务端加个断点和修改下代码:如 ...

  8. Netty源码剖析-发送数据

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! 开始之前先介绍下Netty写数据的三种方式: ①:write:写到一 ...

  9. Netty学习笔记(三)——netty源码剖析

    1.Netty启动源码剖析 启动类: public class NettyNioServer { public static void main(String[] args) throws Excep ...

随机推荐

  1. 2.Handler处理器 和 自定义Opener

    Handler处理器 和 自定义Opener opener是 urllib2.OpenerDirector 的实例,我们之前一直都在使用的urlopen,它是一个特殊的opener(也就是模块帮我们构 ...

  2. winform执行程序报错:已停止工作,windows正在检查该问题的解决方案

    每次运行程序时都会弹出错误框:winform已停止工作,windows正在检查该问题的解决方案 事件查看器错位信息: 错误应用程序名称: TMS_winform.exe,版本: 1.0.0.0,时间戳 ...

  3. MVVM 简化的Messager类

    看MVVMLight的Messager源码,自己实现了一个简单的Messager类. Messager类可以在MVVM中,实现View与VM.VM与VM.View与View的通信. public cl ...

  4. [leetcode.com]算法题目 - Pow(x, n)

    Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...

  5. 微服务统一登陆认证怎么做?JWT ?

    无状态登录原理 1.1.什么是有状态? 有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session. 例如登录:用户登 ...

  6. “全栈2019”Java多线程第三十三章:await与signal/signalAll

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  7. IPv6 Can't assign requested address

    今天试了下 bind IPv6 的地址,报错  Can't assign requested address http://stackoverflow.com/questions/24780404/p ...

  8. Python 绝技 —— TCP服务器与客户端

    i春秋作家:wasrehpic 0×00 前言 「网络」一直以来都是黑客最热衷的竞技场.数据在网络中肆意传播:主机扫描.代码注入.网络嗅探.数据篡改重放.拒绝服务攻击……黑客的功底越深厚,能做的就越多 ...

  9. underscore.js源码研究(4)

    概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...

  10. Linux巩固记录(6) Hbase环境准备-zookeeper安装

    Hbase是运行在hadoop之上,所以请参考第3篇文章搭建好一个master,两个slave的hadoop环境,我采用的版本为hadoop2.7.4 不了解Hbase的同学可以参考下这篇文章,分析得 ...