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