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.ChannelHandlerContext;
19 import io.netty.channel.ChannelInboundHandlerAdapter;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27
28
29 public class ObjectEchoClientHandler extends ChannelInboundHandlerAdapter {
30
31 private final List<Integer> firstMessage;
32
33
34
35
36 public ObjectEchoClientHandler() {
37 firstMessage = new ArrayList<Integer>(ObjectEchoClient.SIZE);
38 for (int i = 0; i < ObjectEchoClient.SIZE; i ++) {
39 firstMessage.add(Integer.valueOf(i));
40 }
41 }
42
43 @Override
44 public void channelActive(ChannelHandlerContext ctx) {
45
46 ctx.writeAndFlush(firstMessage);
47 }
48
49 @Override
50 public void channelRead(ChannelHandlerContext ctx, Object msg) {
51
52 ctx.write(msg);
53 }
54
55 @Override
56 public void channelReadComplete(ChannelHandlerContext ctx) {
57 ctx.flush();
58 }
59
60 @Override
61 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
62 cause.printStackTrace();
63 ctx.close();
64 }
65 }