View Javadoc
1   /*
2    * Copyright 2012 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    *   https://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.channel;
17  
18  import io.netty.channel.ChannelHandlerMask.Skip;
19  
20  import java.net.SocketAddress;
21  
22  /**
23   * {@link ChannelHandler} implementation which represents a combination out of a {@link ChannelInboundHandler} and
24   * the {@link ChannelOutboundHandler}.
25   *
26   * It is a good starting point if your {@link ChannelHandler} implementation needs to intercept operations and also
27   * state updates.
28   */
29  public class ChannelDuplexHandler extends ChannelInboundHandlerAdapter implements ChannelOutboundHandler {
30  
31      /**
32       * Calls {@link ChannelHandlerContext#bind(SocketAddress, ChannelPromise)} to forward
33       * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
34       *
35       * Sub-classes may override this method to change behavior.
36       */
37      @Skip
38      @Override
39      public void bind(ChannelHandlerContext ctx, SocketAddress localAddress,
40                       ChannelPromise promise) throws Exception {
41          ctx.bind(localAddress, promise);
42      }
43  
44      /**
45       * Calls {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress, ChannelPromise)} to forward
46       * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
47       *
48       * Sub-classes may override this method to change behavior.
49       */
50      @Skip
51      @Override
52      public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
53                          SocketAddress localAddress, ChannelPromise promise) throws Exception {
54          ctx.connect(remoteAddress, localAddress, promise);
55      }
56  
57      /**
58       * Calls {@link ChannelHandlerContext#disconnect(ChannelPromise)} to forward
59       * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
60       *
61       * Sub-classes may override this method to change behavior.
62       */
63      @Skip
64      @Override
65      public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise)
66              throws Exception {
67          ctx.disconnect(promise);
68      }
69  
70      /**
71       * Calls {@link ChannelHandlerContext#close(ChannelPromise)} to forward
72       * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
73       *
74       * Sub-classes may override this method to change behavior.
75       */
76      @Skip
77      @Override
78      public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
79          ctx.close(promise);
80      }
81  
82      /**
83       * Calls {@link ChannelHandlerContext#deregister(ChannelPromise)} to forward
84       * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
85       *
86       * Sub-classes may override this method to change behavior.
87       */
88      @Skip
89      @Override
90      public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
91          ctx.deregister(promise);
92      }
93  
94      /**
95       * Calls {@link ChannelHandlerContext#read()} to forward
96       * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
97       *
98       * Sub-classes may override this method to change behavior.
99       */
100     @Skip
101     @Override
102     public void read(ChannelHandlerContext ctx) throws Exception {
103         ctx.read();
104     }
105 
106     /**
107      * Calls {@link ChannelHandlerContext#write(Object, ChannelPromise)} to forward
108      * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
109      *
110      * Sub-classes may override this method to change behavior.
111      */
112     @Skip
113     @Override
114     public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
115         ctx.write(msg, promise);
116     }
117 
118     /**
119      * Calls {@link ChannelHandlerContext#flush()} to forward
120      * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
121      *
122      * Sub-classes may override this method to change behavior.
123      */
124     @Skip
125     @Override
126     public void flush(ChannelHandlerContext ctx) throws Exception {
127         ctx.flush();
128     }
129 }