netty 4.x源码分析

 

       服务端需要经过socket、bind、accept、read、write等步骤,客户端需要经过socket、connect、read、write等步骤,后续此系列文章会对每一个步骤如何发生进行分析。

 

  1. netty4源码分析-线程的创建
  2. netty4源码分析-socket
  3. netty4源码分析-bind
  4.  Netty4源码分析-NioEventLoop实现的线程运行逻辑

  5. netty4源码分析-connect
  6. netty4源码分析-accept
  7. netty4源码分析-write
  8. netty4源码分析-flush
  9. netty4源码分析-read
  10. 通信总结

以下是服务端和客户端的测试代码,后面的分析以这份代码为基础,目前使用的Netty的版本是4.0.4.Final及以上

 

Java代码    收藏代码
  1. 服务端:  
  2. /* 
  3.  * Copyright 2012 The Netty Project 
  4.  * 
  5.  * The Netty Project licenses this file to you under the Apache License, 
  6.  * version 2.0 (the "License"); you may not use this file except in compliance 
  7.  * with the License. You may obtain a copy of the License at: 
  8.  * 
  9.  *   http://www.apache.org/licenses/LICENSE-2.0 
  10.  * 
  11.  * Unless required by applicable law or agreed to in writing, software 
  12.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  13.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  14.  * License for the specific language governing permissions and limitations 
  15.  * under the License. 
  16.  */  
  17. package io.netty.example.echo;  
  18.   
  19. import io.netty.bootstrap.ServerBootstrap;  
  20. import io.netty.channel.ChannelFuture;  
  21. import io.netty.channel.ChannelInitializer;  
  22. import io.netty.channel.ChannelOption;  
  23. import io.netty.channel.EventLoopGroup;  
  24. import io.netty.channel.nio.NioEventLoopGroup;  
  25. import io.netty.channel.socket.SocketChannel;  
  26. import io.netty.channel.socket.nio.NioServerSocketChannel;  
  27. import io.netty.handler.logging.LogLevel;  
  28. import io.netty.handler.logging.LoggingHandler;  
  29.   
  30. /** 
  31.  * Echoes back any received data from a client. 
  32.  */  
  33. public class EchoServer {  
  34.   
  35.     private final int port;  
  36.   
  37.     public EchoServer(int port) {  
  38.         this.port = port;  
  39.     }  
  40.   
  41.     public void run() throws Exception {  
  42.         // Configure the server.  
  43.         EventLoopGroup bossGroup = new NioEventLoopGroup();  
  44.         EventLoopGroup workerGroup = new NioEventLoopGroup();  
  45.         try {  
  46.             ServerBootstrap b = new ServerBootstrap();  
  47.             b.group(bossGroup, workerGroup)  
  48.              .channel(NioServerSocketChannel.class)  
  49.              .option(ChannelOption.SO_BACKLOG, 100)  
  50.              .handler(new LoggingHandler(LogLevel.INFO))  
  51.              .childHandler(new ChannelInitializer<SocketChannel>() {  
  52.                  @Override  
  53.                  public void initChannel(SocketChannel ch) throws Exception {  
  54.                      ch.pipeline().addLast(  
  55.                              //new LoggingHandler(LogLevel.INFO),  
  56.                              new EchoServerHandler());  
  57.                  }  
  58.              });  
  59.   
  60.             // Start the server.  
  61.             ChannelFuture f = b.bind(port).sync();  
  62.   
  63.             // Wait until the server socket is closed.  
  64.             f.channel().closeFuture().sync();  
  65.         } finally {  
  66.             // Shut down all event loops to terminate all threads.  
  67.             bossGroup.shutdownGracefully();  
  68.             workerGroup.shutdownGracefully();  
  69.         }  
  70.     }  
  71.   
  72.     public static void main(String[] args) throws Exception {  
  73.         int port;  
  74.         if (args.length > 0) {  
  75.             port = Integer.parseInt(args[0]);  
  76.         } else {  
  77.             port = 8080;  
  78.         }  
  79.         new EchoServer(port).run();  
  80.     }  
  81. }  

 

Java代码    收藏代码
  1. /* 
  2.  * Copyright 2012 The Netty Project 
  3.  * 
  4.  * The Netty Project licenses this file to you under the Apache License, 
  5.  * version 2.0 (the "License"); you may not use this file except in compliance 
  6.  * with the License. You may obtain a copy of the License at: 
  7.  * 
  8.  *   http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  13.  * License for the specific language governing permissions and limitations 
  14.  * under the License. 
  15.  */  
  16. package io.netty.example.echo;  
  17.   
  18. import io.netty.channel.ChannelHandler.Sharable;  
  19. import io.netty.channel.ChannelHandlerContext;  
  20. import io.netty.channel.ChannelInboundHandlerAdapter;  
  21.   
  22. import java.util.logging.Level;  
  23. import java.util.logging.Logger;  
  24.   
  25. /** 
  26.  * Handler implementation for the echo server. 
  27.  */  
  28. @Sharable  
  29. public class EchoServerHandler extends ChannelInboundHandlerAdapter {  
  30.   
  31.     private static final Logger logger = Logger.getLogger(  
  32.             EchoServerHandler.class.getName());  
  33.   
  34.     @Override  
  35.     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
  36.         ctx.write(msg);  
  37.     }  
  38.   
  39.     @Override  
  40.     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {  
  41.         ctx.flush();  
  42.     }  
  43.   
  44.     @Override  
  45.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {  
  46.         // Close the connection when an exception is raised.  
  47.         logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);  
  48.         ctx.close();  
  49.     }  
  50. }  

 

Java代码    收藏代码
  1. 客户端:  
  2. /* 
  3.  * Copyright 2012 The Netty Project 
  4.  * 
  5.  * The Netty Project licenses this file to you under the Apache License, 
  6.  * version 2.0 (the "License"); you may not use this file except in compliance 
  7.  * with the License. You may obtain a copy of the License at: 
  8.  * 
  9.  *   http://www.apache.org/licenses/LICENSE-2.0 
  10.  * 
  11.  * Unless required by applicable law or agreed to in writing, software 
  12.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  13.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  14.  * License for the specific language governing permissions and limitations 
  15.  * under the License. 
  16.  */  
  17. package io.netty.example.echo;  
  18.   
  19. import io.netty.bootstrap.Bootstrap;  
  20. import io.netty.channel.ChannelFuture;  
  21. import io.netty.channel.ChannelInitializer;  
  22. import io.netty.channel.ChannelOption;  
  23. import io.netty.channel.EventLoopGroup;  
  24. import io.netty.channel.nio.NioEventLoopGroup;  
  25. import io.netty.channel.socket.SocketChannel;  
  26. import io.netty.channel.socket.nio.NioSocketChannel;  
  27.   
  28. /** 
  29.  * Sends one message when a connection is open and echoes back any received 
  30.  * data to the server.  Simply put, the echo client initiates the ping-pong 
  31.  * traffic between the echo client and server by sending the first message to 
  32.  * the server. 
  33.  */  
  34. public class EchoClient {  
  35.   
  36.     private final String host;  
  37.     private final int port;  
  38.     private final int firstMessageSize;  
  39.   
  40.     public EchoClient(String host, int port, int firstMessageSize) {  
  41.         this.host = host;  
  42.         this.port = port;  
  43.         this.firstMessageSize = firstMessageSize;  
  44.     }  
  45.   
  46.     public void run() throws Exception {  
  47.         // Configure the client.  
  48.         EventLoopGroup group = new NioEventLoopGroup();  
  49.         try {  
  50.             Bootstrap b = new Bootstrap();  
  51.             b.group(group)  
  52.              .channel(NioSocketChannel.class)  
  53.              .option(ChannelOption.TCP_NODELAY, true)  
  54.              .handler(new ChannelInitializer<SocketChannel>() {  
  55.                  @Override  
  56.                  public void initChannel(SocketChannel ch) throws Exception {  
  57.                      ch.pipeline().addLast(  
  58.                              //new LoggingHandler(LogLevel.INFO),  
  59.                              new EchoClientHandler(firstMessageSize));  
  60.                  }  
  61.              });  
  62.   
  63.             // Start the client.  
  64.             ChannelFuture f = b.connect(host, port).sync();  
  65.   
  66.             // Wait until the connection is closed.  
  67.             f.channel().closeFuture().sync();  
  68.         } finally {  
  69.             // Shut down the event loop to terminate all threads.  
  70.             group.shutdownGracefully();  
  71.         }  
  72.     }  
  73.   
  74.     public static void main(String[] args) throws Exception {  
  75.         // Print usage if no argument is specified.  
  76.         if (args.length < 2 || args.length > 3) {  
  77.             System.err.println(  
  78.                     "Usage: " + EchoClient.class.getSimpleName() +  
  79.                     " <host> <port> [<first message size>]");  
  80.             return;  
  81.         }  
  82.   
  83.         // Parse options.  
  84.         final String host = args[0];  
  85.         final int port = Integer.parseInt(args[1]);  
  86.         final int firstMessageSize;  
  87.         if (args.length == 3) {  
  88.             firstMessageSize = Integer.parseInt(args[2]);  
  89.         } else {  
  90.             firstMessageSize = 256;  
  91.         }  
  92.   
  93.         new EchoClient(host, port, firstMessageSize).run();  
  94.     }  
  95. }  

 

Java代码    收藏代码
  1. /* 
  2.  * Copyright 2012 The Netty Project 
  3.  * 
  4.  * The Netty Project licenses this file to you under the Apache License, 
  5.  * version 2.0 (the "License"); you may not use this file except in compliance 
  6.  * with the License. You may obtain a copy of the License at: 
  7.  * 
  8.  *   http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  13.  * License for the specific language governing permissions and limitations 
  14.  * under the License. 
  15.  */  
  16. package io.netty.example.echo;  
  17.   
  18. import io.netty.buffer.ByteBuf;  
  19. import io.netty.buffer.Unpooled;  
  20. import io.netty.channel.ChannelHandlerContext;  
  21. import io.netty.channel.ChannelInboundHandlerAdapter;  
  22.   
  23. import java.util.logging.Level;  
  24. import java.util.logging.Logger;  
  25.   
  26. /** 
  27.  * Handler implementation for the echo client.  It initiates the ping-pong 
  28.  * traffic between the echo client and server by sending the first message to 
  29.  * the server. 
  30.  */  
  31. public class EchoClientHandler extends ChannelInboundHandlerAdapter {  
  32.   
  33.     private static final Logger logger = Logger.getLogger(  
  34.             EchoClientHandler.class.getName());  
  35.   
  36.     private final ByteBuf firstMessage;  
  37.   
  38.     /** 
  39.      * Creates a client-side handler. 
  40.      */  
  41.     public EchoClientHandler(int firstMessageSize) {  
  42.         if (firstMessageSize <= 0) {  
  43.             throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize);  
  44.         }  
  45.         firstMessage = Unpooled.buffer(firstMessageSize);  
  46.         for (int i = 0; i < firstMessage.capacity(); i ++) {  
  47.             firstMessage.writeByte((byte) i);  
  48.         }  
  49.     }  
  50.   
  51.     @Override  
  52.     public void channelActive(ChannelHandlerContext ctx) {  
  53.         ctx.writeAndFlush(firstMessage);  
  54.     }  
  55.   
  56.     @Override  
  57.     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
  58.         ctx.write(msg);  
  59.     }  
  60.   
  61.     @Override  
  62.     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {  
  63.        ctx.flush();  
  64.     }  
  65.   
  66.     @Override  
  67.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {  
  68.         // Close the connection when an exception is raised.  
  69.         logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);  
  70.         ctx.close();  
  71.     }  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值