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