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