1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.objectecho;
17
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.channel.ChannelInboundHandlerAdapter;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import static io.netty.channel.ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE;
26
27
28
29
30
31
32 public class ObjectEchoClientHandler extends ChannelInboundHandlerAdapter {
33
34 private final List<Integer> firstMessage;
35
36
37
38
39 public ObjectEchoClientHandler() {
40 firstMessage = new ArrayList<Integer>(ObjectEchoClient.SIZE);
41 for (int i = 0; i < ObjectEchoClient.SIZE; i ++) {
42 firstMessage.add(Integer.valueOf(i));
43 }
44 }
45
46 @Override
47 public void channelActive(ChannelHandlerContext ctx) {
48
49 ChannelFuture future = ctx.writeAndFlush(firstMessage);
50 future.addListener(FIRE_EXCEPTION_ON_FAILURE);
51 }
52
53 @Override
54 public void channelRead(ChannelHandlerContext ctx, Object msg) {
55
56 ctx.write(msg);
57 }
58
59 @Override
60 public void channelReadComplete(ChannelHandlerContext ctx) {
61 ctx.flush();
62 }
63
64 @Override
65 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
66 cause.printStackTrace();
67 ctx.close();
68 }
69 }