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.WriteBufferWaterMark;
24  
25  import java.io.IOException;
26  import java.util.Map;
27  
28  import static io.netty.channel.kqueue.KQueueChannelOption.SO_ACCEPTFILTER;
29  import static io.netty.channel.unix.UnixChannelOption.SO_REUSEPORT;
30  
31  public class KQueueServerSocketChannelConfig extends KQueueServerChannelConfig {
32      KQueueServerSocketChannelConfig(KQueueServerSocketChannel channel) {
33          super(channel);
34  
35          // Use SO_REUSEADDR by default as java.nio does the same.
36          //
37          // See https://github.com/netty/netty/issues/2605
38          setReuseAddress(true);
39      }
40  
41      @Override
42      public Map<ChannelOption<?>, Object> getOptions() {
43          return getOptions(super.getOptions(), SO_REUSEPORT, SO_ACCEPTFILTER);
44      }
45  
46      @SuppressWarnings("unchecked")
47      @Override
48      public <T> T getOption(ChannelOption<T> option) {
49          if (option == SO_REUSEPORT) {
50              return (T) Boolean.valueOf(isReusePort());
51          }
52          if (option == SO_ACCEPTFILTER) {
53              return (T) getAcceptFilter();
54          }
55          return super.getOption(option);
56      }
57  
58      @Override
59      public <T> boolean setOption(ChannelOption<T> option, T value) {
60          validate(option, value);
61  
62          if (option == SO_REUSEPORT) {
63              setReusePort((Boolean) value);
64          } else if (option == SO_ACCEPTFILTER) {
65              setAcceptFilter((AcceptFilter) value);
66          } else {
67              return super.setOption(option, value);
68          }
69  
70          return true;
71      }
72  
73      public KQueueServerSocketChannelConfig setReusePort(boolean reusePort) {
74          try {
75              ((KQueueServerSocketChannel) channel).socket.setReusePort(reusePort);
76              return this;
77          } catch (IOException e) {
78              throw new ChannelException(e);
79          }
80      }
81  
82      public boolean isReusePort() {
83          try {
84              return ((KQueueServerSocketChannel) channel).socket.isReusePort();
85          } catch (IOException e) {
86              throw new ChannelException(e);
87          }
88      }
89  
90      public KQueueServerSocketChannelConfig setAcceptFilter(AcceptFilter acceptFilter) {
91          try {
92              ((KQueueServerSocketChannel) channel).socket.setAcceptFilter(acceptFilter);
93              return this;
94          } catch (IOException e) {
95              throw new ChannelException(e);
96          }
97      }
98  
99      public AcceptFilter getAcceptFilter() {
100         try {
101             return ((KQueueServerSocketChannel) channel).socket.getAcceptFilter();
102         } catch (IOException e) {
103             throw new ChannelException(e);
104         }
105     }
106 
107     @Override
108     public KQueueServerSocketChannelConfig setRcvAllocTransportProvidesGuess(boolean transportProvidesGuess) {
109         super.setRcvAllocTransportProvidesGuess(transportProvidesGuess);
110         return this;
111     }
112 
113     @Override
114     public KQueueServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
115         super.setReuseAddress(reuseAddress);
116         return this;
117     }
118 
119     @Override
120     public KQueueServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
121         super.setReceiveBufferSize(receiveBufferSize);
122         return this;
123     }
124 
125     @Override
126     public KQueueServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
127         return this;
128     }
129 
130     @Override
131     public KQueueServerSocketChannelConfig setBacklog(int backlog) {
132         super.setBacklog(backlog);
133         return this;
134     }
135 
136     @Override
137     public KQueueServerSocketChannelConfig setTcpFastOpen(boolean enableTcpFastOpen) {
138         super.setTcpFastOpen(enableTcpFastOpen);
139         return this;
140     }
141 
142     @Override
143     public KQueueServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
144         super.setConnectTimeoutMillis(connectTimeoutMillis);
145         return this;
146     }
147 
148     @Override
149     @Deprecated
150     public KQueueServerSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
151         super.setMaxMessagesPerRead(maxMessagesPerRead);
152         return this;
153     }
154 
155     @Override
156     public KQueueServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
157         super.setWriteSpinCount(writeSpinCount);
158         return this;
159     }
160 
161     @Override
162     public KQueueServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
163         super.setAllocator(allocator);
164         return this;
165     }
166 
167     @Override
168     public KQueueServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
169         super.setRecvByteBufAllocator(allocator);
170         return this;
171     }
172 
173     @Override
174     public KQueueServerSocketChannelConfig setAutoRead(boolean autoRead) {
175         super.setAutoRead(autoRead);
176         return this;
177     }
178 
179     @Override
180     @Deprecated
181     public KQueueServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
182         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
183         return this;
184     }
185 
186     @Override
187     @Deprecated
188     public KQueueServerSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
189         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
190         return this;
191     }
192 
193     @Override
194     public KQueueServerSocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
195         super.setWriteBufferWaterMark(writeBufferWaterMark);
196         return this;
197     }
198 
199     @Override
200     public KQueueServerSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
201         super.setMessageSizeEstimator(estimator);
202         return this;
203     }
204 }