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 io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.socket.SocketChannelConfig;
20  
21  import java.nio.ByteBuffer;
22  import java.nio.channels.WritableByteChannel;
23  import java.util.Map;
24  
25  /**
26   * A set of configuration properties of a {@link Channel}.
27   * <p>
28   * Please down-cast to more specific configuration type such as
29   * {@link SocketChannelConfig} or use {@link #setOptions(Map)} to set the
30   * transport-specific properties:
31   * <pre>
32   * {@link Channel} ch = ...;
33   * {@link SocketChannelConfig} cfg = <strong>({@link SocketChannelConfig}) ch.getConfig();</strong>
34   * cfg.setTcpNoDelay(false);
35   * </pre>
36   *
37   * <h3>Option map</h3>
38   *
39   * An option map property is a dynamic write-only property which allows
40   * the configuration of a {@link Channel} without down-casting its associated
41   * {@link ChannelConfig}.  To update an option map, please call {@link #setOptions(Map)}.
42   * <p>
43   * All {@link ChannelConfig} has the following options:
44   *
45   * <table border="1" cellspacing="0" cellpadding="6">
46   * <tr>
47   * <th>Name</th><th>Associated setter method</th>
48   * </tr><tr>
49   * <td>{@link ChannelOption#CONNECT_TIMEOUT_MILLIS}</td><td>{@link #setConnectTimeoutMillis(int)}</td>
50   * </tr><tr>
51   * <td>{@link ChannelOption#WRITE_SPIN_COUNT}</td><td>{@link #setWriteSpinCount(int)}</td>
52   * </tr><tr>
53   * <td>{@link ChannelOption#WRITE_BUFFER_WATER_MARK}</td><td>{@link #setWriteBufferWaterMark(WriteBufferWaterMark)}</td>
54   * </tr><tr>
55   * <td>{@link ChannelOption#ALLOCATOR}</td><td>{@link #setAllocator(ByteBufAllocator)}</td>
56   * </tr><tr>
57   * <td>{@link ChannelOption#AUTO_READ}</td><td>{@link #setAutoRead(boolean)}</td>
58   * </tr>
59   * </table>
60   * <p>
61   * More options are available in the sub-types of {@link ChannelConfig}.  For
62   * example, you can configure the parameters which are specific to a TCP/IP
63   * socket as explained in {@link SocketChannelConfig}.
64   */
65  public interface ChannelConfig {
66  
67      /**
68       * Return all set {@link ChannelOption}'s.
69       */
70      Map<ChannelOption<?>, Object> getOptions();
71  
72      /**
73       * Sets the configuration properties from the specified {@link Map}.
74       */
75      boolean setOptions(Map<ChannelOption<?>, ?> options);
76  
77      /**
78       * Return the value of the given {@link ChannelOption}
79       */
80      <T> T getOption(ChannelOption<T> option);
81  
82      /**
83       * Sets a configuration property with the specified name and value.
84       * To override this method properly, you must call the super class:
85       * <pre>
86       * public boolean setOption(ChannelOption&lt;T&gt; option, T value) {
87       *     if (super.setOption(option, value)) {
88       *         return true;
89       *     }
90       *
91       *     if (option.equals(additionalOption)) {
92       *         ....
93       *         return true;
94       *     }
95       *
96       *     return false;
97       * }
98       * </pre>
99       *
100      * @return {@code true} if and only if the property has been set
101      */
102     <T> boolean setOption(ChannelOption<T> option, T value);
103 
104     /**
105      * Returns the connect timeout of the channel in milliseconds.  If the
106      * {@link Channel} does not support connect operation, this property is not
107      * used at all, and therefore will be ignored.
108      *
109      * @return the connect timeout in milliseconds.  {@code 0} if disabled.
110      */
111     int getConnectTimeoutMillis();
112 
113     /**
114      * Sets the connect timeout of the channel in milliseconds.  If the
115      * {@link Channel} does not support connect operation, this property is not
116      * used at all, and therefore will be ignored.
117      *
118      * @param connectTimeoutMillis the connect timeout in milliseconds.
119      *                             {@code 0} to disable.
120      */
121     ChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
122 
123     /**
124      * @deprecated Use {@link MaxMessagesRecvByteBufAllocator}
125      * <p>
126      * Returns the maximum number of messages to read per read loop.
127      * a {@link ChannelInboundHandler#channelRead(ChannelHandlerContext, Object) channelRead()} event.
128      * If this value is greater than 1, an event loop might attempt to read multiple times to procure multiple messages.
129      */
130     @Deprecated
131     int getMaxMessagesPerRead();
132 
133     /**
134      * @deprecated Use {@link MaxMessagesRecvByteBufAllocator}
135      * <p>
136      * Sets the maximum number of messages to read per read loop.
137      * If this value is greater than 1, an event loop might attempt to read multiple times to procure multiple messages.
138      */
139     @Deprecated
140     ChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead);
141 
142     /**
143      * Returns the maximum loop count for a write operation until
144      * {@link WritableByteChannel#write(ByteBuffer)} returns a non-zero value.
145      * It is similar to what a spin lock is used for in concurrency programming.
146      * It improves memory utilization and write throughput depending on
147      * the platform that JVM runs on.  The default value is {@code 16}.
148      */
149     int getWriteSpinCount();
150 
151     /**
152      * Sets the maximum loop count for a write operation until
153      * {@link WritableByteChannel#write(ByteBuffer)} returns a non-zero value.
154      * It is similar to what a spin lock is used for in concurrency programming.
155      * It improves memory utilization and write throughput depending on
156      * the platform that JVM runs on.  The default value is {@code 16}.
157      *
158      * @throws IllegalArgumentException
159      *         if the specified value is {@code 0} or less than {@code 0}
160      */
161     ChannelConfig setWriteSpinCount(int writeSpinCount);
162 
163     /**
164      * Returns {@link ByteBufAllocator} which is used for the channel
165      * to allocate buffers.
166      */
167     ByteBufAllocator getAllocator();
168 
169     /**
170      * Set the {@link ByteBufAllocator} which is used for the channel
171      * to allocate buffers.
172      */
173     ChannelConfig setAllocator(ByteBufAllocator allocator);
174 
175     /**
176      * Returns {@link RecvByteBufAllocator} which is used for the channel to allocate receive buffers.
177      */
178     <T extends RecvByteBufAllocator> T getRecvByteBufAllocator();
179 
180     /**
181      * Set the {@link RecvByteBufAllocator} which is used for the channel to allocate receive buffers.
182      */
183     ChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator);
184 
185     /**
186      * Returns {@code true} if and only if {@link ChannelHandlerContext#read()} will be invoked automatically so that
187      * a user application doesn't need to call it at all. The default value is {@code true}.
188      */
189     boolean isAutoRead();
190 
191     /**
192      * Sets if {@link ChannelHandlerContext#read()} will be invoked automatically so that a user application doesn't
193      * need to call it at all. The default value is {@code true}.
194      */
195     ChannelConfig setAutoRead(boolean autoRead);
196 
197     /**
198      * Returns {@code true} if and only if the {@link Channel} will be closed automatically and immediately on
199      * write failure. The default is {@code true}.
200      */
201     boolean isAutoClose();
202 
203     /**
204      * Sets whether the {@link Channel} should be closed automatically and immediately on write failure.
205      * The default is {@code true}.
206      */
207     ChannelConfig setAutoClose(boolean autoClose);
208 
209     /**
210      * Returns the high water mark of the write buffer.  If the number of bytes
211      * queued in the write buffer exceeds this value, {@link Channel#isWritable()}
212      * will start to return {@code false}.
213      */
214     int getWriteBufferHighWaterMark();
215 
216     /**
217      * <p>
218      * Sets the high water mark of the write buffer.  If the number of bytes
219      * queued in the write buffer exceeds this value, {@link Channel#isWritable()}
220      * will start to return {@code false}.
221      */
222     ChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);
223 
224     /**
225      * Returns the low water mark of the write buffer.  Once the number of bytes
226      * queued in the write buffer exceeded the
227      * {@linkplain #setWriteBufferHighWaterMark(int) high water mark} and then
228      * dropped down below this value, {@link Channel#isWritable()} will start to return
229      * {@code true} again.
230      */
231     int getWriteBufferLowWaterMark();
232 
233     /**
234      * <p>
235      * Sets the low water mark of the write buffer.  Once the number of bytes
236      * queued in the write buffer exceeded the
237      * {@linkplain #setWriteBufferHighWaterMark(int) high water mark} and then
238      * dropped down below this value, {@link Channel#isWritable()} will start to return
239      * {@code true} again.
240      */
241     ChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark);
242 
243     /**
244      * Returns {@link MessageSizeEstimator} which is used for the channel
245      * to detect the size of a message.
246      */
247     MessageSizeEstimator getMessageSizeEstimator();
248 
249     /**
250      * Set the {@link MessageSizeEstimator} which is used for the channel
251      * to detect the size of a message.
252      */
253     ChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
254 
255     /**
256      * Returns the {@link WriteBufferWaterMark} which is used for setting the high and low
257      * water mark of the write buffer.
258      */
259     WriteBufferWaterMark getWriteBufferWaterMark();
260 
261     /**
262      * Set the {@link WriteBufferWaterMark} which is used for setting the high and low
263      * water mark of the write buffer.
264      */
265     ChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark);
266 }