Netty Decoder:ByteToMessageDecoder
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的更多相关文章
- Netty(二):如何处理io请求?
文接上一篇.上篇讲到netty暴露一个端口出来,acceptor, handler, pipeline, eventloop 都已准备好.但是并没体现其如何处理接入新的网络请求,今天我们就一起来看看吧 ...
- 网络编程Netty入门:Netty的启动过程分析
目录 Netty的启动过程 Bootstrap 服务端的启动 客户端的启动 TCP粘包.拆包 图示 简单的例子 Netty编解码框架 Netty解码器 ByteToMessageDecoder实现类 ...
- Netty(一):server启动流程解析
netty作为一个被广泛应用的通信框架,有必要我们多了解一点. 实际上netty的几个重要的技术亮点: 1. reactor的线程模型; 2. 安全有效的nio非阻塞io模型应用; 3. pipeli ...
- Netty学习:ChannelHandler执行顺序详解,附源码分析
近日学习Netty,在看书和实践的时候对于书上只言片语的那些话不是十分懂,导致尝试写例子的时候遭遇各种不顺,比如decoder和encoder还有HttpObjectAggregator的添加顺序,研 ...
- 自定义Decoder继承ByteToMessageDecoder实现解码的小案例
ByteToMessageDecoder是一种ChannelInboundHandler,可以称为解码器,负责将byte字节流(ByteBuf)转换成一种Message,Message是应用可以自己定 ...
- netty学习:UDP服务器与Spring整合(2)
上一篇文章中,介绍了netty实现UDP服务器的栗子. 本文将会对UDP服务器与spring boot整合起来,并使用RedisTemplate的操作类访问Redis和使用Spring DATA JP ...
- netty学习:UDP服务器与Spring整合
最近接到一个关于写UDP服务器的任务,然后去netty官网下载了netty的jar包(netty-4.0.49.Final.tar.bz2),解压后,可以看到上面有不少example,找到其中的关于U ...
- Netty实战:设计一个IM框架
来源:逅弈逐码 bitchat 是一个基于 Netty 的 IM 即时通讯框架 项目地址:https://github.com/all4you/bitchat 快速开始 bitchat-example ...
- Netty(一):的入门使用。
Netty的入门基本使用流程代码,不做具体分析.使用版本为Netty 4.x版本. 服务端调用示例: 绑定端口号为8080端口 package com.cllover; import com.sun. ...
随机推荐
- dt常用类
经常使用的一些datatable的操作,包括一些过滤去重的方法 using System; using System.Collections; using System.Collections.Gen ...
- Vue的双向数据绑定
最简单的实现v-model数据绑定,只需要在一个组件里面有个props,加上一个value,然后当组件要去修改数据的时候, $emit一个input事件,并且把新的值传出去.这就实现了Vue里面的数据 ...
- sublime text2 安装及使用教程
1.下载安装包地址:https://www.sublimetext.com/2 2.安装,一直点下一步就好,将下列选项打钩,这样文件右键就可以直接用sublime text2打开 3.新建一个html ...
- Python中使用cx_Oracle调用Oracle存储过程
import cx_Oracle as cx import datetime def execute_sql(): # 声明变量 date_time = datetime.datetime.now() ...
- eclipse中搭建ssm框架
工具:jdk1.7+eclipse+tomcat+mysql. 这里用的版本是spring3,框架中用到的实体类和xml映射文件都可以用工具生成的.接下来会将源码贴出,方便初学者快速搭建. 一.新建一 ...
- TestNG(一)
1.环境搭建 2.框架结构 3.数据驱动 4.监听器 5.重试逻辑 6.异常测试 7.并发测试
- Mongo学习笔记
安装和开始 下载 MongoDB 参考:+MongoDB安装配置(Windows) +Mongo手册
- linux vsftp 简单配置
查看自己是否安装vsftp rpm -qa | grep vsftp rpm -qa 查看自己已安装的包 过滤vsftp systemctl rsetart vsftpd 重启服务 先关闭防火墙 sy ...
- gc的real时间比user时间长
https://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1 htt ...
- 二进制安装mysql5.7
1.创建用户和组 groupadd mysql useradd -r -g mysql mysql 2.上传二进制包至/usr/local下解压并改名为mysql tar zxvf mysql-5.7 ...