View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.channel.ChannelPipeline;
21  import io.netty.channel.socket.DatagramPacket;
22  import io.netty.handler.codec.protobuf.ProtobufDecoder;
23  import static io.netty.util.internal.ObjectUtil.checkNotNull;
24  
25  import java.util.List;
26  
27  /**
28   * A decoder that decodes the content of the received {@link DatagramPacket} using
29   * the specified {@link ByteBuf} decoder. E.g.,
30   *
31   * <pre><code>
32   * {@link ChannelPipeline} pipeline = ...;
33   * pipeline.addLast("udpDecoder", new {@link DatagramPacketDecoder}(new {@link ProtobufDecoder}(...));
34   * </code></pre>
35   */
36  public class DatagramPacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
37  
38      private final MessageToMessageDecoder<ByteBuf> decoder;
39  
40      /**
41       * Create a {@link DatagramPacket} decoder using the specified {@link ByteBuf} decoder.
42       *
43       * @param decoder the specified {@link ByteBuf} decoder
44       */
45      public DatagramPacketDecoder(MessageToMessageDecoder<ByteBuf> decoder) {
46          this.decoder = checkNotNull(decoder, "decoder");
47      }
48  
49      @Override
50      public boolean acceptInboundMessage(Object msg) throws Exception {
51          if (msg instanceof DatagramPacket) {
52              return decoder.acceptInboundMessage(((DatagramPacket) msg).content());
53          }
54          return false;
55      }
56  
57      @Override
58      protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
59          decoder.decode(ctx, msg.content(), out);
60      }
61  
62      @Override
63      public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
64          decoder.channelRegistered(ctx);
65      }
66  
67      @Override
68      public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
69          decoder.channelUnregistered(ctx);
70      }
71  
72      @Override
73      public void channelActive(ChannelHandlerContext ctx) throws Exception {
74          decoder.channelActive(ctx);
75      }
76  
77      @Override
78      public void channelInactive(ChannelHandlerContext ctx) throws Exception {
79          decoder.channelInactive(ctx);
80      }
81  
82      @Override
83      public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
84          decoder.channelReadComplete(ctx);
85      }
86  
87      @Override
88      public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
89          decoder.userEventTriggered(ctx, evt);
90      }
91  
92      @Override
93      public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
94          decoder.channelWritabilityChanged(ctx);
95      }
96  
97      @Override
98      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
99          decoder.exceptionCaught(ctx, cause);
100     }
101 
102     @Override
103     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
104         decoder.handlerAdded(ctx);
105     }
106 
107     @Override
108     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
109         decoder.handlerRemoved(ctx);
110     }
111 
112     @Override
113     public boolean isSharable() {
114         return decoder.isSharable();
115     }
116 }