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 分析的更多相关文章

  1. Netty原理分析

    Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用 ...

  2. Netty系列之Netty可靠性分析

      作者 李林锋 发布于 2014年6月19日 | 29 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件分享 稍后阅读 我的阅读清单   1. 背景 1.1. 宕机的代价 1.1. ...

  3. Netty启动分析

    基于Netty-3.2.5 先看一段Netty的服务端代码: import java.net.InetSocketAddress; import java.util.concurrent.Execut ...

  4. 【转】Netty系列之Netty可靠性分析

    http://www.infoq.com/cn/articles/netty-reliability 首先,我们要从Netty的主要用途来分析它的可靠性,Netty目前的主流用法有三种: 1) 构建R ...

  5. Netty代码分析

    转自:http://www.blogjava.net/BucketLi/archive/2010/12/28/332462.html Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开 ...

  6. Netty系列之Netty可靠性分析--转载

    原文地址:http://www.infoq.com/cn/articles/netty-reliability 1. 背景 1.1. 宕机的代价 1.1.1. 电信行业 毕马威国际(KPMG Inte ...

  7. netty全局分析1

    这个系列都是别人的分析文 https://www.jianshu.com/p/ac7fb5c2640f 一丶 Netty基础入门 Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.U ...

  8. Netty 组件分析

    EventLoop 事件循环对象 EventLoop 本质是一个单线程执行器(同时维护了一个 Selector),里面有 run 方法处理 Channel 上源源不断的 io 事件. 它的继承关系比较 ...

  9. netty ByteBuf分析

    1.Heap Buffer(堆缓冲区) 2.Direct Buffer(直接缓冲区) 3.Composite Buffer(复合缓冲区) 4.PooledByteBuf 池缓冲 readerInex ...

随机推荐

  1. C#与数据库访问技术总结(十八)

    ADO.NET 代码综合示例 前面已经介绍过OLE DB.NET和SQL Server.NET数据提供者可以用来连接不同的数据源. 以下代码不仅综合演示了使用ADO.NET的这两种数据提供者访问数据库 ...

  2. Prototype in JavaScript

    声明 本文旨在入门,简单了解下何为prototype & __proto__ 原型对象 我们创建每个函数都有个prototype(原型)属性,该属性是一个指针,指向一个对象,而这对象的用途是包 ...

  3. spring源码 — 二、从容器中获取Bean

    getBean 上一节中说明了容器的初始化,也就是把Bean的定义GenericBeanDefinition放到了容器中,但是并没有初始化这些Bean.那么Bean什么时候会初始化呢? 在程序第一个主 ...

  4. UVa 10387- Billiard

    UVa 10387- Billiard Table of Contents 1 题目 2 思路 3 代码 4 参考 1 题目 ============= Problem A: Billiard In ...

  5. Redis+Django(Session,Cookie)的用户系统

    一.Django authentication django authentication提供了一个便利的user api接口,无论在py中 request.user,参见Request and re ...

  6. 如何将 DVD 转成 ISO

    Windows 电脑 + 光驱(Mac 下没找到类似 UltraISO 这么好用的软件,知道的朋友推荐一下哈) 安装 UltraISO 软件(试用版即可),http://baoku.360.cn/so ...

  7. AngularJS中实现日志服务

    本篇体验使用AngularJS自定义一个记录日志的服务. 在AngularJS中,服务的一些写法是这样的: var app = angular.module('app',[]); app.provid ...

  8. 那些在学习iOS开发前就应该知道的事(part 2)

    英文原文:Things I wish I had known before starting iOS development—Part 2 http://www.cocoachina.com/ios/ ...

  9. libevent 安装异常

    有homebrew的可以使用 1 brew install memcached 这个命令来安装没有homebrew的可以直接手动安装1.去官网http://memcached.org/下载最新的包,然 ...

  10. Mac OS 中设置VPN(pptp连接方式)

    第一步:点击系统设置(如图) 第二步:点击网络,进入网络设置(如图) 第三步:点击+号-创建新服务,创建新的VPN网络连接(如图) 第四步:接口-中选-VPN, VPN类型-中选-PPTP,服务名称- ...