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