Netty 源碼研究

 

org.jboss.netty.bootstrap
自己 Netty 能夠做爲一個server存在的,所以他存在啓動入口,他具備client啓動,server啓動以及connectionless 啓動(好比UDP)
1.基類bootstrap:他包含ChannelFactory,ChannelPipeline,ChannelPipelineFactory。
ClientBootstrap: 有connect()方法
ConnectionlessBootstrap:有connect(),bind()方法
ServerBootstrap:有bind()方法java

2.org.jboss.netty.buffer
netty本身提供了buffer 來取代nio 中的java.nio.ByteBuffer,他與nio中的byteBuffer相比,有下面的幾個特色
1>能夠根據本身須要本身定義buffer type
2>只是buffer type改變而不拷貝buffer的內容,這樣能夠減小開銷
3>動態大小的buffer type 相似於stringbuffer
4>不須要調用flip()方法
5>更快的性能web


3.org.jboss.netty.channel
channel的核心api,包括異步和事件驅動等各類傳送接口
org.jboss.netty.channel.group
channel group,裏面包含一系列open的channelspring

org.jboss.netty.channel.local
一種虛擬的運輸方式,能容許同一個虛擬機上的兩個部分能夠互相通訊
org.jboss.netty.channel.socket
TCP,UDP端口接口,主要繼承channelbootstrap

org.jboss.netty.channel.socket.nio
基於nio端口channel的具體實現api

org.jboss.netty.channel.socket.oio
基於老的io端口的channel的具體實現websocket

org.jboss.netty.channel.socket.http
基於http的客戶端和相應的server端的實現,能夠在有防火牆的狀況下進行工做app

須要作的事情
a. 將http tunnel 做爲servlet進行配置
web.xml
<servlet>
 *     <servlet-name>NettyTunnelingServlet</servlet-name>
 *      <servlet-class>org.jboss.netty.channel.socket.http.HttpTunnelingServlet</servlet-class>
 *     <!--
 *       The name of the channel, this should be a registered local channel.
 *       See LocalTransportRegister.
 *     -->
 *     <init-param>
 *       <param-name>endpoint</param-name>
 *       <param-value>local:myLocalServer</param-value>
 *     </init-param>
 *     <load-on-startup>1</load-on-startup>
 *   </servlet>
 *
 *   <servlet-mapping>
 *     <servlet-name>NettyTunnelingServlet</servlet-name>
 *     <url-pattern>/netty-tunnel</url-pattern>
 *   </servlet-mapping>
接下來須要將你的基於netty的server app綁定到上面的http servlet
你能夠這樣寫
 *
 * public class LocalEchoServerRegistration {
 *
 *     private final ChannelFactory factory = new DefaultLocalServerChannelFactory();
 *     private volatile Channel serverChannel;
 *
 *     public void start() {
 *         ServerBootstrap serverBootstrap = new ServerBootstrap(factory);
 *         EchoHandler handler = new EchoHandler();
 *         serverBootstrap.getPipeline().addLast("handler", handler);
 *
 *         // Note that "myLocalServer" is the endpoint which was specified in web.xml.
 *         serverChannel = serverBootstrap.bind(new LocalAddress("myLocalServer"));
 *     }
 *
 *     public void stop() {
 *         serverChannel.close();
 *     }
 * }less

而後在Ioc framework(JBoss Microcontainer,Guice,Spring)中定義bean
<bean name="my-local-echo-server"
     class="org.jboss.netty.example.http.tunnel.LocalEchoServerRegistration" />異步

這樣http servlet 就能夠了socket

b. 鏈接http tunnel
構造client
 * ClientBootstrap b = new ClientBootstrap(
 *         new HttpTunnelingClientSocketChannelFactory(
 *                 new NioClientSocketChannelFactory(...)));
 *
 * // Configure the pipeline (or pipeline factory) here.
 * ...
 *
 * // The host name of the HTTP server
 * b.setOption("serverName", "example.com");
 * // The path to the HTTP tunneling Servlet, which was specified in in web.xml
 * b.setOption("serverPath", "contextPath/netty-tunnel");
 * b.connect(new InetSocketAddress("example.com", 80);

4.org.jboss.netty.container
各類容器的兼容
org.jboss.netty.container.microcontainer
JBoss Microcontainer 整合接口

org.jboss.netty.container.osgi
OSGi framework 整合接口

org.jboss.netty.container.spring
Spring framework 整合接口

5.org.jboss.netty.handler
處理器
org.jboss.netty.handler.codec.base64
Base64 編碼

org.jboss.netty.handler.codec.compression
壓縮格式

org.jboss.netty.handler.codec.embedder
嵌入模式下編碼和解碼,即便沒有真正的io環境也能使用
org.jboss.netty.handler.codec.frame
可擴展的接口,從新評估基於流的數據的排列和內容
org.jboss.netty.handler.codec.http.websocket
websocket相關的編碼和解碼,
參考
http://en.wikipedia.org/wiki/Web_Sockets

org.jboss.netty.handler.codec.http
http的編碼解碼以及類型信息

org.jboss.netty.handler.codec.oneone
一個對象到另外一對象的自定義抽象接口,若是有本身編碼須要繼承該抽象類

org.jboss.netty.handler.codec.protobuf
Google Protocol Buffers的編碼解碼
Google Protocol Buffers參考下面
http://code.google.com/p/protobuf/

org.jboss.netty.handler.codec.replay

org.jboss.netty.handler.codec.rtsp
Real_Time_Streaming_Protocol的編碼解碼
Real_Time_Streaming_Protocol 參考下面wiki
http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol

org.jboss.netty.handler.codec.serialization
將序列化的對象轉換到byte buffer的相關實現

org.jboss.netty.handler.codec.string
字符串編碼解碼,好比utf8編碼等,繼承oneone包的接口

org.jboss.netty.handler.execution
基於java.util.concurrent.Executor的實現

org.jboss.netty.handler.queue
將event存入內部隊列的處理

org.jboss.netty.handler.ssl
基於javax.net.ssl.SSLEngine的SSL以及TLS實現
參考
http://en.wikipedia.org/wiki/Transport_Layer_Security

org.jboss.netty.handler.stream
異步寫入大數據,不會產生outOfMemory 也不會花費不少內存

org.jboss.netty.handler.timeout
經過jboss.netty.util.Timer來對讀寫超時或者閒置連接的通知

6.org.jboss.netty.logging
根據不一樣的log framework 實現的類

7.org.jboss.netty.util
nettyutil類

org.jboss.netty.util.internal netty內部util類,不被外部使用

相關文章
相關標籤/搜索