查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.buffer.ByteBufAllocator;
19  import io.netty.util.AbstractConstant;
20  import io.netty.util.ConstantPool;
21  import io.netty.util.internal.ObjectUtil;
22  
23  import java.net.InetAddress;
24  import java.net.NetworkInterface;
25  
26  /**
27   * A {@link ChannelOption} allows to configure a {@link ChannelConfig} in a type-safe
28   * way. Which {@link ChannelOption} is supported depends on the actual implementation
29   * of {@link ChannelConfig} and may depend on the nature of the transport it belongs
30   * to.
31   *
32   * @param <T>   the type of the value which is valid for the {@link ChannelOption}
33   */
34  public class ChannelOption<T> extends AbstractConstant<ChannelOption<T>> {
35  
36      private static final ConstantPool<ChannelOption<Object>> pool = new ConstantPool<ChannelOption<Object>>() {
37          @Override
38          protected ChannelOption<Object> newConstant(int id, String name) {
39              return new ChannelOption<Object>(id, name);
40          }
41      };
42  
43      /**
44       * Returns the {@link ChannelOption} of the specified name.
45       */
46      @SuppressWarnings("unchecked")
47      public static <T> ChannelOption<T> valueOf(String name) {
48          return (ChannelOption<T>) pool.valueOf(name);
49      }
50  
51      /**
52       * Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
53       */
54      @SuppressWarnings("unchecked")
55      public static <T> ChannelOption<T> valueOf(Class<?> firstNameComponent, String secondNameComponent) {
56          return (ChannelOption<T>) pool.valueOf(firstNameComponent, secondNameComponent);
57      }
58  
59      /**
60       * Returns {@code true} if a {@link ChannelOption} exists for the given {@code name}.
61       */
62      public static boolean exists(String name) {
63          return pool.exists(name);
64      }
65  
66      /**
67       * Creates a new {@link ChannelOption} for the given {@code name} or fail with an
68       * {@link IllegalArgumentException} if a {@link ChannelOption} for the given {@code name} exists.
69       *
70       * @deprecated use {@link #valueOf(String)}.
71       */
72      @Deprecated
73      @SuppressWarnings("unchecked")
74      public static <T> ChannelOption<T> newInstance(String name) {
75          return (ChannelOption<T>) pool.newInstance(name);
76      }
77  
78      public static final ChannelOption<ByteBufAllocator> ALLOCATOR = valueOf("ALLOCATOR");
79      public static final ChannelOption<RecvByteBufAllocator> RCVBUF_ALLOCATOR = valueOf("RCVBUF_ALLOCATOR");
80      public static final ChannelOption<MessageSizeEstimator> MESSAGE_SIZE_ESTIMATOR = valueOf("MESSAGE_SIZE_ESTIMATOR");
81  
82      public static final ChannelOption<Integer> CONNECT_TIMEOUT_MILLIS = valueOf("CONNECT_TIMEOUT_MILLIS");
83      /**
84       * @deprecated Use {@link MaxMessagesRecvByteBufAllocator}
85       * and {@link MaxMessagesRecvByteBufAllocator#maxMessagesPerRead(int)}.
86       */
87      @Deprecated
88      public static final ChannelOption<Integer> MAX_MESSAGES_PER_READ = valueOf("MAX_MESSAGES_PER_READ");
89      public static final ChannelOption<Integer> MAX_MESSAGES_PER_WRITE = valueOf("MAX_MESSAGES_PER_WRITE");
90  
91      public static final ChannelOption<Integer> WRITE_SPIN_COUNT = valueOf("WRITE_SPIN_COUNT");
92      /**
93       * @deprecated Use {@link #WRITE_BUFFER_WATER_MARK}
94       */
95      @Deprecated
96      public static final ChannelOption<Integer> WRITE_BUFFER_HIGH_WATER_MARK = valueOf("WRITE_BUFFER_HIGH_WATER_MARK");
97      /**
98       * @deprecated Use {@link #WRITE_BUFFER_WATER_MARK}
99       */
100     @Deprecated
101     public static final ChannelOption<Integer> WRITE_BUFFER_LOW_WATER_MARK = valueOf("WRITE_BUFFER_LOW_WATER_MARK");
102     public static final ChannelOption<WriteBufferWaterMark> WRITE_BUFFER_WATER_MARK =
103             valueOf("WRITE_BUFFER_WATER_MARK");
104 
105     public static final ChannelOption<Boolean> ALLOW_HALF_CLOSURE = valueOf("ALLOW_HALF_CLOSURE");
106     public static final ChannelOption<Boolean> AUTO_READ = valueOf("AUTO_READ");
107 
108     /**
109      * If {@code true} then the {@link Channel} is closed automatically and immediately on write failure.
110      * The default value is {@code true}.
111      */
112     public static final ChannelOption<Boolean> AUTO_CLOSE = valueOf("AUTO_CLOSE");
113 
114     public static final ChannelOption<Boolean> SO_BROADCAST = valueOf("SO_BROADCAST");
115     public static final ChannelOption<Boolean> SO_KEEPALIVE = valueOf("SO_KEEPALIVE");
116     public static final ChannelOption<Integer> SO_SNDBUF = valueOf("SO_SNDBUF");
117     public static final ChannelOption<Integer> SO_RCVBUF = valueOf("SO_RCVBUF");
118     public static final ChannelOption<Boolean> SO_REUSEADDR = valueOf("SO_REUSEADDR");
119     public static final ChannelOption<Integer> SO_LINGER = valueOf("SO_LINGER");
120     public static final ChannelOption<Integer> SO_BACKLOG = valueOf("SO_BACKLOG");
121     public static final ChannelOption<Integer> SO_TIMEOUT = valueOf("SO_TIMEOUT");
122 
123     public static final ChannelOption<Integer> IP_TOS = valueOf("IP_TOS");
124     public static final ChannelOption<InetAddress> IP_MULTICAST_ADDR = valueOf("IP_MULTICAST_ADDR");
125     public static final ChannelOption<NetworkInterface> IP_MULTICAST_IF = valueOf("IP_MULTICAST_IF");
126     public static final ChannelOption<Integer> IP_MULTICAST_TTL = valueOf("IP_MULTICAST_TTL");
127     public static final ChannelOption<Boolean> IP_MULTICAST_LOOP_DISABLED = valueOf("IP_MULTICAST_LOOP_DISABLED");
128 
129     public static final ChannelOption<Boolean> TCP_NODELAY = valueOf("TCP_NODELAY");
130     /**
131      * Client-side TCP FastOpen. Sending data with the initial TCP handshake.
132      */
133     public static final ChannelOption<Boolean> TCP_FASTOPEN_CONNECT = valueOf("TCP_FASTOPEN_CONNECT");
134 
135     /**
136      * Server-side TCP FastOpen. Configures the maximum number of outstanding (waiting to be accepted) TFO connections.
137      */
138     public static final ChannelOption<Integer> TCP_FASTOPEN = valueOf(ChannelOption.class, "TCP_FASTOPEN");
139 
140     @Deprecated
141     public static final ChannelOption<Boolean> DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION =
142             valueOf("DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION");
143 
144     public static final ChannelOption<Boolean> SINGLE_EVENTEXECUTOR_PER_GROUP =
145             valueOf("SINGLE_EVENTEXECUTOR_PER_GROUP");
146 
147     /**
148      * Creates a new {@link ChannelOption} with the specified unique {@code name}.
149      */
150     private ChannelOption(int id, String name) {
151         super(id, name);
152     }
153 
154     @Deprecated
155     protected ChannelOption(String name) {
156         this(pool.nextId(), name);
157     }
158 
159     /**
160      * Validate the value which is set for the {@link ChannelOption}. Sub-classes
161      * may override this for special checks.
162      */
163     public void validate(T value) {
164         ObjectUtil.checkNotNull(value, "value");
165     }
166 }