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