View Javadoc
1   /*
2    * Copyright 2012 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.socket;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelException;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.DefaultChannelConfig;
22  import io.netty.channel.MessageSizeEstimator;
23  import io.netty.channel.RecvByteBufAllocator;
24  import io.netty.channel.ServerChannelRecvByteBufAllocator;
25  import io.netty.channel.WriteBufferWaterMark;
26  import io.netty.util.NetUtil;
27  import io.netty.util.internal.ObjectUtil;
28  
29  import java.net.ServerSocket;
30  import java.net.SocketException;
31  import java.util.Map;
32  
33  import static io.netty.channel.ChannelOption.SO_BACKLOG;
34  import static io.netty.channel.ChannelOption.SO_RCVBUF;
35  import static io.netty.channel.ChannelOption.SO_REUSEADDR;
36  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
37  
38  /**
39   * The default {@link ServerSocketChannelConfig} implementation.
40   */
41  public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
42                                                implements ServerSocketChannelConfig {
43  
44      protected final ServerSocket javaSocket;
45      private volatile int backlog = NetUtil.SOMAXCONN;
46  
47      /**
48       * Creates a new instance.
49       */
50      public DefaultServerSocketChannelConfig(ServerSocketChannel channel, ServerSocket javaSocket) {
51          super(channel, new ServerChannelRecvByteBufAllocator());
52          this.javaSocket = ObjectUtil.checkNotNull(javaSocket, "javaSocket");
53      }
54  
55      @Override
56      public Map<ChannelOption<?>, Object> getOptions() {
57          return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG);
58      }
59  
60      @SuppressWarnings("unchecked")
61      @Override
62      public <T> T getOption(ChannelOption<T> option) {
63          if (option == SO_RCVBUF) {
64              return (T) Integer.valueOf(getReceiveBufferSize());
65          }
66          if (option == SO_REUSEADDR) {
67              return (T) Boolean.valueOf(isReuseAddress());
68          }
69          if (option == SO_BACKLOG) {
70              return (T) Integer.valueOf(getBacklog());
71          }
72  
73          return super.getOption(option);
74      }
75  
76      @Override
77      public <T> boolean setOption(ChannelOption<T> option, T value) {
78          validate(option, value);
79  
80          if (option == SO_RCVBUF) {
81              setReceiveBufferSize((Integer) value);
82          } else if (option == SO_REUSEADDR) {
83              setReuseAddress((Boolean) value);
84          } else if (option == SO_BACKLOG) {
85              setBacklog((Integer) value);
86          } else {
87              return super.setOption(option, value);
88          }
89  
90          return true;
91      }
92  
93      @Override
94      public boolean isReuseAddress() {
95          try {
96              return javaSocket.getReuseAddress();
97          } catch (SocketException e) {
98              throw new ChannelException(e);
99          }
100     }
101 
102     @Override
103     public ServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
104         try {
105             javaSocket.setReuseAddress(reuseAddress);
106         } catch (SocketException e) {
107             throw new ChannelException(e);
108         }
109         return this;
110     }
111 
112     @Override
113     public int getReceiveBufferSize() {
114         try {
115             return javaSocket.getReceiveBufferSize();
116         } catch (SocketException e) {
117             throw new ChannelException(e);
118         }
119     }
120 
121     @Override
122     public ServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
123         try {
124             javaSocket.setReceiveBufferSize(receiveBufferSize);
125         } catch (SocketException e) {
126             throw new ChannelException(e);
127         }
128         return this;
129     }
130 
131     @Override
132     public ServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
133         javaSocket.setPerformancePreferences(connectionTime, latency, bandwidth);
134         return this;
135     }
136 
137     @Override
138     public int getBacklog() {
139         return backlog;
140     }
141 
142     @Override
143     public ServerSocketChannelConfig setBacklog(int backlog) {
144         checkPositiveOrZero(backlog, "backlog");
145         this.backlog = backlog;
146         return this;
147     }
148 
149     @Override
150     public ServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
151         super.setConnectTimeoutMillis(connectTimeoutMillis);
152         return this;
153     }
154 
155     @Override
156     @Deprecated
157     public ServerSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
158         super.setMaxMessagesPerRead(maxMessagesPerRead);
159         return this;
160     }
161 
162     @Override
163     public ServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
164         super.setWriteSpinCount(writeSpinCount);
165         return this;
166     }
167 
168     @Override
169     public ServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
170         super.setAllocator(allocator);
171         return this;
172     }
173 
174     @Override
175     public ServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
176         super.setRecvByteBufAllocator(allocator);
177         return this;
178     }
179 
180     @Override
181     public ServerSocketChannelConfig setAutoRead(boolean autoRead) {
182         super.setAutoRead(autoRead);
183         return this;
184     }
185 
186     @Override
187     public ServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
188         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
189         return this;
190     }
191 
192     @Override
193     public ServerSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
194         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
195         return this;
196     }
197 
198     @Override
199     public ServerSocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
200         super.setWriteBufferWaterMark(writeBufferWaterMark);
201         return this;
202     }
203 
204     @Override
205     public ServerSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
206         super.setMessageSizeEstimator(estimator);
207         return this;
208     }
209 }