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  
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 KQueueServerChannelConfig extends KQueueChannelConfig implements ServerSocketChannelConfig {
38      private volatile int backlog = NetUtil.SOMAXCONN;
39      private volatile boolean enableTcpFastOpen;
40  
41      KQueueServerChannelConfig(AbstractKQueueChannel 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) (isTcpFastOpen() ? Integer.valueOf(1) : Integer.valueOf(0));
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 > 0);
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 ((AbstractKQueueChannel) channel).socket.isReuseAddress();
91          } catch (IOException e) {
92              throw new ChannelException(e);
93          }
94      }
95  
96      @Override
97      public KQueueServerChannelConfig setReuseAddress(boolean reuseAddress) {
98          try {
99              ((AbstractKQueueChannel) 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 ((AbstractKQueueChannel) channel).socket.getReceiveBufferSize();
110         } catch (IOException e) {
111             throw new ChannelException(e);
112         }
113     }
114 
115     @Override
116     public KQueueServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
117         try {
118             ((AbstractKQueueChannel) 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 KQueueServerChannelConfig setBacklog(int backlog) {
132         checkPositiveOrZero(backlog, "backlog");
133         this.backlog = backlog;
134         return this;
135     }
136 
137     /**
138      * Returns {@code true} if TCP FastOpen is enabled.
139      *
140      * @see <a href="https://tools.ietf.org/html/rfc7413#appendix-A.2">RFC 7413 Passive Open</a>
141      */
142     public boolean isTcpFastOpen() {
143         return enableTcpFastOpen;
144     }
145 
146     /**
147      * Enables TCP FastOpen 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 enableTcpFastOpen {@code true} if TCP FastOpen should be enabled for incoming connections.
151      *
152      * @see <a href="https://tools.ietf.org/html/rfc7413#appendix-A.2">RFC 7413 Passive Open</a>
153      */
154     public KQueueServerChannelConfig setTcpFastOpen(boolean enableTcpFastOpen) {
155         this.enableTcpFastOpen = enableTcpFastOpen;
156         return this;
157     }
158 
159     @Override
160     public KQueueServerChannelConfig setRcvAllocTransportProvidesGuess(boolean transportProvidesGuess) {
161         super.setRcvAllocTransportProvidesGuess(transportProvidesGuess);
162         return this;
163     }
164 
165     @Override
166     public KQueueServerChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
167         return this;
168     }
169 
170     @Override
171     public KQueueServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
172         super.setConnectTimeoutMillis(connectTimeoutMillis);
173         return this;
174     }
175 
176     @Override
177     @Deprecated
178     public KQueueServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
179         super.setMaxMessagesPerRead(maxMessagesPerRead);
180         return this;
181     }
182 
183     @Override
184     public KQueueServerChannelConfig setWriteSpinCount(int writeSpinCount) {
185         super.setWriteSpinCount(writeSpinCount);
186         return this;
187     }
188 
189     @Override
190     public KQueueServerChannelConfig setAllocator(ByteBufAllocator allocator) {
191         super.setAllocator(allocator);
192         return this;
193     }
194 
195     @Override
196     public KQueueServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
197         super.setRecvByteBufAllocator(allocator);
198         return this;
199     }
200 
201     @Override
202     public KQueueServerChannelConfig setAutoRead(boolean autoRead) {
203         super.setAutoRead(autoRead);
204         return this;
205     }
206 
207     @Override
208     @Deprecated
209     public KQueueServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
210         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
211         return this;
212     }
213 
214     @Override
215     @Deprecated
216     public KQueueServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
217         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
218         return this;
219     }
220 
221     @Override
222     public KQueueServerChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
223         super.setWriteBufferWaterMark(writeBufferWaterMark);
224         return this;
225     }
226 
227     @Override
228     public KQueueServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
229         super.setMessageSizeEstimator(estimator);
230         return this;
231     }
232 }