1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.discard;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelFutureListener;
21 import io.netty.channel.ChannelHandlerContext;
22 import io.netty.channel.SimpleChannelInboundHandler;
23
24
25
26
27 public class DiscardClientHandler extends SimpleChannelInboundHandler<Object> {
28
29 private ByteBuf content;
30 private ChannelHandlerContext ctx;
31
32 @Override
33 public void channelActive(ChannelHandlerContext ctx) {
34 this.ctx = ctx;
35
36
37 content = ctx.alloc().directBuffer(DiscardClient.SIZE).writeZero(DiscardClient.SIZE);
38
39
40 generateTraffic();
41 }
42
43 @Override
44 public void channelInactive(ChannelHandlerContext ctx) {
45 content.release();
46 }
47
48 @Override
49 public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
50
51 }
52
53 @Override
54 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
55
56 cause.printStackTrace();
57 ctx.close();
58 }
59
60 long counter;
61
62 private void generateTraffic() {
63
64
65 ctx.writeAndFlush(content.duplicate().retain()).addListener(trafficGenerator);
66 }
67
68 private final ChannelFutureListener trafficGenerator = new ChannelFutureListener() {
69 @Override
70 public void operationComplete(ChannelFuture future) {
71 if (future.isSuccess()) {
72 generateTraffic();
73 } else {
74 future.cause().printStackTrace();
75 future.channel().close();
76 }
77 }
78 };
79 }