2015-09-25 28 views
0

我正在寫一個應用程序,我需要跟蹤發送/接收的字節和帶寬。我正在使用本身使用Netty 5.0的庫。如何在netty中設置TrafficCounter?

我唯一的訪問Netty代碼是通過擴展我正在使用的庫的class

如何在此課程中創建和設置TrafficCounter?我找不到任何有關如何在網絡上做到這一點的例子(至少不是在Netty 5.0中)。我有多個使用Netty的線程,所以我需要每個通道的流量。

回答

0

你可以看看這個PR仍然包含關於如何使用TrafficCounter(其實TrafficShapingHandler)一個完整的例子:

https://github.com/fredericBregier/netty/tree/8f704e6020de364b031a77e1ee403d3ae4d8e10d/example/src/main/java/io/netty/example/discard

,特別是對於DiscardServer同時包含全局和信道業務塑造,最後一個(ChannelTrafficShaping)是你正在尋找的那個。

那麼你可以有如下:

 bootstrap.handler(new ChannelInitializer<Channel>() { 
      @Override 
      public void initChannel(Channel channel) throws Exception { 
       getPacketProtocol().newClientSession(client, TcpClientSession.this); 

       channel.config().setOption(ChannelOption.IP_TOS, 0x18); 
       channel.config().setOption(ChannelOption.TCP_NODELAY, false); 

       ChannelPipeline pipeline = channel.pipeline(); 

       refreshReadTimeoutHandler(channel); 
       refreshWriteTimeoutHandler(channel); 

       pipeline.addLast("encryption", new TcpPacketEncryptor(TcpClientSession.this)); 
       pipeline.addLast("traffic", new ChannelTrafficShapingHandler(0, MAXCHANNELTHROUGHPUT, 1000)); // ADDED 
       pipeline.addLast("sizer", new TcpPacketSizer(TcpClientSession.this)); 
       pipeline.addLast("codec", new TcpPacketCodec(TcpClientSession.this)); 
       pipeline.addLast("manager", TcpClientSession.this); 
      } 
     }).group(this.group).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout() * 1000); 

,並在API當然外觀:Traffic,特別ChannelTrafficShapingHandler