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