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.ChannelOption;
20  import io.netty.channel.MessageSizeEstimator;
21  import io.netty.channel.RecvByteBufAllocator;
22  import io.netty.channel.socket.ServerSocketChannelConfig;
23  
24  import java.util.Map;
25  
26  public final class EpollServerSocketChannelConfig extends EpollServerChannelConfig
27          implements ServerSocketChannelConfig {
28  
29      EpollServerSocketChannelConfig(EpollServerSocketChannel channel) {
30          super(channel);
31  
32          // Use SO_REUSEADDR by default as java.nio does the same.
33          //
34          // See https://github.com/netty/netty/issues/2605
35          setReuseAddress(true);
36      }
37  
38      @Override
39      public Map<ChannelOption<?>, Object> getOptions() {
40          return getOptions(super.getOptions(), EpollChannelOption.SO_REUSEPORT);
41      }
42  
43      @SuppressWarnings("unchecked")
44      @Override
45      public <T> T getOption(ChannelOption<T> option) {
46          if (option == EpollChannelOption.SO_REUSEPORT) {
47              return (T) Boolean.valueOf(isReusePort());
48          }
49          return super.getOption(option);
50      }
51  
52      @Override
53      public <T> boolean setOption(ChannelOption<T> option, T value) {
54          validate(option, value);
55  
56          if (option == EpollChannelOption.SO_REUSEPORT) {
57              setReusePort((Boolean) value);
58          } else {
59              return super.setOption(option, value);
60          }
61  
62          return true;
63      }
64  
65      @Override
66      public EpollServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
67          super.setReuseAddress(reuseAddress);
68          return this;
69      }
70  
71      @Override
72      public EpollServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
73          super.setReceiveBufferSize(receiveBufferSize);
74          return this;
75      }
76  
77      @Override
78      public EpollServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
79          return this;
80      }
81  
82      @Override
83      public EpollServerSocketChannelConfig setBacklog(int backlog) {
84          super.setBacklog(backlog);
85          return this;
86      }
87  
88      @Override
89      public EpollServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
90          super.setConnectTimeoutMillis(connectTimeoutMillis);
91          return this;
92      }
93  
94      @Override
95      public EpollServerSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
96          super.setMaxMessagesPerRead(maxMessagesPerRead);
97          return this;
98      }
99  
100     @Override
101     public EpollServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
102         super.setWriteSpinCount(writeSpinCount);
103         return this;
104     }
105 
106     @Override
107     public EpollServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
108         super.setAllocator(allocator);
109         return this;
110     }
111 
112     @Override
113     public EpollServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
114         super.setRecvByteBufAllocator(allocator);
115         return this;
116     }
117 
118     @Override
119     public EpollServerSocketChannelConfig setAutoRead(boolean autoRead) {
120         super.setAutoRead(autoRead);
121         return this;
122     }
123 
124     @Override
125     public EpollServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
126         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
127         return this;
128     }
129 
130     @Override
131     public EpollServerSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
132         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
133         return this;
134     }
135 
136     @Override
137     public EpollServerSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
138         super.setMessageSizeEstimator(estimator);
139         return this;
140     }
141 
142     /**
143      * Returns {@code true} if the SO_REUSEPORT option is set.
144      */
145     public boolean isReusePort() {
146         return Native.isReusePort(channel.fd().intValue()) == 1;
147     }
148 
149     /**
150      * Set the SO_REUSEPORT option on the underlying Channel. This will allow to bind multiple
151      * {@link EpollSocketChannel}s to the same port and so accept connections with multiple threads.
152      *
153      * Be aware this method needs be called before {@link EpollSocketChannel#bind(java.net.SocketAddress)} to have
154      * any affect.
155      */
156     public EpollServerSocketChannelConfig setReusePort(boolean reusePort) {
157         Native.setReusePort(channel.fd().intValue(), reusePort ? 1 : 0);
158         return this;
159     }
160 }