<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Netty 4.0 源碼分析(二):Echo Server

    Netty項目中,自帶了很多使用的例子,對于剛剛開始接觸和學習Netty源碼的開發者來說,可以通過例子來更好的理解Netty的具體實現。源碼可以再netty 4.0的example找到。

     

     1 public class EchoServerHandler extends ChannelInboundByteHandlerAdapter {            
     2     private static final Logger logger = Logger.getLogger(
     3             EchoServerHandler.class.getName());
     4 
     5     @Override
     6     public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
     7         ByteBuf out = ctx.nextOutboundByteBuffer();
     8         out.discardReadBytes();
     9         out.writeBytes(in);
    10         ctx.flush();
    11     }
    12  
    13     @Override
    14     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    15         // Close the connection when an exception is raised.
    16         logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
    17         ctx.close();
    18     }
    19 }

    Line 1: 聲明一個EchoServerHandler, 并且繼承了ChannelInboundByteHandlerAdapter 這樣EchoServerHandler就可以處理client發送過來的request

    Line 6: 重寫inboundBufferUpdated方法,對client發送過來的request進行對ByteBuffer對象的操作。關于ByteBuffer的概念將在以后章節討論。

    Line 7: ctx.nextOutboundByteBuffer()將返回一個ByteBuffer對象。如果該對象不存在,那么拋出UnsupportedOperationException異常。

    Line 14: 重寫exceptionCaughtserver端捕獲異常。

     

     1 public class EchoServer {
     2  
     3     private final int port;
     4  
     5     public EchoServer(int port) {
     6         this.port = port;
     7     }
     8  
     9     public void run() throws Exception {
    10         // Configure the server.
    11         ServerBootstrap b = new ServerBootstrap();
    12         try {
    13             b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
    14              .channel(NioServerSocketChannel.class)
    15              .option(ChannelOption.SO_BACKLOG, 100)
    16              .localAddress(new InetSocketAddress(port))
    17              .childOption(ChannelOption.TCP_NODELAY, true)
    18              .handler(new LoggingHandler(LogLevel.INFO))
    19              .childHandler(new ChannelInitializer<SocketChannel>() {
    20                  @Override
    21                  public void initChannel(SocketChannel ch) throws Exception {
    22                      ch.pipeline().addLast(
    23                              new LoggingHandler(LogLevel.INFO),
    24                              new EchoServerHandler());
    25                  }
    26              });
    27  
    28             // Start the server.
    29             ChannelFuture f = b.bind().sync();
    30  
    31             // Wait until the server socket is closed.
    32             f.channel().closeFuture().sync();
    33         } finally {
    34             // Shut down all event loops to terminate all threads.
    35             b.shutdown();
    36         }
    37     }
    38  
    39     public static void main(String[] args) throws Exception {
    40         int port;
    41         if (args.length > 0) {
    42             port = Integer.parseInt(args[0]);
    43         } else {
    44             port = 8080;
    45         }
    46         new EchoServer(port).run();
    47     }
    48 }

     

     

    Line 11: 通過ServerBootStrap對象,來啟動服務器

    Line 13:  通過group方法,來綁定EventLoopGroupEventLoopGroup用來處理SocketChannelChannel上面的所有時間和IO

    Line 16: localAddress方法用于綁定服務器地址和端口。

    Line 18,19: handler方法和childhandler方法用于指定各種ChannelHandler對象,指定的ChannelHandler對象將用于處理client端來的request

    Line 21: 初始化一個Channel對象,并且綁定之前定義的EchoServerHandler類。

    Line 22:  EchoServerHandler添加到Pipeline中。

    Line 29: ServerBootstrap對象準備就緒,啟動server

    Line 32: 等待直到server socket關閉

     

    public class EchoClientHandler extends ChannelInboundByteHandlerAdapter {
     
        private static final Logger logger = Logger.getLogger(
                EchoClientHandler.class.getName());
     
        private final ByteBuf firstMessage;
     
        /**
         * Creates a client-side handler.
         
    */
        public EchoClientHandler(int firstMessageSize) {
            if (firstMessageSize <= 0) {
                throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize);
            }
            firstMessage = Unpooled.buffer(firstMessageSize);
            for (int i = 0; i < firstMessage.capacity(); i ++) {
                firstMessage.writeByte((byte) i);
            }
        }
     
        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            ctx.write(firstMessage);
        }
     
        @Override
        public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
            ByteBuf out = ctx.nextOutboundByteBuffer();
            out.discardReadBytes();
            out.writeBytes(in);
            ctx.flush();
        }
     
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            // Close the connection when an exception is raised.
            logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
            ctx.close();
        }
    }

     

    EchoClientHandler類的實現與EchoServerHandler類似,也都繼承了ChannelInboundByteHandlerAdapter。不同在于重寫了channelActive()方法。

     

     1 public class EchoClient {
     2  
     3     private final String host;
     4     private final int port;
     5     private final int firstMessageSize;
     6  
     7     public EchoClient(String host, int port, int firstMessageSize) {
     8         this.host = host;
     9         this.port = port;
    10         this.firstMessageSize = firstMessageSize;
    11     }
    12  
    13     public void run() throws Exception {
    14         // Configure the client.
    15         Bootstrap b = new Bootstrap();
    16         try {
    17             b.group(new NioEventLoopGroup())
    18              .channel(NioSocketChannel.class)
    19              .option(ChannelOption.TCP_NODELAY, true)
    20              .remoteAddress(new InetSocketAddress(host, port))
    21              .handler(new ChannelInitializer<SocketChannel>() {
    22                  @Override
    23                  public void initChannel(SocketChannel ch) throws Exception {
    24                      ch.pipeline().addLast(
    25                              new LoggingHandler(LogLevel.INFO),
    26                              new EchoClientHandler(firstMessageSize));
    27                  }
    28              });
    29  
    30             // Start the client.
    31             ChannelFuture f = b.connect().sync();
    32  
    33             // Wait until the connection is closed.
    34             f.channel().closeFuture().sync();
    35         } finally {
    36             // Shut down the event loop to terminate all threads.
    37             b.shutdown();
    38         }
    39     }
    40  
    41     public static void main(String[] args) throws Exception {
    42         // Print usage if no argument is specified.
    43         if (args.length < 2 || args.length > 3) {
    44             System.err.println(
    45                     "Usage: " + EchoClient.class.getSimpleName() +
    46                     " <host> <port> [<first message size>]");
    47             return;
    48         }
    49  
    50         // Parse options.
    51         final String host = args[0];
    52         final int port = Integer.parseInt(args[1]);
    53         final int firstMessageSize;
    54         if (args.length == 3) {
    55             firstMessageSize = Integer.parseInt(args[2]);
    56         } else {
    57             firstMessageSize = 256;
    58         }
    59  
    60         new EchoClient(host, port, firstMessageSize).run();
    61     }
    62 }

     

    Line 20: 指定遠程服務器的地址和端口(localhost:8080


    備注:因為筆者開始寫Netty源碼分析的時候,Netty 4.0還是處于Alpha階段,之后的API可能還會有改動,筆者將會及時更改。使用開源已經有好幾年的時間了,一直沒有時間和精力來具體研究某個開源項目的具體實現,這次是第一次寫開源項目的源碼分析,如果文中有錯誤的地方,歡迎讀者可以留言指出。對于轉載的讀者,請注明文章的出處。
    希望和廣大的開發者/開源愛好者進行交流,歡迎大家的留言和討論。



    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-11-24 12:38 Chan Chen 閱讀(7076) 評論(1)  編輯  收藏 所屬分類: Netty

    評論

    # re: Netty 4.0 源碼分析(二):Echo Server[未登錄] 2013-01-23 15:05

    源碼有,但是具體怎么使用。不怎么會!請教。實現一個最簡單的例子的講解  回復  更多評論   

    主站蜘蛛池模板: 国产偷国产偷亚洲高清日韩| 成人免费无码精品国产电影| 亚洲A∨无码无在线观看| 国产区在线免费观看| 永久亚洲成a人片777777| 国产免费内射又粗又爽密桃视频| 亚洲人成影院在线观看| 一个人免费观看日本www视频| 成人亚洲网站www在线观看| 一级做a爰片久久毛片免费陪 | 亚洲av日韩av欧v在线天堂| 妇女自拍偷自拍亚洲精品| mm1313亚洲精品无码又大又粗| 亚洲免费视频一区二区三区| 亚洲精品视频在线看| 国产免费一区二区三区不卡| 亚洲av无码专区国产乱码在线观看| 久久久久免费精品国产小说| 亚洲精品第一国产综合精品| 大陆一级毛片免费视频观看i| 国产亚洲Av综合人人澡精品| 亚洲中文字幕久久精品无码APP | 一区二区视频在线免费观看| 精品国产亚洲一区二区三区| 中文字幕成人免费视频| 色婷五月综激情亚洲综合| 哒哒哒免费视频观看在线www| a级毛片无码免费真人久久| 亚洲精品mv在线观看| 国产三级免费观看| 91成人免费观看在线观看| 亚洲国产电影在线观看| 四虎1515hm免费国产| 免费视频一区二区| 亚洲中文字幕一二三四区| 国产日韩成人亚洲丁香婷婷| 精品成在人线AV无码免费看| 一级毛片不卡免费看老司机| 亚洲冬月枫中文字幕在线看| 亚洲视频在线一区二区| 91精品视频免费|