1. ByteToMessageDecoder

  这个类是解码器的基类,其中描述了解码器基本的工作方式和实现原理;;还定义了一个解码的抽象方法decode,这个方法由业务实现,负责将一段字节数据解码为具体的消息对象。

    // 存储接收到的数据
ByteBuf cumulation;
private boolean first; @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf) {
CodecOutputList out = CodecOutputList.newInstance();
try {
ByteBuf data = (ByteBuf) msg; first = cumulation == null;
if (first) {
// 如果 ByteBuf 为空,则将 ByteBuf 指向接收到的消息
cumulation = data;
} else {
// 如果 ByteBuf 不为空,则将接收到的数据追加到 ByteBuf
cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data);
}
// 解码 ByteBuf 中的数据
callDecode(ctx, cumulation, out);
} catch (DecoderException e) {
throw e;
} catch (Throwable t) {
throw new DecoderException(t);
} finally {
// ByteBuf 中的数据被消费完后,重置消费次数,释放内存,ByteBuf置为null
if (cumulation != null && !cumulation.isReadable()) {
numReads = 0;
cumulation.release();
cumulation = null;
// ByteBuf 被消费16后压缩消息
} else if (++ numReads >= discardAfterReads) { numReads = 0;
discardSomeReadBytes();
}
// 将解码得到的报文对象全部分发下去
int size = out.size();
decodeWasNull = !out.insertSinceRecycled();
fireChannelRead(ctx, out, size);
out.recycle();
}
} else {
ctx.fireChannelRead(msg);
}
} // 解码 ByteBuf
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
try {
// 如果 ByteBuf 有可读数据就继续解码
while (in.isReadable()) { // 如果 out 中有内容,则将内容分发出去,然后清空 out, 并将 outSize 置为 0
int outSize = out.size(); if (outSize > 0) {
// 将 out 中的所有消息分发出去
fireChannelRead(ctx, out, outSize);
// out 存储解码后的报文对象
// 清空容器()
out.clear(); if (ctx.isRemoved()) {
break;
}
outSize = 0;
}
// 解码之前的可读长度
int oldInputLength = in.readableBytes();
// 调用自定义的解码器
decode(ctx, in, out); if (ctx.isRemoved()) {
break;
}
// 由上面可知,outSize 为0 ,
// 没有读取数据,没有得到消息对象,则跳出循环
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 Decoder:ByteToMessageDecoder的更多相关文章

  1. Netty(二):如何处理io请求?

    文接上一篇.上篇讲到netty暴露一个端口出来,acceptor, handler, pipeline, eventloop 都已准备好.但是并没体现其如何处理接入新的网络请求,今天我们就一起来看看吧 ...

  2. 网络编程Netty入门:Netty的启动过程分析

    目录 Netty的启动过程 Bootstrap 服务端的启动 客户端的启动 TCP粘包.拆包 图示 简单的例子 Netty编解码框架 Netty解码器 ByteToMessageDecoder实现类 ...

  3. Netty(一):server启动流程解析

    netty作为一个被广泛应用的通信框架,有必要我们多了解一点. 实际上netty的几个重要的技术亮点: 1. reactor的线程模型; 2. 安全有效的nio非阻塞io模型应用; 3. pipeli ...

  4. Netty学习:ChannelHandler执行顺序详解,附源码分析

    近日学习Netty,在看书和实践的时候对于书上只言片语的那些话不是十分懂,导致尝试写例子的时候遭遇各种不顺,比如decoder和encoder还有HttpObjectAggregator的添加顺序,研 ...

  5. 自定义Decoder继承ByteToMessageDecoder实现解码的小案例

    ByteToMessageDecoder是一种ChannelInboundHandler,可以称为解码器,负责将byte字节流(ByteBuf)转换成一种Message,Message是应用可以自己定 ...

  6. netty学习:UDP服务器与Spring整合(2)

    上一篇文章中,介绍了netty实现UDP服务器的栗子. 本文将会对UDP服务器与spring boot整合起来,并使用RedisTemplate的操作类访问Redis和使用Spring DATA JP ...

  7. netty学习:UDP服务器与Spring整合

    最近接到一个关于写UDP服务器的任务,然后去netty官网下载了netty的jar包(netty-4.0.49.Final.tar.bz2),解压后,可以看到上面有不少example,找到其中的关于U ...

  8. Netty实战:设计一个IM框架

    来源:逅弈逐码 bitchat 是一个基于 Netty 的 IM 即时通讯框架 项目地址:https://github.com/all4you/bitchat 快速开始 bitchat-example ...

  9. Netty(一):的入门使用。

    Netty的入门基本使用流程代码,不做具体分析.使用版本为Netty 4.x版本. 服务端调用示例: 绑定端口号为8080端口 package com.cllover; import com.sun. ...

随机推荐

  1. MVC htmlAttributes and additionalViewData

    @Html.TextBoxFor(m => m.UserName, new { title = "ABC" }) // 输出结果为 <input data-val=&q ...

  2. python基础 (函数名,闭包,和迭代器)

    1.函数名作用 函数名本质上就是函数的内存地址或对象. 1.可以被引用 2.可以被当作容器类型的元素 3.可以当作函数的参数和返回值 4.如果记不住的话,那就记住一句话,就当普通变量用 2.闭包 什么 ...

  3. HDU2034

    #include <bits/stdc++.h> using namespace std; int main() { int n,m,val; set<int>::iterat ...

  4. 【转载】http proxy原理

    最近使用Charles抓https包时,发现get和post方式的请求都能抓到,但是method为connect的就是抓不到.而且提示如下: You may need to configure you ...

  5. ibatis中的resultMap

    优点: resultMap可以实现一种功能 当你是1对多 这种多张表查询的时候 你没办法 通过表连接来实现一个集合设置到一个实例里,但是通过resultMap里可以做到 根据关联的字段 查询到一个集合 ...

  6. int main(int argc, char** argv) 以及CommandLineParser

    参考链接: 关于int main(int argc, char** argv) http://blog.csdn.net/liuhuiyi/article/details/8239303 http:/ ...

  7. AHOI——Day1个人感悟

    今天,是个bilibili的日子.(嗯?什么意思?) 洛谷已经尽力了: 于是我带着洛谷的祝福,来到了AHOI的考场--合肥一中. 其实我是考完才签到的,我一大早五点多就起来了,到考场后,在肯德基吃了早 ...

  8. 02.02.02 第2章 制作power bi图表(Power BI商业智能分析)

    ---恢复内容开始--- 02.02.02第2章 制作power bi图表 02.02.02.01 power pivot数据导入 00:08:43 02.02.02.02建立数据透视表 00:11: ...

  9. 学习Acegi应用到实际项目中(8)- 扩展UserDetailsService接口

    一个能为DaoAuthenticationProvider提供存取认证库的的类,它必须要实现UserDetailsService接口: public UserDetails loadUserByUse ...

  10. Git使用(一、TortoiseGit和Gitlab在Windows下的项目库创建和上传)

    介绍使用TortoiseGit初次创建并上传到gitlab项目库,转载请注明出处. 一.需要先安装git环境,并配置Git用户名及邮箱. 二.用PuTTYgen生成公约私钥对(鼠标画画).PuTTYg ...