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