Netty之源代码解析

Netty始终要写一篇来作为终结的,但是到了写的时候才发现无从下手,了解 的还是不够吧。无奈,从四处摘录了一大片东西,很多都是官网下来的,没有什么文字说明,权当参考了。

首先来一张总体架构图,这个是从Neety官网上摘下来的,描述了Netty的核心架构和总体功能。
 

1.  BootStrap

 

Bootstrap : ChannelFactory, ChannelPipeline, ChannelPipelineFactory

初始化channel的辅助类

为具体的子类提供公共数据结构

ServerBootstrap: bind()

创建服务器端channel的辅助类

接收connection请求

ClientBootstrap: connect()

创建客户端channel的辅助类

发起connection请求

ConnectionlessBootstrap: connect() , bind()

创建无连接传输channel的辅助类(UDP)

包括Client 和Server

2.  Buffer

Buffer的作用在于取代nio中的java.nio.ByteBuffer,相比ByteBuffer,可以根据需要自定义buffertype。内置混合的buffertype, 以实现zero-copy。提供类似StringBuffer的动态dynamic buffer;不需要调用flip方法;推荐使用ChannelBuffers的静态工厂创建ChannelBuffer.

3.  channel

org.jboss.netty.channel

channel核心api,包括异步和事件驱动等各种传送接口

org.jboss.netty.channel.group

channel group,帮助用户维护channel列表

org.jboss.netty.channel.local

一种虚拟传输方式,允许同一个虚拟机上的两个部分可以互相通信

org.jboss.netty.channel.socket

TCP, UDP接口,继承了核心的channel API

org.jboss.netty.channel.socket.nio

基于nio的Socket channel实现

org.jboss.netty.channel.socket.oio

基于老io的Socket channel实现

org.jboss.netty.channel.socket.http

基于http的客户端和相应的server端的实现,工作在有防火墙的情况

Channel的核心包结构如下图所示:


4.  handler

org.jboss.netty.handler

处理器

org.jboss.netty.handler.codec

编码解码器

org.jboss.netty.handler.execution

基于Executor的实现

org.jboss.netty.handler.queue

将event存入内部队列的处理

org.jboss.netty.handler.ssl

基于SSLEngine的SSL以及TLS实现

org.jboss.netty.handler.stream

异步写入大数据,不会产生outOfMemory也不会花费很多内存

org.jboss.netty.handler.timeout

通过Timer来对读写超时或者闲置链接进行通知

 

5.  Netty的事件模型

 

5.1.  Netty Pipline

                                      I/O Request
                                     via Channel or
                                 ChannelHandlerContext
                                           |
  +----------------------------------------+---------------+
  |                  ChannelPipeline       |               |
  |                                       \|/              |
  |  +----------------------+  +-----------+------------+  |
  |  | Upstream Handler  N  |  | Downstream Handler  1  |  |
  |  +----------+-----------+  +-----------+------------+  |
  |            /|\                         |               |
  |             |                         \|/              |
  |  +----------+-----------+  +-----------+------------+  |
  |  | Upstream Handler N-1 |  | Downstream Handler  2  |  |
  |  +----------+-----------+  +-----------+------------+  |
  |            /|\                         .               |
  |             .                          .               |
  |     [ sendUpstream() ]        [ sendDownstream() ]     |
  |     [ + INBOUND data ]        [ + OUTBOUND data  ]     |
  |             .                          .               |
  |             .                         \|/              |
  |  +----------+-----------+  +-----------+------------+  |
  |  | Upstream Handler  2  |  | Downstream Handler M-1 |  |
  |  +----------+-----------+  +-----------+------------+  |
  |            /|\                         |               |
  |             |                         \|/              |
  |  +----------+-----------+  +-----------+------------+  |
  |  | Upstream Handler  1  |  | Downstream Handler  M  |  |
  |  +----------+-----------+  +-----------+------------+  |
  |            /|\                         |               |
  +-------------+--------------------------+---------------+
                |                         \|/
  +-------------+--------------------------+---------------+
  |             |                          |               |
  |     [ Socket.read() ]          [ Socket.write() ]      |
  |                                                        |
  |  Netty Internal I/O Threads (Transport Implementation) |
  +--------------------------------------------------------+

 

处理方式:

ChannelPipelinep = Channels.pipeline();

p.addLast("1", newUpstreamHandlerA());

p.addLast("2", newUpstreamHandlerB());

p.addLast("3", newDownstreamHandlerA());

p.addLast("4", newDownstreamHandlerB());

p.addLast("5", newUpstreamHandlerX());

Upstream: 1 –> 2 –> 5 顺序处理

Downstream: 4 –> 3 逆序处理

 

5.2.  ChannelState

 

Direction

State

Value

Meaning

Upstream

OPEN

true

The channel is open.

Upstream

OPEN

false

The channel is closed.

Upstream

BOUND

SocketAddress

The channel is bound to a local address.

Upstream

BOUND

null

The channel is unbound to a local address.

Upstream

CONNECTED

SocketAddress

The channel is connected to a remote address.

Upstream

CONNECTED

null

The channel is disconnected from a remote address.

Upstream

INTEREST_OPS

an integer

The channel interestOps has been changed.

Downstream

OPEN

true

N/A

Downstream

OPEN

false

Close the channel.

Downstream

BOUND

SocketAddress

Bind the channel to the specified local address.

Downstream

BOUND

null

Unbind the channel from the current local address.

Downstream

CONNECTED

SocketAddress

Connect the channel to the specified remote address.

Downstream

CONNECTED

null

Disconnect the channel from the current remote address.

Downstream

INTEREST_OPS

an integer

Change the interestOps of the channel.

 

5.3.  Upstreamevents

Event name

Event type and condition

Meaning

"messageReceived"

MessageEvent

a message object (e.g. ChannelBuffer) was received from a remote peer

"exceptionCaught"

ExceptionEvent

an exception was raised by an I/O thread or a ChannelHandler

"channelOpen"

ChannelStateEvent
(state = OPEN, value = true)

a Channel is open, but not bound nor connected

"channelClosed"

ChannelStateEvent
(state = OPEN, value = false)

a Channel was closed and all its related resources were released

"channelBound"

ChannelStateEvent
(state = BOUND, value = SocketAddress)

a Channel is open and bound to a local address, but not connected

"channelUnbound"

ChannelStateEvent
(state = BOUND, value = null)

a Channel was unbound from the current local address

"channelConnected"

ChannelStateEvent
(state = CONNECTED, value =SocketAddress)

a Channel is open, bound to a local address, and connected to a remote address

"writeComplete"

WriteCompletionEvent

something has been written to a remote peer

"channelDisconnected"

ChannelStateEvent
(state = CONNECTED, value = null)

a Channel was disconnected from its remote peer

"channelInterestChanged"

ChannelStateEvent
(state = INTEREST_OPS, no value)

a Channel's interestOps was changed

 

5.4.  Downstreamevents

Event name

Event type and condition

Meaning

"write"

MessageEvent

Send a message to the Channel.

"bind"

ChannelStateEvent
(state = BOUND, value = SocketAddress)

Bind the Channel to the specified local address.

"unbind"

ChannelStateEvent
(state = BOUND, value = null)

Unbind the Channel from the current local address.

"connect"

ChannelStateEvent
(state = CONNECTED, value = SocketAddress)

Connect the Channel to the specified remote address.

"disconnect"

ChannelStateEvent
(state = CONNECTED, value = null)

Disconnect the Channel from the current remote address.

"close"

ChannelStateEvent
(state = OPEN, value = false)

Close the Channel.

 

6.  Netty VS Mina

Netty基于Pipeline处理,Mina基于Filter过滤

Netty的事件驱动模型具有更好的扩展性和易用性

Https,SSL,PB,RSTP,Text&Binary等协议支持

Netty中UDP传输有更好的支持

官方测试Netty比Mina性能更好


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netty是一个基于Java的网络编程框架,它提供了高性能、异步事件驱动的网络应用程序开发能力。Netty可以用于构建各种类型的网络应用,包括客户端和服务器端。 要实现基于Netty的P2P代码,可以按照以下步骤进行: 1. 创建一个Server端和一个或多个Client端:首先,你需要创建一个Server端和一个或多个Client端。Server端用于接收来自其他Client端的连接请求,而Client端则用于与其他Client端建立连接。 2. 配置Server端和Client端的Bootstrap:使用Netty的Bootstrap类来配置Server端和Client端的启动参数。你可以设置监听的端口号、线程模型、处理器等。 3. 实现ChannelHandler:在Server端和Client端中,你需要实现自定义的ChannelHandler来处理接收到的消息。ChannelHandler是Netty中用于处理网络事件的组件,你可以在其中编写业务逻辑。 4. 编写消息传输逻辑:在ChannelHandler中,你可以编写消息传输的逻辑。例如,在Server端中,你可以接收来自Client端的消息,并将其转发给其他Client端;在Client端中,你可以发送消息给Server端或其他Client端。 5. 启动Server端和Client端:在代码中调用Server端和Client端的启动方法,启动它们并开始监听连接请求。 6. 进行P2P通信:一旦Server端和Client端都启动成功并建立连接,它们之间就可以进行P2P通信了。你可以通过发送和接收消息来实现点对点的通信。 这只是一个简单的概述,实际的代码实现可能会更加复杂,具体的实现方式还取决于你的需求和设计。你可以参考Netty的官方文档和示例代码来更深入地了解Netty的使用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值