查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2019 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.handler.address;
17  
18  import io.netty.channel.ChannelFuture;
19  import io.netty.channel.ChannelFutureListener;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.ChannelOutboundHandler;
22  import io.netty.channel.ChannelOutboundHandlerAdapter;
23  import io.netty.channel.ChannelPromise;
24  
25  import java.net.NetworkInterface;
26  import java.net.SocketAddress;
27  
28  /**
29   * {@link ChannelOutboundHandler} implementation which allows to dynamically replace the used
30   * {@code remoteAddress} and / or {@code localAddress} when making a connection attempt.
31   * <p>
32   * This can be useful to for example bind to a specific {@link NetworkInterface} based on
33   * the {@code remoteAddress}.
34   */
35  public abstract class DynamicAddressConnectHandler extends ChannelOutboundHandlerAdapter {
36  
37      @Override
38      public final void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
39                                SocketAddress localAddress, ChannelPromise promise) {
40          final SocketAddress remote;
41          final SocketAddress local;
42          try {
43              remote = remoteAddress(remoteAddress, localAddress);
44              local = localAddress(remoteAddress, localAddress);
45          } catch (Exception e) {
46              promise.setFailure(e);
47              return;
48          }
49          ctx.connect(remote, local, promise).addListener(new ChannelFutureListener() {
50              @Override
51              public void operationComplete(ChannelFuture future) {
52                  if (future.isSuccess()) {
53                      // We only remove this handler from the pipeline once the connect was successful as otherwise
54                      // the user may try to connect again.
55                      future.channel().pipeline().remove(DynamicAddressConnectHandler.this);
56                  }
57              }
58          });
59      }
60  
61      /**
62       * Returns the local {@link SocketAddress} to use for
63       * {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress)} based on the original {@code remoteAddress}
64       * and {@code localAddress}.
65       * By default, this method returns the given {@code localAddress}.
66       */
67      protected SocketAddress localAddress(
68              @SuppressWarnings("unused") SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
69          return localAddress;
70      }
71  
72      /**
73       * Returns the remote {@link SocketAddress} to use for
74       * {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress)} based on the original {@code remoteAddress}
75       * and {@code localAddress}.
76       * By default, this method returns the given {@code remoteAddress}.
77       */
78      protected SocketAddress remoteAddress(
79              SocketAddress remoteAddress, @SuppressWarnings("unused") SocketAddress localAddress) throws Exception {
80          return remoteAddress;
81      }
82  }