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.internal.PlatformDependent;
26  
27  import java.net.Socket;
28  import java.net.SocketException;
29  import java.util.Map;
30  
31  import static io.netty.channel.ChannelOption.*;
32  
33  /**
34   * The default {@link SocketChannelConfig} implementation.
35   */
36  public class DefaultSocketChannelConfig extends DefaultChannelConfig
37                                          implements SocketChannelConfig {
38  
39      protected final Socket javaSocket;
40      private volatile boolean allowHalfClosure;
41  
42      /**
43       * Creates a new instance.
44       */
45      public DefaultSocketChannelConfig(SocketChannel channel, Socket javaSocket) {
46          super(channel);
47          if (javaSocket == null) {
48              throw new NullPointerException("javaSocket");
49          }
50          this.javaSocket = javaSocket;
51  
52          // Enable TCP_NODELAY by default if possible.
53          if (PlatformDependent.canEnableTcpNoDelayByDefault()) {
54              try {
55                  setTcpNoDelay(true);
56              } catch (Exception e) {
57                  // Ignore.
58              }
59          }
60      }
61  
62      @Override
63      public Map<ChannelOption<?>, Object> getOptions() {
64          return getOptions(
65                  super.getOptions(),
66                  SO_RCVBUF, SO_SNDBUF, TCP_NODELAY, SO_KEEPALIVE, SO_REUSEADDR, SO_LINGER, IP_TOS,
67                  ALLOW_HALF_CLOSURE);
68      }
69  
70      @SuppressWarnings("unchecked")
71      @Override
72      public <T> T getOption(ChannelOption<T> option) {
73          if (option == SO_RCVBUF) {
74              return (T) Integer.valueOf(getReceiveBufferSize());
75          }
76          if (option == SO_SNDBUF) {
77              return (T) Integer.valueOf(getSendBufferSize());
78          }
79          if (option == TCP_NODELAY) {
80              return (T) Boolean.valueOf(isTcpNoDelay());
81          }
82          if (option == SO_KEEPALIVE) {
83              return (T) Boolean.valueOf(isKeepAlive());
84          }
85          if (option == SO_REUSEADDR) {
86              return (T) Boolean.valueOf(isReuseAddress());
87          }
88          if (option == SO_LINGER) {
89              return (T) Integer.valueOf(getSoLinger());
90          }
91          if (option == IP_TOS) {
92              return (T) Integer.valueOf(getTrafficClass());
93          }
94          if (option == ALLOW_HALF_CLOSURE) {
95              return (T) Boolean.valueOf(isAllowHalfClosure());
96          }
97  
98          return super.getOption(option);
99      }
100 
101     @Override
102     public <T> boolean setOption(ChannelOption<T> option, T value) {
103         validate(option, value);
104 
105         if (option == SO_RCVBUF) {
106             setReceiveBufferSize((Integer) value);
107         } else if (option == SO_SNDBUF) {
108             setSendBufferSize((Integer) value);
109         } else if (option == TCP_NODELAY) {
110             setTcpNoDelay((Boolean) value);
111         } else if (option == SO_KEEPALIVE) {
112             setKeepAlive((Boolean) value);
113         } else if (option == SO_REUSEADDR) {
114             setReuseAddress((Boolean) value);
115         } else if (option == SO_LINGER) {
116             setSoLinger((Integer) value);
117         } else if (option == IP_TOS) {
118             setTrafficClass((Integer) value);
119         } else if (option == ALLOW_HALF_CLOSURE) {
120             setAllowHalfClosure((Boolean) value);
121         } else {
122             return super.setOption(option, value);
123         }
124 
125         return true;
126     }
127 
128     @Override
129     public int getReceiveBufferSize() {
130         try {
131             return javaSocket.getReceiveBufferSize();
132         } catch (SocketException e) {
133             throw new ChannelException(e);
134         }
135     }
136 
137     @Override
138     public int getSendBufferSize() {
139         try {
140             return javaSocket.getSendBufferSize();
141         } catch (SocketException e) {
142             throw new ChannelException(e);
143         }
144     }
145 
146     @Override
147     public int getSoLinger() {
148         try {
149             return javaSocket.getSoLinger();
150         } catch (SocketException e) {
151             throw new ChannelException(e);
152         }
153     }
154 
155     @Override
156     public int getTrafficClass() {
157         try {
158             return javaSocket.getTrafficClass();
159         } catch (SocketException e) {
160             throw new ChannelException(e);
161         }
162     }
163 
164     @Override
165     public boolean isKeepAlive() {
166         try {
167             return javaSocket.getKeepAlive();
168         } catch (SocketException e) {
169             throw new ChannelException(e);
170         }
171     }
172 
173     @Override
174     public boolean isReuseAddress() {
175         try {
176             return javaSocket.getReuseAddress();
177         } catch (SocketException e) {
178             throw new ChannelException(e);
179         }
180     }
181 
182     @Override
183     public boolean isTcpNoDelay() {
184         try {
185             return javaSocket.getTcpNoDelay();
186         } catch (SocketException e) {
187             throw new ChannelException(e);
188         }
189     }
190 
191     @Override
192     public SocketChannelConfig setKeepAlive(boolean keepAlive) {
193         try {
194             javaSocket.setKeepAlive(keepAlive);
195         } catch (SocketException e) {
196             throw new ChannelException(e);
197         }
198         return this;
199     }
200 
201     @Override
202     public SocketChannelConfig setPerformancePreferences(
203             int connectionTime, int latency, int bandwidth) {
204         javaSocket.setPerformancePreferences(connectionTime, latency, bandwidth);
205         return this;
206     }
207 
208     @Override
209     public SocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
210         try {
211             javaSocket.setReceiveBufferSize(receiveBufferSize);
212         } catch (SocketException e) {
213             throw new ChannelException(e);
214         }
215         return this;
216     }
217 
218     @Override
219     public SocketChannelConfig setReuseAddress(boolean reuseAddress) {
220         try {
221             javaSocket.setReuseAddress(reuseAddress);
222         } catch (SocketException e) {
223             throw new ChannelException(e);
224         }
225         return this;
226     }
227 
228     @Override
229     public SocketChannelConfig setSendBufferSize(int sendBufferSize) {
230         try {
231             javaSocket.setSendBufferSize(sendBufferSize);
232         } catch (SocketException e) {
233             throw new ChannelException(e);
234         }
235         return this;
236     }
237 
238     @Override
239     public SocketChannelConfig setSoLinger(int soLinger) {
240         try {
241             if (soLinger < 0) {
242                 javaSocket.setSoLinger(false, 0);
243             } else {
244                 javaSocket.setSoLinger(true, soLinger);
245             }
246         } catch (SocketException e) {
247             throw new ChannelException(e);
248         }
249         return this;
250     }
251 
252     @Override
253     public SocketChannelConfig setTcpNoDelay(boolean tcpNoDelay) {
254         try {
255             javaSocket.setTcpNoDelay(tcpNoDelay);
256         } catch (SocketException e) {
257             throw new ChannelException(e);
258         }
259         return this;
260     }
261 
262     @Override
263     public SocketChannelConfig setTrafficClass(int trafficClass) {
264         try {
265             javaSocket.setTrafficClass(trafficClass);
266         } catch (SocketException e) {
267             throw new ChannelException(e);
268         }
269         return this;
270     }
271 
272     @Override
273     public boolean isAllowHalfClosure() {
274         return allowHalfClosure;
275     }
276 
277     @Override
278     public SocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure) {
279         this.allowHalfClosure = allowHalfClosure;
280         return this;
281     }
282 
283     @Override
284     public SocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
285         super.setConnectTimeoutMillis(connectTimeoutMillis);
286         return this;
287     }
288 
289     @Override
290     @Deprecated
291     public SocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
292         super.setMaxMessagesPerRead(maxMessagesPerRead);
293         return this;
294     }
295 
296     @Override
297     public SocketChannelConfig setWriteSpinCount(int writeSpinCount) {
298         super.setWriteSpinCount(writeSpinCount);
299         return this;
300     }
301 
302     @Override
303     public SocketChannelConfig setAllocator(ByteBufAllocator allocator) {
304         super.setAllocator(allocator);
305         return this;
306     }
307 
308     @Override
309     public SocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
310         super.setRecvByteBufAllocator(allocator);
311         return this;
312     }
313 
314     @Override
315     public SocketChannelConfig setAutoRead(boolean autoRead) {
316          super.setAutoRead(autoRead);
317         return this;
318     }
319 
320     @Override
321     public SocketChannelConfig setAutoClose(boolean autoClose) {
322         super.setAutoClose(autoClose);
323         return this;
324     }
325 
326     @Override
327     public SocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
328         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
329         return this;
330     }
331 
332     @Override
333     public SocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
334         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
335         return this;
336     }
337 
338     @Override
339     public SocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
340         super.setWriteBufferWaterMark(writeBufferWaterMark);
341         return this;
342     }
343 
344     @Override
345     public SocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
346         super.setMessageSizeEstimator(estimator);
347         return this;
348     }
349 }