【Netty学习】 ChannelInitializer 学习
ChannelInitializer在Netty中是一个很重要的东西。也是4.x版本中用户接触比较多的一个类
它本身是继承ChannelInboundHandlerAdapter的。实现ChannelInboundHandler类
【推荐1】Netty4 ChannelPipeLine分析 ★★★★☆
【推荐2】java netty之ChannelPipeline ★★★☆☆
【推荐3】netty源码分析之FrameDecoder(LengthFieldBasedFrameDecoder) ★★★☆☆
ChannelPipeline 中的中的事件流程
I/O Request
viaChannelor
ChannelHandlerContext
|
+---------------------------------------------------+---------------+
| ChannelPipeline | |
| \|/ |
| +---------------------+ +-----------+----------+ |
| | Inbound Handler N | | Outbound Handler 1 | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
| | \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler N-1 | | Outbound Handler 2 | |
| +----------+----------+ +-----------+----------+ |
| /|\ . |
| . . |
| ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
| [ method call] [method call] |
| . . |
| . \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler 2 | | Outbound Handler M-1 | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
| | \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler 1 | | Outbound Handler M | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
+---------------+-----------------------------------+---------------+
| \|/
+---------------+-----------------------------------+---------------+
| | | |
| [ Socket.read() ] [ Socket.write() ] |
| |
| Netty Internal I/O Threads (Transport Implementation) |
+-------------------------------------------------------------------+
这个图是joc里面的ChannelPipeline文档里面的一张示意图。图示的内容是。
当有IO请求ChannelInboundHandler 是从上之下搜索,相应的实现。一次执行。
4.x版本中的Netty。启用了一个新的东西用于初始ChannelPipeline -> ChannelInitializer<Channel>ChannelInitializer有一个抽象未实现的方法:initChannel(C ch) 用于开发者自己定义相关的Handler添加到信道中。原理及详细分析请参考上面推荐的两篇文章。可能版本还不是最新的。但是4.x版本之后的一些大致的思路都是没有什么变化。 下面把自己写的小东西贴出来。
package com.engine.netty.server.initializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.timeout.IdleStateHandler; import com.engine.netty.server.handler.IdleStateCheckHandler;
import com.engine.netty.server.handler.codec.DecoderAMF3;
import com.engine.netty.server.handler.codec.EncoderAMF3;
import com.engine.netty.socket.ChatServerHandler; /**
* Creates a newly configured {@link ChannelPipeline} for a new channel.
*/
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> { @Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); // 编码或者解码 拆包
/*
* 需要和客户端订立 相关通信协议底层规则 - 【消息长度(2 : short表示 2个字节)】【消息内容】
* lengthFieldLength => 是整数 - 占四个字节 所以同时偏移四个字节 => (0, 4, 0, 4)
*
* */
pipeline.addLast("framer", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
pipeline.addLast("prepender", new LengthFieldPrepender(4, false)); // 空闲状态检查 间隔无消息,断开连接
pipeline.addLast("idleStateCheck", new IdleStateHandler(0, 0, 600));
pipeline.addLast("idleCheckHandler", new IdleStateCheckHandler()); // AMF3 编码或者解码
pipeline.addLast("decoder", new DecoderAMF3());
pipeline.addLast("encoder", new EncoderAMF3()); // 序列化对象object编码和解码 【用于Java Object】
//pipeline.addLast("objectEncoder", new ObjectEncoder());
//pipeline.addLast("objectDecoder", new ObjectDecoder(ClassResolvers.cacheDisabled(null))); /*// 以UTF-8格式编码和解码 数据 【用于字符串】
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));*/ // and then business logic.
pipeline.addLast("handler", new ChatServerHandler());
}
}NettyServerInitializer
发出去AMF3的东西是上一篇记录里面写的之外。其他两种编码和解码都是官网有相对应例子的。编码和解码说白其实就是对字节数据的翻译。在Netty中数据已ByteBuf保存。ByteBuf提供了很多的方法读取相对应的数据。在这里就不详细介绍了。有兴趣的可以去看看官网的示例。或者是API 文档。文档中有一些类。官方都提供了相对应的代码范例。
【空闲状态检查】是官网API文档中找到的。
io.netty.handler.timeout 这个包中就是Netty提供的一些关于读超时,写超时,读写超时。空闲时间等的东西。 LengthFieldBasedFrameDecoder 和 LengthFieldPrepender
这两个编码解码 相当于是对数据的处理。
先讲一下LengthFieldBasedFrameDecoder :
直接通过命名大家很明显的可以理解这个类 一个根据帧长度的解码类。到底是怎么样子的呢?其实很简单。 LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip)
参数:
maxFrameLength: the maximum length of the frame. If the length of the frame is greater than this value, TooLongFrameException will be thrown. (最大帧长)
lengthFieldOffset: the offset of the length field (偏移量)
lengthFieldLength: the length of the length field
lengthAdjustment: the compensation value to add to the value of the length field
initialBytesToStrip: the number of first bytes to strip out from the decoded frame
它有5个参数 (详细的解释可以参考推荐文章3).
说简单点就是第一个是字节消息的最大长度。第二个参数是消息长度这个字节的偏移量。第三个参数是描述字节消息长度的那部分的长度。第四个不解释。第五个是跳过首部的字节长度
我这边写的0202其实意思就是 我的消息长度是有一个short类型来描述的。short类型的数据占2个字节。第二个2是说在返回最终解码的数据的时候跳过这个描述长度的字节数。
API文档中的:
2 bytes length field at offset 0, strip header :
Because we can get the length
of the content by calling ByteBuf.readableBytes(), you might want to strip the
length field by specifying initialBytesToStrip. In this example, we specified 2,
that is same with the length of the length field, to strip the first two bytes.
lengthFieldOffset = 0
lengthFieldLength = 2
lengthAdjustment = 0
initialBytesToStrip = 2 (= the length of the Length field) BEFORE
DECODE (14 bytes) AFTER DECODE (12 bytes)
+--------+----------------+ +----------------+
| Length | Actual Content |----->| Actual Content |
| 0x000C | "HELLO, WORLD" | | "HELLO, WORLD" |
+--------+----------------+ +----------------+ 最后的ChatServerHandler是我自己写的逻辑的Handler貌似就写到这边了。O(∩_∩)O哈哈~
【Netty学习】 ChannelInitializer 学习的更多相关文章
- 【Netty源码学习】ServerBootStrap
上一篇博客[Netty源码学习]BootStrap中我们介绍了客户端使用的启动服务,接下来我们介绍一下服务端使用的启动服务. 总体来说ServerBootStrap有两个主要功能: (1)调用父类Ab ...
- Netty 源码学习——客户端流程分析
Netty 源码学习--客户端流程分析 友情提醒: 需要观看者具备一些 NIO 的知识,否则看起来有的地方可能会不明白. 使用版本依赖 <dependency> <groupId&g ...
- netty权威指南学习笔记八——编解码技术之JBoss Marshalling
JBoss Marshalling 是一个java序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调参数和附加特性,这些参数 ...
- netty权威指南学习笔记五——分隔符和定长解码器的应用
TCP以流的方式进行数据传输,上层应用协议为了对消息进行区分,通常采用以下4中方式: 消息长度固定,累计读取到长度综合为定长LEN的报文后,就认为读取到了一个完整的消息,将计数器置位,重新开始读取下一 ...
- netty权威指南学习笔记三——TCP粘包/拆包之粘包现象
TCP是个流协议,流没有一定界限.TCP底层不了解业务,他会根据TCP缓冲区的实际情况进行包划分,在业务上,一个业务完整的包,可能会被TCP底层拆分为多个包进行发送,也可能多个小包组合成一个大的数据包 ...
- Netty源码学习系列之4-ServerBootstrap的bind方法
前言 今天研究ServerBootstrap的bind方法,该方法可以说是netty的重中之重.核心中的核心.前两节的NioEventLoopGroup和ServerBootstrap的初始化就是为b ...
- 【Netty源码学习】DefaultChannelPipeline(三)
上一篇博客中[Netty源码学习]ChannelPipeline(二)我们介绍了接口ChannelPipeline的提供的方法,接下来我们分析一下其实现类DefaultChannelPipeline具 ...
- 【Netty源码学习】ChannelPipeline(一)
ChannelPipeline类似于一个管道,管道中存放的是一系列对读取数据进行业务操作的ChannelHandler. 1.ChannelPipeline的结构图: 在之前的博客[Netty源码学习 ...
- Netty 源码学习——EventLoop
Netty 源码学习--EventLoop 在前面 Netty 源码学习--客户端流程分析中我们已经知道了一个 EventLoop 大概的流程,这一章我们来详细的看一看. NioEventLoopGr ...
随机推荐
- thinkphp模板中foreach循环没数据的错误解决
从控制器方法中$this->assign();函数将值传递给html模板 但是模板不显示数据,直接出来的是代码,效果就和html中写了php代码不能解析一样. 原来是我将thinkphp框架的引 ...
- NodeJs - 100
Nodejs官方文档 https://nodejs.org/en/docs/ Nodejs官方网站 https://nodejs.org/en/ Nodejs的特征: 1.采用非阻塞性IO机制:—— ...
- (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug
如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...
- eXosip2代码分析
主要类型定义: 1.struct eXtl_protocol struct eXtl_protocol { int enabled; int proto_port; ]; ]; int proto_n ...
- gcc/g++链接时.o文件及库的顺序问题
折腾gcc/g++链接时.o文件及库的顺序问题 链接静态库的顺序问题 GCC 编译使用动态链接库和静态链接库--及先后顺序----及环境变量设置总结
- Android adb 命令使用总结
adb原理 参考文档 How ADB works http://www.cnblogs.com/ifantastic/p/5186362.html http://blog.csdn.ne ...
- Memcached 笔记与总结(2)编译 php-memcache 扩展
环境:CentOS 6.6 + Apache 2.2.21 + PHP 5.3.10 php-memcache 是 php 写的 memcached 的客户端,以扩展的形式发布. 对于正在运行的 ph ...
- 《编写可维护的 Javascript》读书笔记(附录 A 部分):Javascript 编码风格指南(1)原始值
记录一下比较有用的编码规范(该指南是基于 Java 语言编码规范和 Javascript 编程规范,同时结合作者 Nicholos Zakas 的个人经验和喜好). 一些关于格式(包括缩进.行的长度. ...
- Web 在线文件管理器学习笔记与总结(7)重命名文件
rename($oldname,$newname) 重命名文件或目录 <<<EOF EOF; 使用heredoc 技术,来部分实现界面与代码的准分离 重命名时,需要验证新文件名的合法 ...
- 基于s5pv210的uboot总结
1.启动过程特征总结 (1)第一阶段为汇编阶段.第二阶段为C阶段 (2)第一阶段在SRAM中.第二阶段在DRAM中 (3)第一阶段注重SoC内部.第二阶段注重SoC外部Board内部 2.uboot的 ...