问题

  • channel 是如何处理发送一半中断后继续重发的
  • channel 具体作用是什么

概述

这一节我们将介绍 Channel 和内部接口 Unsafe .其中Unsafe 是内部接口,聚合在Channel 中协助网络读写操作相关的操作,设计初衷就是 Channel 的内部辅助类,不应该被用户使用。

继承类分析

继承关系链 :

AbstractChannel -> AbstractNioChannel -> AbstractNioByteChannel -> NioSocketChannel 如下图

从以上的类结构我们也要学习一下类的构建,各个类实现应该实现的功能,最后生成的具体类具有不同的功能。
AbstractChannel ,保存以下重要的字段 ,主要
- EventLoop
- localAddress
- remoteAddress
- unsafe
- DefaultChannelPipleline
- Future类 和 Promise类 等

AbstractNioChannel,从类名可以看出和nio 中 Channel 相关,注册,监听

    private final SelectableChannel ch;
protected final int readInterestOp;
private volatile SelectionKey selectionKey;
private volatile boolean inputShutdown; /**
* The future of the current connection attempt. If not null, subsequent
* connection attempts will fail.
*/
private ChannelPromise connectPromise;
private ScheduledFuture<?> connectTimeoutFuture;
private SocketAddress requestedRemoteAddress;
 AbstractNioByteChannel 这个类是Channel对Byte进行操作,对ByteBuff的读写。

源码分析

AbstractChannel

AbstractChannel 的读写方法都是交由 ChannelPiple 来解决的
    @Override
public Channel read() {
pipeline.read();
return this;
} @Override
public ChannelFuture write(Object msg) {
return pipeline.write(msg);
}

eventLoop方法,直接返回持有的 eventloop对象

    @Override
public EventLoop eventLoop() {
return eventLoop;
}

AbstractNioChannel

public abstract class AbstractNioChannel extends AbstractChannel {

    private static final InternalLogger logger =
InternalLoggerFactory.getInstance(AbstractNioChannel.class); // No.1 注册监听相关的字段
private final SelectableChannel ch;
protected final int readInterestOp;
private volatile SelectionKey selectionKey;
private volatile boolean inputShutdown; // No.2 异步执行的字段,或是回调相关的字段
/**
* The future of the current connection attempt. If not null, subsequent
* connection attempts will fail.
*/
private ChannelPromise connectPromise;
private ScheduledFuture<?> connectTimeoutFuture;
private SocketAddress requestedRemoteAddress; ... //核心方法
@Override
protected void doRegister() throws Exception {
boolean selected = false;
for (;;) {
try {
//拿父类的channel对象(父类的channel对象是java原生channel 对象)
selectionKey = javaChannel().register(eventLoop().selector, 0, this);
return;
} catch (CancelledKeyException e) {
if (!selected) {
// Force the Selector to select now as the "canceled" SelectionKey may still be
// cached and not removed because no Select.select(..) operation was called yet.
eventLoop().selectNow();
selected = true;
} else {
// We forced a select operation on the selector before but the SelectionKey is still cached
// for whatever reason. JDK bug ?
throw e;
}
}
}
} //开始read的操作
@Override
protected void doBeginRead() throws Exception {
if (inputShutdown) {
return;
} final SelectionKey selectionKey = this.selectionKey;
if (!selectionKey.isValid()) {
return;
} //就是改变监听的事件
final int interestOps = selectionKey.interestOps();
if ((interestOps & readInterestOp) == 0) {
selectionKey.interestOps(interestOps | readInterestOp);
}
}

AbstractNioByteChannel

    @Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
int writeSpinCount = -1; //循环
for (;;) {
Object msg = in.current(true); if (msg == null) {
// Wrote all messages.写完了(发送完了)所有的消息,清除标志,结束
clearOpWrite();
break;
} if (msg instanceof ByteBuf) {
//加入是ByteBuf类型
ByteBuf buf = (ByteBuf) msg;
int readableBytes = buf.readableBytes();
//判断当前的可读字节是否为 0 ,为 0 丢弃掉
if (readableBytes == 0) {
in.remove();
continue;
} boolean setOpWrite = false;
boolean done = false;
long flushedAmount = 0;
//循环发送次数
if (writeSpinCount == -1) {
writeSpinCount = config().getWriteSpinCount();
}
for (int i = writeSpinCount - 1; i >= 0; i --) {
//doWriteBytes 子类实现
int localFlushedAmount = doWriteBytes(buf);
if (localFlushedAmount == 0) {
setOpWrite = true;
break;
} flushedAmount += localFlushedAmount;
//一直到不可读
if (!buf.isReadable()) {
done = true;
break;
}
}
//发送完,更新发送的进度(有可能没发完)
in.progress(flushedAmount); if (done) {
in.remove();
} else {
//没发完,设置写半包标识,启动刷新线程继续发送之前没有发送完成的半包消息
incompleteWrite(setOpWrite);
break;
}
} else if (msg instanceof FileRegion) {
FileRegion region = (FileRegion) msg;
boolean setOpWrite = false;
boolean done = false;
long flushedAmount = 0;
if (writeSpinCount == -1) {
writeSpinCount = config().getWriteSpinCount();
} //循环发送
for (int i = writeSpinCount - 1; i >= 0; i --) {
long localFlushedAmount = doWriteFileRegion(region);
if (localFlushedAmount == 0) {
setOpWrite = true;
break;
} flushedAmount += localFlushedAmount;
if (region.transfered() >= region.count()) {
done = true;
break;
}
}
//发送完(有可能发送了一半)更新进度
in.progress(flushedAmount); if (done) {
in.remove();
} else {
//没法完,创建一个任务扔到EventLoop
incompleteWrite(setOpWrite);
break;
}
} else {
throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(msg));
}
}
} //没写完(没发送完)
protected final void incompleteWrite(boolean setOpWrite) {
// Did not write completely.
if (setOpWrite) {
setOpWrite();
} else {
// Schedule flush again later so other tasks can be picked up in the meantime
//创建任务扔到 eventLoop执行
Runnable flushTask = this.flushTask;
if (flushTask == null) {
flushTask = this.flushTask = new Runnable() {
@Override
public void run() {
flush();
}
};
}
eventLoop().execute(flushTask);
}
}
循环发送次数是指一次发送没有完成时(写半包),程序就继续尝试循环写操作,此时IO线程是不能处理其他事件的,例如读新的消息或者执行定时任务和 NioTask 等, 如果网络IO阻塞或者对方接收消息太慢,可能会导致线程假死,于是就要循环发送。

AbstractNioMessageChannel

我们再来看一下AbstractNioChannel 的另外一个子类 AbstractNioMessageChannel,直接看doWrite方法
    @Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
final SelectionKey key = selectionKey();
final int interestOps = key.interestOps(); for (;;) {
Object msg = in.current();
if (msg == null) {
// Wrote all messages.
if ((interestOps & SelectionKey.OP_WRITE) != 0) {
key.interestOps(interestOps & ~SelectionKey.OP_WRITE);
}
break;
} boolean done = false;
for (int i = config().getWriteSpinCount() - 1; i >= 0; i --) {
if (doWriteMessage(msg, in)) {
done = true;
break;
}
} if (done) {
in.remove();
} else {
// Did not write all messages.
//没发送完,设置标志,交给 select 多路复用器轮询对应的channel重新发送尚未发送完成的半包信息
if ((interestOps & SelectionKey.OP_WRITE) == 0) {
key.interestOps(interestOps | SelectionKey.OP_WRITE);
}
break;
}
}
}
AbstractNioMessageChannel 和 AbstractNioByteChannel的区别在于

NioServerSocketChannel 和 NioServerChannel 的分析

NioSocketChannel 和 NioServerSocketChannel 的区别到底是什么?后者是服务端当中负责绑定端口,读取数据功能,连接和断开,写消息都不支持,这些功能都在NioSocketChannel中实现

AbstractNioMessageServerChannel 的具体子类是 NioServerSocketChannel(该类是服务器端接受处理客户端的channel),它的doReadMessages方法(被对应的unsafe类read方法,这里可能有点饶,具体看代码实现)分析如下
	@Override
protected int doReadMessages(List<Object> buf) throws Exception {
SocketChannel ch = javaChannel().accept(); try {
if (ch != null) {
//构建一个NioSocketChannel放进数组中
buf.add(new NioSocketChannel(this, childEventLoopGroup().next(), ch));
return 1;
}
} catch (Throwable t) {
logger.warn("Failed to create a new channel from an accepted socket.", t); try {
ch.close();
} catch (Throwable t2) {
logger.warn("Failed to close a socket.", t2);
}
} return 0;
}

NioServerChannel的源码分析

public class NioServerSocketChannel extends AbstractNioMessageServerChannel
implements io.netty.channel.socket.ServerSocketChannel { private static final ChannelMetadata METADATA = new ChannelMetadata(false); private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioServerSocketChannel.class); private static ServerSocketChannel newSocket() {
try {
return ServerSocketChannel.open();
} catch (IOException e) {
throw new ChannelException(
"Failed to open a server socket.", e);
}
} private final ServerSocketChannelConfig config; /**
* Create a new instance
*/
public NioServerSocketChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
super(null, eventLoop, childGroup, newSocket(), SelectionKey.OP_ACCEPT);
config = new DefaultServerSocketChannelConfig(this, javaChannel().socket());
} @Override
public InetSocketAddress localAddress() {
return (InetSocketAddress) super.localAddress();
} @Override
public ChannelMetadata metadata() {
return METADATA;
} @Override
public ServerSocketChannelConfig config() {
return config;
} @Override
public boolean isActive() {
return javaChannel().socket().isBound();
} @Override
public InetSocketAddress remoteAddress() {
return null;
} @Override
protected ServerSocketChannel javaChannel() {
return (ServerSocketChannel) super.javaChannel();
} @Override
protected SocketAddress localAddress0() {
return javaChannel().socket().getLocalSocketAddress();
} @Override
protected void doBind(SocketAddress localAddress) throws Exception {
javaChannel().socket().bind(localAddress, config.getBacklog());
} @Override
protected void doClose() throws Exception {
javaChannel().close();
} @Override
protected int doReadMessages(List<Object> buf) throws Exception {
SocketChannel ch = javaChannel().accept(); try {
if (ch != null) {
buf.add(new NioSocketChannel(this, childEventLoopGroup().next(), ch));
return 1;
}
} catch (Throwable t) {
logger.warn("Failed to create a new channel from an accepted socket.", t); try {
ch.close();
} catch (Throwable t2) {
logger.warn("Failed to close a socket.", t2);
}
} return 0;
} // Unnecessary stuff
@Override
protected boolean doConnect(
SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
throw new UnsupportedOperationException();
} @Override
protected void doFinishConnect() throws Exception {
throw new UnsupportedOperationException();
} @Override
protected SocketAddress remoteAddress0() {
return null;
} @Override
protected void doDisconnect() throws Exception {
throw new UnsupportedOperationException();
} @Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
throw new UnsupportedOperationException();
}
}
可以看到 NioServerChannel 的主要都是 override 父类的方法,即是说大部分的逻辑都在父类 Abstract中进行了一层层的封装,给我们一个启发,好的类结构在
在一开始就已经设计好,最终的具体实现交由尾端实现。

总结

本文介绍了channel的主要功能作用。

参考资料

  • 《Netty权威指南》

netty(五) channel的更多相关文章

  1. Netty 源码解析(二):Netty 的 Channel

    本文首发于微信公众号[猿灯塔],转载引用请说明出处 接下来的时间灯塔君持续更新Netty系列一共九篇 Netty源码解析(一):开始 当前:Netty 源码解析(二): Netty 的 Channel ...

  2. Netty之Channel*

    Netty之Channel* 本文内容主要参考**<<Netty In Action>> ** 和Netty的文档和源码,偏笔记向. 先简略了解一下ChannelPipelin ...

  3. spark2.1源码分析3:spark-rpc如何实现将netty的Channel隐藏在inbox中

    class TransportServer bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Overri ...

  4. spark-rpc是如何实现将netty的Channel隐藏在inbox中的

    class TransportServer bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Overri ...

  5. Netty:Channel 建立后消息发送失败

    1. 问题现象 Channel 建立后消息发送失败: ChannelFuture future = DeviceManager.getBootstrap().connect(); deviceChan ...

  6. Netty Associated -- Channel

    A nexus to a network socket or a component which is capable of I/O operations such as read, write, c ...

  7. Netty的Channel

    Channel是一个网络端口连接,或者是可以进行读,写,链接,绑定端口的组件的连接.  Channel就是一个链接,它提供了如下的功能. 1:获取当前链接的状态 2:配置当前链接参数 3:进行read ...

  8. netty笔记-:Channel与ChannelHandlerContext执行write方法的区别

      在netty中有我们一般有两种发送数据的方式,即使用ChannelHandlerContext或者Channel的write方法,这两种方法都能发送数据,那么其有什么区别呢.这儿引用netty文档 ...

  9. Netty:Channel

    上一篇我们通过一个简单的Netty代码了解到了Netty中的核心组件,这一篇我们将围绕核心组件中的Channel来展开学习. Channel的简介 Channel代表着与网络套接字或者能够进行IO操作 ...

  10. 项目系统Netty的Channel和用户之间的关系绑定正确做法,以及Channel通道的安全性方案

    前言 考虑一个功能业务,在web程序中向指定的某个用户进行实时通讯 在Web运用的Socket通讯功能中(如在线客服),为保证点对点通讯.而这个看似简单的根据用户寻到起channel通道实际会碰到不少 ...

随机推荐

  1. Android 开发OOM解决方案

    OOM(Out Of Memory)在加载图片过多或者过大的情况下会发生OOM,可以查看APP最高可用内存: int maxMemory = (int) (Runtim.getRuntime().ma ...

  2. <img src = "..."/>的一个图片上面怎么在放上字

    转自:https://zhidao.baidu.com/question/1495805873400412779.html 例子1: html中可以用css相对定位让文字在图片的上面. 1.新建htm ...

  3. TCP/IP详解,卷1:协议--IP:网际协议

    引言 I P 是 T C P / I P 协议族中最为核心的协议.所有的 T C P.U D P.I C M P 及 I G M P 数据都以 I P 数据 报格式传输(见图 1 - 4).许多刚开始 ...

  4. Linux - cron - cron 表达式

    概述 之前 cron 的补充 这次介绍下 表达式 背景 之前有说过 cron 这次说下 表达式 1. 准备 环境 os centos7 2. 模板文件 文件 位置 /etc/crontab 作用 系统 ...

  5. 牛客竞赛第二场D Kth Minimum Clique 贪心+bitmap

    Kth Minimum Clique 题意 给出n(n<100)个点的邻接表,和n个点的权值,求第k大的团(完全子图) 分析 n很小,并且好像没有什么算法和这个有关系,所以可以往暴力枚举的方向想 ...

  6. LED Decorative Light Manufacturer - LED Neon Rope: 5 Advantages

    In the past 100 years, lighting has come a long way. Nowadays, the decorative LED lighting design ca ...

  7. 条件锁condition与Queue()

    在学习之前你应该先了解锁和队列基础 import queue import time import random import threading import asyncio import logg ...

  8. python 常用包之xml文件处理

    1,处理xml的包 from xml.etree import ElementTree as ET 2,如何写出xml文件 xml文件和html中的元素很像,有父级子集之说, root = ET.El ...

  9. 国内某Python大神自创完整版,系统性学习Python

    很多小伙伴纠结于这个一百天的时间,我觉得完全没有必要,也违背了我最初放这个大纲上来的初衷,我是觉得这个学习大纲还不错,自学按照这个来也能相对系统的学习知识,而不是零散细碎的知识最后无法整合,每个人的基 ...

  10. tensflow

    今天换了电脑,重新安装视频中软件,发现没记录很麻烦,还是记录以下步骤吧 打开终端输入指令:python,如图1所示,我的系统是ubuntu16.04.03,默认安装的python版本为2.7.12. ...