View Javadoc
1   /*
2    * Copyright 2014 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.epoll;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelException;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.MessageSizeEstimator;
22  import io.netty.channel.RecvByteBufAllocator;
23  import io.netty.channel.WriteBufferWaterMark;
24  import io.netty.channel.socket.ServerSocketChannelConfig;
25  
26  import java.io.IOException;
27  import java.net.InetAddress;
28  import java.util.Map;
29  
30  public final class EpollServerSocketChannelConfig extends EpollServerChannelConfig
31          implements ServerSocketChannelConfig {
32  
33      EpollServerSocketChannelConfig(EpollServerSocketChannel channel) {
34          super(channel);
35  
36          // Use SO_REUSEADDR by default as java.nio does the same.
37          //
38          // See https://github.com/netty/netty/issues/2605
39          setReuseAddress(true);
40      }
41  
42      @Override
43      public Map<ChannelOption<?>, Object> getOptions() {
44          return getOptions(super.getOptions(), EpollChannelOption.SO_REUSEPORT, EpollChannelOption.IP_FREEBIND,
45              EpollChannelOption.IP_TRANSPARENT, EpollChannelOption.TCP_DEFER_ACCEPT);
46      }
47  
48      @SuppressWarnings("unchecked")
49      @Override
50      public <T> T getOption(ChannelOption<T> option) {
51          if (option == EpollChannelOption.SO_REUSEPORT) {
52              return (T) Boolean.valueOf(isReusePort());
53          }
54          if (option == EpollChannelOption.IP_FREEBIND) {
55              return (T) Boolean.valueOf(isFreeBind());
56          }
57          if (option == EpollChannelOption.IP_TRANSPARENT) {
58              return (T) Boolean.valueOf(isIpTransparent());
59          }
60          if (option == EpollChannelOption.TCP_DEFER_ACCEPT) {
61              return (T) Integer.valueOf(getTcpDeferAccept());
62          }
63          return super.getOption(option);
64      }
65  
66      @Override
67      public <T> boolean setOption(ChannelOption<T> option, T value) {
68          validate(option, value);
69  
70          if (option == EpollChannelOption.SO_REUSEPORT) {
71              setReusePort((Boolean) value);
72          } else if (option == EpollChannelOption.IP_FREEBIND) {
73              setFreeBind((Boolean) value);
74          } else if (option == EpollChannelOption.IP_TRANSPARENT) {
75              setIpTransparent((Boolean) value);
76          } else if (option == EpollChannelOption.TCP_MD5SIG) {
77              @SuppressWarnings("unchecked")
78              final Map<InetAddress, byte[]> m = (Map<InetAddress, byte[]>) value;
79              setTcpMd5Sig(m);
80          } else if (option == EpollChannelOption.TCP_DEFER_ACCEPT) {
81              setTcpDeferAccept((Integer) value);
82          } else {
83              return super.setOption(option, value);
84          }
85  
86          return true;
87      }
88  
89      @Override
90      public EpollServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
91          super.setReuseAddress(reuseAddress);
92          return this;
93      }
94  
95      @Override
96      public EpollServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
97          super.setReceiveBufferSize(receiveBufferSize);
98          return this;
99      }
100 
101     @Override
102     public EpollServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
103         return this;
104     }
105 
106     @Override
107     public EpollServerSocketChannelConfig setBacklog(int backlog) {
108         super.setBacklog(backlog);
109         return this;
110     }
111 
112     @Override
113     public EpollServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
114         super.setConnectTimeoutMillis(connectTimeoutMillis);
115         return this;
116     }
117 
118     @Override
119     @Deprecated
120     public EpollServerSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
121         super.setMaxMessagesPerRead(maxMessagesPerRead);
122         return this;
123     }
124 
125     @Override
126     public EpollServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
127         super.setWriteSpinCount(writeSpinCount);
128         return this;
129     }
130 
131     @Override
132     public EpollServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
133         super.setAllocator(allocator);
134         return this;
135     }
136 
137     @Override
138     public EpollServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
139         super.setRecvByteBufAllocator(allocator);
140         return this;
141     }
142 
143     @Override
144     public EpollServerSocketChannelConfig setAutoRead(boolean autoRead) {
145         super.setAutoRead(autoRead);
146         return this;
147     }
148 
149     @Override
150     @Deprecated
151     public EpollServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
152         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
153         return this;
154     }
155 
156     @Override
157     @Deprecated
158     public EpollServerSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
159         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
160         return this;
161     }
162 
163     @Override
164     public EpollServerSocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
165         super.setWriteBufferWaterMark(writeBufferWaterMark);
166         return this;
167     }
168 
169     @Override
170     public EpollServerSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
171         super.setMessageSizeEstimator(estimator);
172         return this;
173     }
174 
175     /**
176      * Set the {@code TCP_MD5SIG} option on the socket. See {@code linux/tcp.h} for more details.
177      * Keys can only be set on, not read to prevent a potential leak, as they are confidential.
178      * Allowing them being read would mean anyone with access to the channel could get them.
179      */
180     public EpollServerSocketChannelConfig setTcpMd5Sig(Map<InetAddress, byte[]> keys) {
181         try {
182             ((EpollServerSocketChannel) channel).setTcpMd5Sig(keys);
183             return this;
184         } catch (IOException e) {
185             throw new ChannelException(e);
186         }
187     }
188 
189     /**
190      * Returns {@code true} if the SO_REUSEPORT option is set.
191      */
192     public boolean isReusePort() {
193         try {
194             return channel.socket.isReusePort();
195         } catch (IOException e) {
196             throw new ChannelException(e);
197         }
198     }
199 
200     /**
201      * Set the SO_REUSEPORT option on the underlying Channel. This will allow to bind multiple
202      * {@link EpollSocketChannel}s to the same port and so accept connections with multiple threads.
203      *
204      * Be aware this method needs be called before {@link EpollSocketChannel#bind(java.net.SocketAddress)} to have
205      * any affect.
206      */
207     public EpollServerSocketChannelConfig setReusePort(boolean reusePort) {
208         try {
209             channel.socket.setReusePort(reusePort);
210             return this;
211         } catch (IOException e) {
212             throw new ChannelException(e);
213         }
214     }
215 
216     /**
217      * Returns {@code true} if <a href="http://man7.org/linux/man-pages/man7/ip.7.html">IP_FREEBIND</a> is enabled,
218      * {@code false} otherwise.
219      */
220     public boolean isFreeBind() {
221         try {
222             return channel.socket.isIpFreeBind();
223         } catch (IOException e) {
224             throw new ChannelException(e);
225         }
226     }
227 
228     /**
229      * If {@code true} is used <a href="http://man7.org/linux/man-pages/man7/ip.7.html">IP_FREEBIND</a> is enabled,
230      * {@code false} for disable it. Default is disabled.
231      */
232     public EpollServerSocketChannelConfig setFreeBind(boolean freeBind) {
233         try {
234             channel.socket.setIpFreeBind(freeBind);
235             return this;
236         } catch (IOException e) {
237             throw new ChannelException(e);
238         }
239     }
240 
241     /**
242      * Returns {@code true} if <a href="http://man7.org/linux/man-pages/man7/ip.7.html">IP_TRANSPARENT</a> is enabled,
243      * {@code false} otherwise.
244      */
245     public boolean isIpTransparent() {
246         try {
247             return channel.socket.isIpTransparent();
248         } catch (IOException e) {
249             throw new ChannelException(e);
250         }
251     }
252 
253     /**
254      * If {@code true} is used <a href="http://man7.org/linux/man-pages/man7/ip.7.html">IP_TRANSPARENT</a> is enabled,
255      * {@code false} for disable it. Default is disabled.
256      */
257     public EpollServerSocketChannelConfig setIpTransparent(boolean transparent) {
258         try {
259             channel.socket.setIpTransparent(transparent);
260             return this;
261         } catch (IOException e) {
262             throw new ChannelException(e);
263         }
264     }
265 
266     /**
267      * Set the {@code TCP_DEFER_ACCEPT} option on the socket. See {@code man 7 tcp} for more details.
268      */
269     public EpollServerSocketChannelConfig setTcpDeferAccept(int deferAccept) {
270         try {
271             channel.socket.setTcpDeferAccept(deferAccept);
272             return this;
273         } catch (IOException e) {
274             throw new ChannelException(e);
275         }
276     }
277 
278     /**
279      * Returns a positive value if <a href="http://linux.die.net/man/7/tcp">TCP_DEFER_ACCEPT</a> is enabled.
280      */
281     public int getTcpDeferAccept() {
282         try {
283             return channel.socket.getTcpDeferAccept();
284         } catch (IOException e) {
285             throw new ChannelException(e);
286         }
287     }
288 }