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    *   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.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.NetUtil;
26  import io.netty.util.internal.UnstableApi;
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  
35  @UnstableApi
36  public class KQueueServerChannelConfig extends KQueueChannelConfig implements ServerSocketChannelConfig {
37      protected final AbstractKQueueChannel channel;
38      private volatile int backlog = NetUtil.SOMAXCONN;
39  
40      KQueueServerChannelConfig(AbstractKQueueChannel channel) {
41          super(channel);
42          this.channel = channel;
43      }
44  
45      @Override
46      public Map<ChannelOption<?>, Object> getOptions() {
47          return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG);
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          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 {
76              return super.setOption(option, value);
77          }
78  
79          return true;
80      }
81  
82      public boolean isReuseAddress() {
83          try {
84              return channel.socket.isReuseAddress();
85          } catch (IOException e) {
86              throw new ChannelException(e);
87          }
88      }
89  
90      public KQueueServerChannelConfig setReuseAddress(boolean reuseAddress) {
91          try {
92              channel.socket.setReuseAddress(reuseAddress);
93              return this;
94          } catch (IOException e) {
95              throw new ChannelException(e);
96          }
97      }
98  
99      public int getReceiveBufferSize() {
100         try {
101             return channel.socket.getReceiveBufferSize();
102         } catch (IOException e) {
103             throw new ChannelException(e);
104         }
105     }
106 
107     public KQueueServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
108         try {
109             channel.socket.setReceiveBufferSize(receiveBufferSize);
110             return this;
111         } catch (IOException e) {
112             throw new ChannelException(e);
113         }
114     }
115 
116     public int getBacklog() {
117         return backlog;
118     }
119 
120     public KQueueServerChannelConfig setBacklog(int backlog) {
121         if (backlog < 0) {
122             throw new IllegalArgumentException("backlog: " + backlog);
123         }
124         this.backlog = backlog;
125         return this;
126     }
127 
128     @Override
129     public KQueueServerChannelConfig setRcvAllocTransportProvidesGuess(boolean transportProvidesGuess) {
130         super.setRcvAllocTransportProvidesGuess(transportProvidesGuess);
131         return this;
132     }
133 
134     @Override
135     public KQueueServerChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
136         return this;
137     }
138 
139     @Override
140     public KQueueServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
141         super.setConnectTimeoutMillis(connectTimeoutMillis);
142         return this;
143     }
144 
145     @Override
146     @Deprecated
147     public KQueueServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
148         super.setMaxMessagesPerRead(maxMessagesPerRead);
149         return this;
150     }
151 
152     @Override
153     public KQueueServerChannelConfig setWriteSpinCount(int writeSpinCount) {
154         super.setWriteSpinCount(writeSpinCount);
155         return this;
156     }
157 
158     @Override
159     public KQueueServerChannelConfig setAllocator(ByteBufAllocator allocator) {
160         super.setAllocator(allocator);
161         return this;
162     }
163 
164     @Override
165     public KQueueServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
166         super.setRecvByteBufAllocator(allocator);
167         return this;
168     }
169 
170     @Override
171     public KQueueServerChannelConfig setAutoRead(boolean autoRead) {
172         super.setAutoRead(autoRead);
173         return this;
174     }
175 
176     @Override
177     @Deprecated
178     public KQueueServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
179         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
180         return this;
181     }
182 
183     @Override
184     @Deprecated
185     public KQueueServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
186         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
187         return this;
188     }
189 
190     @Override
191     public KQueueServerChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
192         super.setWriteBufferWaterMark(writeBufferWaterMark);
193         return this;
194     }
195 
196     @Override
197     public KQueueServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
198         super.setMessageSizeEstimator(estimator);
199         return this;
200     }
201 }