netty ByteToMessageDecoder 分析
ByteToMessageDecoder
1.socket 移除时触发,最后次读数据处理 @Override
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = internalBuffer();
if (buf.isReadable()) {
ByteBuf bytes = buf.readBytes(buf.readableBytes());
buf.release();
ctx.fireChannelRead(bytes);
}
cumulation = null;
ctx.fireChannelReadComplete();
handlerRemoved0(ctx);
} 2.读取数据 ByteBuf release,discardSomeReadBytes 方法后面研究 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
RecyclableArrayList out = RecyclableArrayList.newInstance();//创建包装过的数组
try {
if (msg instanceof ByteBuf) {
ByteBuf data = (ByteBuf) msg;
if (cumulation == null) { //是否继续读取数据
cumulation = data;
try {
callDecode(ctx, cumulation, out);
} finally {
if (cumulation != null && !cumulation.isReadable()) {
cumulation.release();
cumulation = null;
}
}
} else {//继续读取处理
try {
//如果剩余空间不够开创建新空间
if (cumulation.writerIndex() > cumulation.maxCapacity() - data.readableBytes()) {
ByteBuf oldCumulation = cumulation;
cumulation = ctx.alloc().buffer(oldCumulation.readableBytes() + data.readableBytes());
cumulation.writeBytes(oldCumulation);
oldCumulation.release();
}
cumulation.writeBytes(data);
callDecode(ctx, cumulation, out);
} finally {
if (cumulation != null) {
if (!cumulation.isReadable()) {
cumulation.release();
cumulation = null;
} else {
cumulation.discardSomeReadBytes();
}
}
data.release();
}
}
} else {
out.add(msg);
}
} catch (DecoderException e) {
throw e;
} catch (Throwable t) {
throw new DecoderException(t);
} finally {
int size = out.size();
if (size == 0) {
decodeWasNull = true;
} else {
for (int i = 0; i < size; i ++) {
ctx.fireChannelRead(out.get(i));
}
}
out.recycle();
}
} //其实只要管核心代码 decode 调用业务处理
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
try {
//循环读取数据
while (in.isReadable()) {
int outSize = out.size();
int oldInputLength = in.readableBytes();
decode(ctx, in, out);//业务扩展处理 // Check if this handler was removed before continuing the loop.
// If it was removed, it is not safe to continue to operate on the buffer.
//
// See https://github.com/netty/netty/issues/1664
//如果 handler 删除之前,那么不读取数据了
if (ctx.isRemoved()) {
break;
}
//下面写得很不清晰。。。。。 if (outSize == out.size()) {
if (oldInputLength == in.readableBytes()) {
break;
} else {
continue;
}
} if (oldInputLength == in.readableBytes()) {
throw new DecoderException(
StringUtil.simpleClassName(getClass()) +
".decode() did not read anything but decoded a message.");
} if (isSingleDecode()) {
break;
}
}
} catch (DecoderException e) {
throw e;
} catch (Throwable cause) {
throw new DecoderException(cause);
}
}
netty ByteToMessageDecoder 分析的更多相关文章
- Netty原理分析
Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用 ...
- Netty系列之Netty可靠性分析
作者 李林锋 发布于 2014年6月19日 | 29 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件分享 稍后阅读 我的阅读清单 1. 背景 1.1. 宕机的代价 1.1. ...
- Netty启动分析
基于Netty-3.2.5 先看一段Netty的服务端代码: import java.net.InetSocketAddress; import java.util.concurrent.Execut ...
- 【转】Netty系列之Netty可靠性分析
http://www.infoq.com/cn/articles/netty-reliability 首先,我们要从Netty的主要用途来分析它的可靠性,Netty目前的主流用法有三种: 1) 构建R ...
- Netty代码分析
转自:http://www.blogjava.net/BucketLi/archive/2010/12/28/332462.html Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开 ...
- Netty系列之Netty可靠性分析--转载
原文地址:http://www.infoq.com/cn/articles/netty-reliability 1. 背景 1.1. 宕机的代价 1.1.1. 电信行业 毕马威国际(KPMG Inte ...
- netty全局分析1
这个系列都是别人的分析文 https://www.jianshu.com/p/ac7fb5c2640f 一丶 Netty基础入门 Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.U ...
- Netty 组件分析
EventLoop 事件循环对象 EventLoop 本质是一个单线程执行器(同时维护了一个 Selector),里面有 run 方法处理 Channel 上源源不断的 io 事件. 它的继承关系比较 ...
- netty ByteBuf分析
1.Heap Buffer(堆缓冲区) 2.Direct Buffer(直接缓冲区) 3.Composite Buffer(复合缓冲区) 4.PooledByteBuf 池缓冲 readerInex ...
随机推荐
- SQL中order by;group up;like;关联查询join on的用法
排序order by的用法: 1.order by 字段名1 asc/desc, 字段名2 asc/desc,... 先按照字段名1的升序/降续给表进行排列 然后 按照字段名2的升序/降续给表进行排列 ...
- HTML入门的简单学习
1:HTML简介 1.1:HTML(Haper Text Markup language):超文本标记语言 超文本就是指页面内可以包含图片,链接,甚至音乐,程序等非文字元素 1.2 ...
- vbscript调用WMI一键式式发布网站
作为.net开发,在window环境下,不得不熟悉些脚本语言,来减轻些日常开发中所遇到的一些繁杂的事情,比如自动发布网站,自动发布网站等等. WMI windows管理程序接口,可用各种与语言调用,方 ...
- Leetcode 6 ZigZag Conversion 字符串处理
题意:将字符串排成Z字形. PAHNAPLSIIGYIR 如果是5的话,是这样排的 P I AP YR H L G N SI A I 于是,少年少女们,自己去找规律吧 提示:每个Z ...
- hdu 1548 楼梯 bfs或最短路 dijkstra
http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...
- android: 通过内容提供器读取系统联系人
读取系统联系人 由于我们之前一直使用的都是模拟器,电话簿里面并没有联系人存在,所以现在需要自 己手动添加几个,以便稍后进行读取.打开电话簿程序,界面如图 7.1 所示. 图 7.1 可以看到,目前 ...
- 推荐几款API文档集合工具
https://zealdocs.org/ 开源.免费,支持Linux.Windows http://velocity.silverlakesoftware.com/ https://kape ...
- [1].jekyll扫盲
一.jekyll是什么? jekyll是一款免费的blog生成工具,将纯文本(plain text)转换为静态网站或博客. Jekyll是一个使用Ruby编写的静态站点生成工具,使用Liquid模板渲 ...
- python ide: pycharm
1, 设置python路径 2,运行py文件 https://www.jetbrains.com/help/pycharm/2016.1/creating-and-running-your-first ...
- Android webView打不开网页的解决办法
在我们开发过程中,有可能会遇到webview有些网页打不开的问题.这可能是设置的不对,下面就是解决办法. 进行如下设置吧,大多数情况都能解决! displayWebview.getSettings() ...