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    *   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.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.WriteBufferWaterMark;
25  import io.netty.util.NetUtil;
26  
27  import java.net.ServerSocket;
28  import java.net.SocketException;
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  /**
36   * The default {@link ServerSocketChannelConfig} implementation.
37   */
38  public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
39                                                implements ServerSocketChannelConfig {
40  
41      protected final ServerSocket javaSocket;
42      private volatile int backlog = NetUtil.SOMAXCONN;
43  
44      /**
45       * Creates a new instance.
46       */
47      public DefaultServerSocketChannelConfig(ServerSocketChannel channel, ServerSocket javaSocket) {
48          super(channel);
49          if (javaSocket == null) {
50              throw new NullPointerException("javaSocket");
51          }
52          this.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         if (backlog < 0) {
145             throw new IllegalArgumentException("backlog: " + backlog);
146         }
147         this.backlog = backlog;
148         return this;
149     }
150 
151     @Override
152     public ServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
153         super.setConnectTimeoutMillis(connectTimeoutMillis);
154         return this;
155     }
156 
157     @Override
158     @Deprecated
159     public ServerSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
160         super.setMaxMessagesPerRead(maxMessagesPerRead);
161         return this;
162     }
163 
164     @Override
165     public ServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
166         super.setWriteSpinCount(writeSpinCount);
167         return this;
168     }
169 
170     @Override
171     public ServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
172         super.setAllocator(allocator);
173         return this;
174     }
175 
176     @Override
177     public ServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
178         super.setRecvByteBufAllocator(allocator);
179         return this;
180     }
181 
182     @Override
183     public ServerSocketChannelConfig setAutoRead(boolean autoRead) {
184         super.setAutoRead(autoRead);
185         return this;
186     }
187 
188     @Override
189     public ServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
190         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
191         return this;
192     }
193 
194     @Override
195     public ServerSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
196         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
197         return this;
198     }
199 
200     @Override
201     public ServerSocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
202         super.setWriteBufferWaterMark(writeBufferWaterMark);
203         return this;
204     }
205 
206     @Override
207     public ServerSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
208         super.setMessageSizeEstimator(estimator);
209         return this;
210     }
211 }