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. Skipping acquire of configured file ···doesn't support architecture 'i386' acquire of configured file

    系统更新的时候报错: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'http://rep ...

  2. g++ 学习笔记

    gcc and g++分别是GNU的c & c++编译器.gcc/g++在执行编译的时候一般有下面4步: 1 预处理,生成.i的文件. 命令如下:g++ -E  test.cpp 此命令只进行 ...

  3. [二分答案][NOIP2015]跳石头

    跳石头 题目描述 一年一度的“跳石头”比赛又要开始了!这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N 块岩石(不含起 ...

  4. iframe和form表单实现ajax请求上传数据

    form的target属性设置为iframe的name值时,表示提交到url后返回的数据显示到iframe区域 <form action="/upload.html" met ...

  5. 在IDEA下导入Maven项目之后 Dependencies报红线

    在IDEA中导入新的Maven项目之后,许多jar包出现波浪线的情况,在网上搜了很多办法都不管,什么直接删掉pom文件中的依赖,保存之后,刷新再ctrl+z 撤回 问题就消失了, 还有的说,你可以直接 ...

  6. java学习(四)

    学号 20189214 <Java程序设计>第四周学习总结 教材学习内容总结 枚举 枚举是JDK1.5版本新增的特性(泛型.For-each等如今被广泛应用的特性也是由JDK1.5时所新增 ...

  7. Non-decreasing Array LT665

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  8. RF自动化测试

    1.自动化分层:UI层实现界面自动化,Service层实现接口自动化,Unit层实现单元测试. 2.UI自动化测试常见的工具有:QTP,AutoIt,Selenium.Selenium是做Web测试最 ...

  9. 老赵点滴地址:http://blog.zhaojie.me/2009/05/a-simple-actor-model-implementation.html

    老赵点滴地址:http://blog.zhaojie.me/2009/05/a-simple-actor-model-implementation.html

  10. [Ionic] Error: No provider for Http! Error: No provider for Http!

    1. 打开src/app/app.module.ts 2. 在最上面导入 import{HttpModule} from '@angular/http'; 3. 在imports块中加入:HttpMo ...