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