1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec;
17
18 import io.netty.channel.ChannelDuplexHandler;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.channel.ChannelPromise;
21 import io.netty.util.ReferenceCounted;
22 import io.netty.util.internal.TypeParameterMatcher;
23
24 import java.util.List;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public abstract class MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN> extends ChannelDuplexHandler {
56
57 private final MessageToMessageDecoder<Object> decoder = new MessageToMessageDecoder<Object>() {
58
59 @Override
60 public boolean acceptInboundMessage(Object msg) throws Exception {
61 return MessageToMessageCodec.this.acceptInboundMessage(msg);
62 }
63
64 @Override
65 @SuppressWarnings("unchecked")
66 protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
67 MessageToMessageCodec.this.decode(ctx, (INBOUND_IN) msg, out);
68 }
69
70 @Override
71 public boolean isSharable() {
72 return MessageToMessageCodec.this.isSharable();
73 }
74 };
75 private final MessageToMessageEncoder<Object> encoder = new MessageToMessageEncoder<Object>() {
76
77 @Override
78 public boolean acceptOutboundMessage(Object msg) throws Exception {
79 return MessageToMessageCodec.this.acceptOutboundMessage(msg);
80 }
81
82 @Override
83 @SuppressWarnings("unchecked")
84 protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
85 MessageToMessageCodec.this.encode(ctx, (OUTBOUND_IN) msg, out);
86 }
87
88 @Override
89 public boolean isSharable() {
90 return MessageToMessageCodec.this.isSharable();
91 }
92 };
93
94 private final TypeParameterMatcher inboundMsgMatcher;
95 private final TypeParameterMatcher outboundMsgMatcher;
96
97
98
99
100
101 protected MessageToMessageCodec() {
102 inboundMsgMatcher = TypeParameterMatcher.find(this, MessageToMessageCodec.class, "INBOUND_IN");
103 outboundMsgMatcher = TypeParameterMatcher.find(this, MessageToMessageCodec.class, "OUTBOUND_IN");
104 }
105
106
107
108
109
110
111
112 protected MessageToMessageCodec(
113 Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType) {
114 inboundMsgMatcher = TypeParameterMatcher.get(inboundMessageType);
115 outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType);
116 }
117
118 @Override
119 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
120 decoder.channelRead(ctx, msg);
121 }
122
123 @Override
124 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
125 decoder.channelReadComplete(ctx);
126 }
127
128 @Override
129 public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
130 encoder.write(ctx, msg, promise);
131 }
132
133
134
135
136
137
138 public boolean acceptInboundMessage(Object msg) throws Exception {
139 return inboundMsgMatcher.match(msg);
140 }
141
142
143
144
145
146
147 public boolean acceptOutboundMessage(Object msg) throws Exception {
148 return outboundMsgMatcher.match(msg);
149 }
150
151
152
153
154 protected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)
155 throws Exception;
156
157
158
159
160 protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out)
161 throws Exception;
162 }