查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2018 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.socket.nio;
17  
18  import io.netty.channel.ChannelException;
19  import io.netty.channel.ChannelOption;
20  import io.netty.util.internal.SuppressJava6Requirement;
21  
22  import java.io.IOException;
23  import java.nio.channels.Channel;
24  import java.nio.channels.ServerSocketChannel;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Set;
28  
29  /**
30   * Provides {@link ChannelOption} over a given {@link java.net.SocketOption} which is then passed through the underlying
31   * {@link java.nio.channels.NetworkChannel}.
32   */
33  @SuppressJava6Requirement(reason = "Usage explicit by the user")
34  public final class NioChannelOption<T> extends ChannelOption<T> {
35  
36      private final java.net.SocketOption<T> option;
37  
38      @SuppressWarnings("deprecation")
39      private NioChannelOption(java.net.SocketOption<T> option) {
40          super(option.name());
41          this.option = option;
42      }
43  
44      /**
45       * Returns a {@link ChannelOption} for the given {@link java.net.SocketOption}.
46       */
47      public static <T> ChannelOption<T> of(java.net.SocketOption<T> option) {
48          return new NioChannelOption<T>(option);
49      }
50  
51      // It's important to not use java.nio.channels.NetworkChannel as otherwise the classes that sometimes call this
52      // method may not be used on Java 6, as method linking can happen eagerly even if this method was not actually
53      // called at runtime.
54      //
55      // See https://github.com/netty/netty/issues/8166
56  
57      // Internal helper methods to remove code duplication between Nio*Channel implementations.
58      @SuppressJava6Requirement(reason = "Usage guarded by java version check")
59      static <T> boolean setOption(Channel jdkChannel, NioChannelOption<T> option, T value) {
60          java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
61          if (!channel.supportedOptions().contains(option.option)) {
62              return false;
63          }
64          if (channel instanceof ServerSocketChannel && option.option == java.net.StandardSocketOptions.IP_TOS) {
65              // Skip IP_TOS as a workaround for a JDK bug:
66              // See https://mail.openjdk.java.net/pipermail/nio-dev/2018-August/005365.html
67              return false;
68          }
69          try {
70              channel.setOption(option.option, value);
71              return true;
72          } catch (IOException e) {
73              throw new ChannelException(e);
74          }
75      }
76  
77      @SuppressJava6Requirement(reason = "Usage guarded by java version check")
78      static <T> T getOption(Channel jdkChannel, NioChannelOption<T> option) {
79          java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
80  
81          if (!channel.supportedOptions().contains(option.option)) {
82              return null;
83          }
84          if (channel instanceof ServerSocketChannel && option.option == java.net.StandardSocketOptions.IP_TOS) {
85              // Skip IP_TOS as a workaround for a JDK bug:
86              // See https://mail.openjdk.java.net/pipermail/nio-dev/2018-August/005365.html
87              return null;
88          }
89          try {
90              return channel.getOption(option.option);
91          } catch (IOException e) {
92              throw new ChannelException(e);
93          }
94      }
95  
96      @SuppressJava6Requirement(reason = "Usage guarded by java version check")
97      @SuppressWarnings("unchecked")
98      static ChannelOption[] getOptions(Channel jdkChannel) {
99          java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
100         Set<java.net.SocketOption<?>> supportedOpts = channel.supportedOptions();
101 
102         if (channel instanceof ServerSocketChannel) {
103             List<ChannelOption<?>> extraOpts = new ArrayList<ChannelOption<?>>(supportedOpts.size());
104             for (java.net.SocketOption<?> opt : supportedOpts) {
105                 if (opt == java.net.StandardSocketOptions.IP_TOS) {
106                     // Skip IP_TOS as a workaround for a JDK bug:
107                     // See https://mail.openjdk.java.net/pipermail/nio-dev/2018-August/005365.html
108                     continue;
109                 }
110                 extraOpts.add(new NioChannelOption(opt));
111             }
112             return extraOpts.toArray(new ChannelOption[0]);
113         } else {
114             ChannelOption<?>[] extraOpts = new ChannelOption[supportedOpts.size()];
115 
116             int i = 0;
117             for (java.net.SocketOption<?> opt : supportedOpts) {
118                 extraOpts[i++] = new NioChannelOption(opt);
119             }
120             return extraOpts;
121         }
122     }
123 }