Netty 5.0

主题结构

Channel(I) - AbstractChannel- AbstractNioChannel- AbstractNioByteChannel - NioSocketChannel   

  1. 内部有一个 DefaultChannelPipeline, 定义的 connect, read, write等方法实际是调用pipeline的方法
    private final DefaultChannelPipeline pipeline;
  2. 内部有 远端地址和本地地址
  3. 内部有 一个unsafe对象, 与Channel成伴出现, 例如 NioByteUnsafe与AbstractNioByteChannel成对出现

ChannelPipeline(I) - DefaultChannelPipeline   

  1. 内部是一个链表,链表由 ChannelHandlerContext 组成。
  2. 声明处理流链表的build操作,如 add, remove等等, 参数可以是 handler的name, handler实例等
  3. 声明对链表的整体功能操作,如 read, write, close, bind等等,并没有真实实现,其实还是交给链表中的每个节点(ctx)完成

ChannelHandlerContext(I) - AbstractChannelHandlerContext   

  1. 标记当前的Handler的属性和状态,比如是inbound还是outbound, 是否是invokedPrevRead(准备读)状态等
  2. 提供了一个invoker来调用实际的方法,在inbound, outbound的方法被调用时, 比如调用read()的时候,找到outbound,遍历outbound,执行每个invoker的read()方法。其实是交给了invoker完成

ChannelHandlerInvoker(I) - DefaultChannelHandlerInvoker   

  1. 定义了一个EventExecutor(ScheduledExecutorService) , 负责实际任务的线程控制
  2. 声明和实现了方法的线程控制部分,如果当前线程是executor线程,执行任务,否则创建子线程执行。
  3. 执行一些前置的判定等等,实际工作还是交给handler执行

ChannelHandler(I) - ChannelHandlerAdapter

  1. 处理流链表中的节点, 如 read(ChannelHandlerContext ctx)
  2. 处理的I/O事件或截获的I/O操作,并将其转发给ChannelPipeline中的下一个处理节点

调用过程案例:
  Channel.write -> ChannelPipeline.write -> ChannelHandlerContext.write -> ChannelHandlerInvoker.write -> ChannelHandler.write
  niosocketChannel.read -> pipeline.read -> ctx.read -> invoker.read -> handler.read -> unsafe.read;

Inbound和outBound的区别

与数据的流向无关,而是触发的方式,由外部网络引起的为in, 由程序员定义的代码引起的为out,即可以理解为in为注册后被动的触发方法,out为程序主动调用之方法, in方法以fire开头,out方法也有read

重点类

SimpleChannelInboundHandler: extends ChannelHandlerAdapter 

  1. 多了一个abstract方法:messageReceived(ChannelHandlerContext ctx, I msg),在channelRead中被调用
  2. 5.0版本目前没有SimpleChannelOutboundHandler, ChannelOutBoundHandler也被注掉了

重点javadoc

A list of ChannelHandlers which handles or intercepts inbound events and outbound operations of a Channel.
ChannelPipeline implements an advanced form of the Intercepting Filter pattern to give a user full control over how an
event is handled and how the ChannelHandlers in a pipeline interact with each other. Creation of a pipeline
Each channel has its own pipeline and it is created automatically when a new channel is created. How an event flows in a pipeline
The following diagram describes how I/O events are processed by ChannelHandlers in a ChannelPipeline typically. An I/O event is handled by a ChannelHandler and is forwarded by the ChannelHandler which handled the event to the
ChannelHandler which is placed right next to it. A ChannelHandler can also trigger an arbitrary I/O event if necessary. To
forward or trigger an event, a ChannelHandler calls the event propagation methods defined in ChannelHandlerContext,
such as ChannelHandlerContext.fireChannelRead(Object) and ChannelHandlerContext.write(Object).

 

An inbound event is handled by the ChannelHandlers in the bottom-up direction as shown on the left side of the
diagram. An inbound event is usually triggered by the I/O thread on the bottom of the diagram so that the
ChannelHandlers are notified when the state of a Channel changes (e.g. newly established connections and closed connections) or the inbound data was read from a remote peer. If an inbound event goes beyond the ChannelHandler at the top of the diagram, it is discarded and logged, depending on your loglevel.
An outbound event is handled by the ChannelHandlers in the top-down direction as shown on the right side of the
diagram. An outbound event is usually triggered by your code that requests an outbound I/O operation, such as a write request and a connection attempt. If an outbound event goes beyond the ChannelHandler at the bottom of the diagram, it is handled by an I/O thread associated with the Channel. The I/O thread often performs the actual output operation such as SocketChannel.write(ByteBuffer).
Forwarding an event to the next handler
As explained briefly above, a ChannelHandler has to invoke the event propagation methods in ChannelHandlerContext
to forward an event to its next handler. Those methods include:
- Inbound event propagation methods:
  - ChannelHandlerContext.fireChannelRegistered()
  - ChannelHandlerContext.fireChannelActive()
  - ChannelHandlerContext.fireChannelRead(Object)
  - ChannelHandlerContext.fireChannelReadComplete()
  - ChannelHandlerContext.fireExceptionCaught(Throwable)
  - ChannelHandlerContext.fireUserEventTriggered(Object)
  - ChannelHandlerContext.fireChannelWritabilityChanged()
  - ChannelHandlerContext.fireChannelInactive()
- Outbound event propagation methods:
  - ChannelHandlerContext.bind(SocketAddress, ChannelPromise)
  - ChannelHandlerContext.connect(SocketAddress, SocketAddress, ChannelPromise)
  - ChannelHandlerContext.write(Object, ChannelPromise)
  - ChannelHandlerContext.flush()
  - ChannelHandlerContext.read()
  - ChannelHandlerContext.disconnect(ChannelPromise)
  - ChannelHandlerContext.close(ChannelPromise) and the following example shows how the event propagation is

时间: 2024-10-03 17:44:45

Netty 5.0的相关文章

Netty 4.0 源码分析(四):ByteBuf

Netty是基于流的消息传递机制.Netty框架中,所有消息的传输都依赖于ByteBuf接口,ByteBuf是Netty NIO框架中的缓冲区.ByteBuf接口可以理解为一般的Byte数组,不过Netty对Byte进行了封装,增加了一些实用的方法. ChannelBuf接口 package io.netty.buffer;public interface ChannelBuf {    ChannelBufType type();    boolean isPooled();} ByteBuf

netty 4.0.27 获得复用的channel

  EventLoopGroup group = new NioEventLoopGroup();         try {             Bootstrap b = new Bootstrap();             b.group(group);             b.channel(NioSocketChannel.class);             b.remoteAddress(new InetSocketAddress(host, port));     

netty 4.0 Object 传输

1对于服务端, private void bindPort(int port){ EventLoopGroup workGroup = new NioEventLoopGroup(); EventLoopGroup bossGroup = new NioEventLoopGroup(); try{ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workGroup); b.channel(NioServerSocketC

【转】Netty那点事(四)Netty与Reactor模式

[原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch4-reactor.md 一:Netty.NIO.多线程? 时隔很久终于又更新了!之前一直迟迟未动也是因为积累不够,后面比较难下手.过年期间@李林锋hw发布了一个Netty5.0架构剖析和源码解读 http://vdisk.weibo.com/s/C9LV9iVqH13rW/1391437855,看完也是收获不少.前面的文章我们分析了Netty的结构,这次咱们

【转】Netty那点事(二)Netty中的buffer

[原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch2-buffer.md 上一篇文章我们概要介绍了Netty的原理及结构,下面几篇文章我们开始对Netty的各个模块进行比较详细的分析.Netty的结构最底层是buffer机制,这部分也相对独立,我们就先从buffer讲起. What:buffer二三事 buffer中文名又叫缓冲区,按照维基百科的解释,是"在数据传输时,在内存里开辟的一块临时保存数据的区域&q

Netty权威指南

Netty权威指南(异步非阻塞通信领域的经典之作,国内首本深入剖析Netty的著作,全面系统讲解原理.实战和源码,带你完美进阶Netty工程师.) 李林锋 著   ISBN 978-7-121-23343-2 2014年6月出版 定价:79.00元 524页 16开 编辑推荐 - 资深一线专家诚意之作,总结多年实践经验,带你全面掌握Java高并发异步通信的首选框架——Netty. - Facebook.阿里巴巴.1号店.并发编程网.JBoss等多位资深技术专家联名力荐. <Netty权威指南>

java网络通信:netty

Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 也就是说,Netty 是一个基于NIO的客户,服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户,服务端应用.Netty相当简化和流线化了网络应用的编程开发过程,例如,TCP和UDP的socket服务开发. 本文示例采用netty 5.0.上代码. 服务端: import io.netty.bootstrap.ServerBootstr

电子书 netty权威指南第2版.pdf

<Netty quanwei指南(第2 版)>是异步非阻塞通信领域的经典之作,基于全新版本的Netty 5.0 编写,是国内首本深入介绍Netty 原理和架构的书籍,也是作者多年实战经验的总结和浓缩.内容不仅包含Java NIO入门知识.Netty 的基础功能开发指导.编解码框架定制等,还包括私有协议栈定制和开发.Netty 核心类库源码分析,以及Netty 的架构剖析. 作者简介 李林峰,Netty中国推广者,现华为技术有限公司平台中间件架构与设计部设计师,公司总裁技术创新奖获得者.长期从事

netty中的PlatformDependent

通过类名就知道这是一个平台有关的类,通过对该类的学习可以帮助我们实现一个跨平台的应用.但是有些方法放的实现不是很好,比如:isWindows0.通过File的separator就可以判断出来.没必要那么复杂把. 目前平台的判断是推崇能力判断,这里也是通过这样来判断是否是android的,jdk的版本.这种思想最初好像是js来判断浏览器的版本.还有就是PlatformDependent是一个对外的接口,真正的实现为PlatformDependent0类,这是一个代理模式吗 能力判断: Class.