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 MessageToMessageEncoder<Object> encoder = new MessageToMessageEncoder<Object>() {
58
59 @Override
60 public boolean acceptOutboundMessage(Object msg) throws Exception {
61 return MessageToMessageCodec.this.acceptOutboundMessage(msg);
62 }
63
64 @Override
65 @SuppressWarnings("unchecked")
66 protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
67 MessageToMessageCodec.this.encode(ctx, (OUTBOUND_IN) msg, out);
68 }
69 };
70
71 private final MessageToMessageDecoder<Object> decoder = new MessageToMessageDecoder<Object>() {
72
73 @Override
74 public boolean acceptInboundMessage(Object msg) throws Exception {
75 return MessageToMessageCodec.this.acceptInboundMessage(msg);
76 }
77
78 @Override
79 @SuppressWarnings("unchecked")
80 protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
81 MessageToMessageCodec.this.decode(ctx, (INBOUND_IN) msg, out);
82 }
83 };
84
85 private final TypeParameterMatcher inboundMsgMatcher;
86 private final TypeParameterMatcher outboundMsgMatcher;
87
88
89
90
91
92 protected MessageToMessageCodec() {
93 inboundMsgMatcher = TypeParameterMatcher.find(this, MessageToMessageCodec.class, "INBOUND_IN");
94 outboundMsgMatcher = TypeParameterMatcher.find(this, MessageToMessageCodec.class, "OUTBOUND_IN");
95 }
96
97
98
99
100
101
102
103 protected MessageToMessageCodec(
104 Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType) {
105 inboundMsgMatcher = TypeParameterMatcher.get(inboundMessageType);
106 outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType);
107 }
108
109 @Override
110 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
111 decoder.channelRead(ctx, msg);
112 }
113
114 @Override
115 public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
116 encoder.write(ctx, msg, promise);
117 }
118
119
120
121
122
123
124 public boolean acceptInboundMessage(Object msg) throws Exception {
125 return inboundMsgMatcher.match(msg);
126 }
127
128
129
130
131
132
133 public boolean acceptOutboundMessage(Object msg) throws Exception {
134 return outboundMsgMatcher.match(msg);
135 }
136
137
138
139
140 protected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)
141 throws Exception;
142
143
144
145
146 protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out)
147 throws Exception;
148 }