View Javadoc
1   /*
2    * Copyright 2015 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  import io.netty.util.NetUtil;
26  
27  import java.io.IOException;
28  import java.util.Map;
29  
30  import static io.netty.channel.ChannelOption.SO_BACKLOG;
31  import static io.netty.channel.ChannelOption.SO_RCVBUF;
32  import static io.netty.channel.ChannelOption.SO_REUSEADDR;
33  
34  public class EpollServerChannelConfig extends EpollChannelConfig implements ServerSocketChannelConfig {
35      protected final AbstractEpollChannel channel;
36      private volatile int backlog = NetUtil.SOMAXCONN;
37      private volatile int pendingFastOpenRequestsThreshold;
38  
39      EpollServerChannelConfig(AbstractEpollChannel channel) {
40          super(channel);
41          this.channel = channel;
42      }
43  
44      @Override
45      public Map<ChannelOption<?>, Object> getOptions() {
46          return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG, EpollChannelOption.TCP_FASTOPEN);
47      }
48  
49      @SuppressWarnings("unchecked")
50      @Override
51      public <T> T getOption(ChannelOption<T> option) {
52          if (option == SO_RCVBUF) {
53              return (T) Integer.valueOf(getReceiveBufferSize());
54          }
55          if (option == SO_REUSEADDR) {
56              return (T) Boolean.valueOf(isReuseAddress());
57          }
58          if (option == SO_BACKLOG) {
59              return (T) Integer.valueOf(getBacklog());
60          }
61          if (option == EpollChannelOption.TCP_FASTOPEN) {
62              return (T) Integer.valueOf(getTcpFastopen());
63          }
64          return super.getOption(option);
65      }
66  
67      @Override
68      public <T> boolean setOption(ChannelOption<T> option, T value) {
69          validate(option, value);
70  
71          if (option == SO_RCVBUF) {
72              setReceiveBufferSize((Integer) value);
73          } else if (option == SO_REUSEADDR) {
74              setReuseAddress((Boolean) value);
75          } else if (option == SO_BACKLOG) {
76              setBacklog((Integer) value);
77          } else if (option == EpollChannelOption.TCP_FASTOPEN) {
78              setTcpFastopen((Integer) value);
79          } else {
80              return super.setOption(option, value);
81          }
82  
83          return true;
84      }
85  
86      public boolean isReuseAddress() {
87          try {
88              return channel.socket.isReuseAddress();
89          } catch (IOException e) {
90              throw new ChannelException(e);
91          }
92      }
93  
94      public EpollServerChannelConfig setReuseAddress(boolean reuseAddress) {
95          try {
96              channel.socket.setReuseAddress(reuseAddress);
97              return this;
98          } catch (IOException e) {
99              throw new ChannelException(e);
100         }
101     }
102 
103     public int getReceiveBufferSize() {
104         try {
105             return channel.socket.getReceiveBufferSize();
106         } catch (IOException e) {
107             throw new ChannelException(e);
108         }
109     }
110 
111     public EpollServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
112         try {
113             channel.socket.setReceiveBufferSize(receiveBufferSize);
114             return this;
115         } catch (IOException e) {
116             throw new ChannelException(e);
117         }
118     }
119 
120     public int getBacklog() {
121         return backlog;
122     }
123 
124     public EpollServerChannelConfig setBacklog(int backlog) {
125         if (backlog < 0) {
126             throw new IllegalArgumentException("backlog: " + backlog);
127         }
128         this.backlog = backlog;
129         return this;
130     }
131 
132     /**
133      * Returns threshold value of number of pending for fast open connect.
134      *
135      * @see <a href="https://tools.ietf.org/html/rfc7413#appendix-A.2">RFC 7413 Passive Open</a>
136      */
137     public int getTcpFastopen() {
138         return pendingFastOpenRequestsThreshold;
139     }
140 
141     /**
142      * Enables tcpFastOpen on the server channel. If the underlying os doesnt support TCP_FASTOPEN setting this has no
143      * effect. This has to be set before doing listen on the socket otherwise this takes no effect.
144      *
145      * @param pendingFastOpenRequestsThreshold number of requests to be pending for fastopen at a given point in time
146      * for security. @see <a href="https://tools.ietf.org/html/rfc7413#appendix-A.2">RFC 7413 Passive Open</a>
147      *
148      * @see <a href="https://tools.ietf.org/html/rfc7413">RFC 7413 TCP FastOpen</a>
149      */
150     public EpollServerChannelConfig setTcpFastopen(int pendingFastOpenRequestsThreshold) {
151         if (this.pendingFastOpenRequestsThreshold < 0) {
152             throw new IllegalArgumentException("pendingFastOpenRequestsThreshold: " + pendingFastOpenRequestsThreshold);
153         }
154         this.pendingFastOpenRequestsThreshold = pendingFastOpenRequestsThreshold;
155         return this;
156     }
157 
158     @Override
159     public EpollServerChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
160         return this;
161     }
162 
163     @Override
164     public EpollServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
165         super.setConnectTimeoutMillis(connectTimeoutMillis);
166         return this;
167     }
168 
169     @Override
170     @Deprecated
171     public EpollServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
172         super.setMaxMessagesPerRead(maxMessagesPerRead);
173         return this;
174     }
175 
176     @Override
177     public EpollServerChannelConfig setWriteSpinCount(int writeSpinCount) {
178         super.setWriteSpinCount(writeSpinCount);
179         return this;
180     }
181 
182     @Override
183     public EpollServerChannelConfig setAllocator(ByteBufAllocator allocator) {
184         super.setAllocator(allocator);
185         return this;
186     }
187 
188     @Override
189     public EpollServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
190         super.setRecvByteBufAllocator(allocator);
191         return this;
192     }
193 
194     @Override
195     public EpollServerChannelConfig setAutoRead(boolean autoRead) {
196         super.setAutoRead(autoRead);
197         return this;
198     }
199 
200     @Override
201     @Deprecated
202     public EpollServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
203         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
204         return this;
205     }
206 
207     @Override
208     @Deprecated
209     public EpollServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
210         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
211         return this;
212     }
213 
214     @Override
215     public EpollServerChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
216         super.setWriteBufferWaterMark(writeBufferWaterMark);
217         return this;
218     }
219 
220     @Override
221     public EpollServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
222         super.setMessageSizeEstimator(estimator);
223         return this;
224     }
225 
226     @Override
227     public EpollServerChannelConfig setEpollMode(EpollMode mode) {
228         super.setEpollMode(mode);
229         return this;
230     }
231 }