ChannelInitializer在Netty中是一个很重要的东西。也是4.x版本中用户接触比较多的一个类

它本身是继承ChannelInboundHandlerAdapter的。实现ChannelInboundHandler类


【推荐1】Netty4 ChannelPipeLine分析  ★★★★☆

【推荐2】java netty之ChannelPipeline  ★★★☆☆

【推荐3】netty源码分析之FrameDecoder(LengthFieldBasedFrameDecoder)  ★★★☆☆

ChannelPipeline 中的中的事件流程

  • I/O Request
    via Channel or
    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 学习的更多相关文章

  1. 【Netty源码学习】ServerBootStrap

    上一篇博客[Netty源码学习]BootStrap中我们介绍了客户端使用的启动服务,接下来我们介绍一下服务端使用的启动服务. 总体来说ServerBootStrap有两个主要功能: (1)调用父类Ab ...

  2. Netty 源码学习——客户端流程分析

    Netty 源码学习--客户端流程分析 友情提醒: 需要观看者具备一些 NIO 的知识,否则看起来有的地方可能会不明白. 使用版本依赖 <dependency> <groupId&g ...

  3. netty权威指南学习笔记八——编解码技术之JBoss Marshalling

    JBoss Marshalling 是一个java序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调参数和附加特性,这些参数 ...

  4. netty权威指南学习笔记五——分隔符和定长解码器的应用

    TCP以流的方式进行数据传输,上层应用协议为了对消息进行区分,通常采用以下4中方式: 消息长度固定,累计读取到长度综合为定长LEN的报文后,就认为读取到了一个完整的消息,将计数器置位,重新开始读取下一 ...

  5. netty权威指南学习笔记三——TCP粘包/拆包之粘包现象

    TCP是个流协议,流没有一定界限.TCP底层不了解业务,他会根据TCP缓冲区的实际情况进行包划分,在业务上,一个业务完整的包,可能会被TCP底层拆分为多个包进行发送,也可能多个小包组合成一个大的数据包 ...

  6. Netty源码学习系列之4-ServerBootstrap的bind方法

    前言 今天研究ServerBootstrap的bind方法,该方法可以说是netty的重中之重.核心中的核心.前两节的NioEventLoopGroup和ServerBootstrap的初始化就是为b ...

  7. 【Netty源码学习】DefaultChannelPipeline(三)

    上一篇博客中[Netty源码学习]ChannelPipeline(二)我们介绍了接口ChannelPipeline的提供的方法,接下来我们分析一下其实现类DefaultChannelPipeline具 ...

  8. 【Netty源码学习】ChannelPipeline(一)

    ChannelPipeline类似于一个管道,管道中存放的是一系列对读取数据进行业务操作的ChannelHandler. 1.ChannelPipeline的结构图: 在之前的博客[Netty源码学习 ...

  9. Netty 源码学习——EventLoop

    Netty 源码学习--EventLoop 在前面 Netty 源码学习--客户端流程分析中我们已经知道了一个 EventLoop 大概的流程,这一章我们来详细的看一看. NioEventLoopGr ...

随机推荐

  1. safedog的小技巧

    限制3389连接:下载SafedogServer\SafeDogGuardCenter\ProGuardData.ini回本地,然后本地搭建安全狗,覆盖,查看计算机名,修改自己计算机名再连接. 卸载安 ...

  2. android之消息机制(一)

    消息操作类Handler 首先编写main.xml文件代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk ...

  3. iOS dispatch_source_t的理解

    Dispatch Source是GCD中的一个基本类型,从字面意思可称为调度源,它的作用是当有一些特定的较底层的系统事件发生时,调度源会捕捉到这些事件,然后可以做其他的逻辑处理,调度源有多种类型,分别 ...

  4. The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

    xmpp 项目中遇到的问题,用苹果的通信API 写一个PUT 方法,向服务器上传一张图片.遇到如题问题. Plist 文件没有NSAppTransportSecurity属性 Dic,添加该属性,再添 ...

  5. UITextField里面的 placeholder颜色和字体

    //  placeholder 颜色 [field setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor& ...

  6. osal_start_timerEx(Lock_TaskID,SBP_START_DEVICE_EVT,SBP_PERIODIC_EVT_PERIOD)的理解

    osal_start_timerEx(Lock_TaskID,SBP_START_DEVICE_EVT,SBP_PERIODIC_EVT_PERIOD)与osal_set_event(Music_Ta ...

  7. 未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序。

    笔记本装的是windows 7旗舰版64位系统,使用的是MS Office 2007(Microsoft.ACE.OLEDB.12.0,32位程序),开发用的是Visual Studio 2010,我 ...

  8. PHP学习(一)----变量及字符串

    PHP中的变量: 1. 定义:$符号来定义变量 2. 说明: (1)PHP弱语言,定义变量的时候不用声明类型,但是并不代表PHP没有数据类型 (2)变量名是区分大小写的,只能是数字,字母或者下划线 ( ...

  9. DOM下的节点属性和操作小结

    属性: 1 .nodeName 节点名称,相当于tagName.属性节点返回属性名,文本节点返回#text.nodeName,是只读的. 2 .nodeType 值:1,元素节点:2,属性节点:3,文 ...

  10. buffer overflow vulnerabilitie

    Computer Systems A Programmer's Perspective Second Edition Avoiding security holes.For many years,bu ...