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. Json格式获取接口返回的值

    关键字:Set Variable       Get Json Value       to json        Get From Dictionary     具体关键字用法不再说明,可百度一下 ...

  2. 探索未知种族之osg类生物---渲染遍历之裁剪一

    前言 上面我们用了四节课的内容,讲解了一些osg概念性的内部原理.希望大家可以再看今天的讲解之前先再仔细的研究一下前四节的内容.这样你就会对整个osg的渲染过程有一个更加清晰的认知,有助于理解下面两个 ...

  3. Python_格式化字符

    %% 百分号标记 #就是输出一个%%c 字符及其ASCII码%s 字符串 %r 是不管是什么打印出来%d 有符号整数(十进制)%u 无符号整数(十进制)%o 无符号整数(八进制)%x 无符号整数(十六 ...

  4. 学习java之路 简单日历查询代码

    /** * 31天的 1, 3, 5, 7, 8, 10 ,12 30天的 4, 6,9,11 28天(平年) 2 29天(闰年) 2 */ class Demo{ public static voi ...

  5. Vmware Workstation - linux系统下 VmTools 安装

    程序版本 : VMware® Workstation 14 Pro 系统环境 : win10 64位下 ubuntu-14.04.5-desktop-amd64 问题:在运行linux系统过程中,de ...

  6. ELK的文档搭建

    一.安装elasticsearch 官网:https://www.elastic.co/guide/index.html https://www.elastic.co/guide/en/elastic ...

  7. python基础--------字符串的调用详解(2)

    Python 字符串的的调用方法~~~@@@ 17.  strip  : 去除字符串左右两边指定的字符 18.   rstrip : 去除字符串右边指定的字符 19 .   lstrip  :  去除 ...

  8. JDK工具 javap

    javap -c [ClassName]  编译为汇编语言

  9. c# 将object尝试转为指定对象

    主方法: /// <summary> /// 将object尝试转为指定对象 /// </summary> /// <param name="data" ...

  10. formdata的使用方法

    function upload_single_file(value){ if(value==''){ layer.msg('请添加文件',{time:1500}) }else{ var formDat ...