View Javadoc
1   /*
2    * Copyright 2013 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.oio;
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.PreferHeapByteBufAllocator;
23  import io.netty.channel.RecvByteBufAllocator;
24  import io.netty.channel.WriteBufferWaterMark;
25  import io.netty.channel.socket.DefaultSocketChannelConfig;
26  import io.netty.channel.socket.SocketChannel;
27  
28  import java.io.IOException;
29  import java.net.Socket;
30  import java.util.Map;
31  
32  import static io.netty.channel.ChannelOption.*;
33  
34  /**
35   * Default {@link OioSocketChannelConfig} implementation
36   */
37  public class DefaultOioSocketChannelConfig extends DefaultSocketChannelConfig implements OioSocketChannelConfig {
38      @Deprecated
39      public DefaultOioSocketChannelConfig(SocketChannel channel, Socket javaSocket) {
40          super(channel, javaSocket);
41          setAllocator(new PreferHeapByteBufAllocator(getAllocator()));
42      }
43  
44      DefaultOioSocketChannelConfig(OioSocketChannel channel, Socket javaSocket) {
45          super(channel, javaSocket);
46          setAllocator(new PreferHeapByteBufAllocator(getAllocator()));
47      }
48  
49      @Override
50      public Map<ChannelOption<?>, Object> getOptions() {
51          return getOptions(
52                  super.getOptions(), SO_TIMEOUT);
53      }
54  
55      @SuppressWarnings("unchecked")
56      @Override
57      public <T> T getOption(ChannelOption<T> option) {
58          if (option == SO_TIMEOUT) {
59              return (T) Integer.valueOf(getSoTimeout());
60          }
61          return super.getOption(option);
62      }
63  
64      @Override
65      public <T> boolean setOption(ChannelOption<T> option, T value) {
66          validate(option, value);
67  
68          if (option == SO_TIMEOUT) {
69              setSoTimeout((Integer) value);
70          } else {
71              return super.setOption(option, value);
72          }
73          return true;
74      }
75  
76      @Override
77      public OioSocketChannelConfig setSoTimeout(int timeout) {
78          try {
79              javaSocket.setSoTimeout(timeout);
80          } catch (IOException e) {
81              throw new ChannelException(e);
82          }
83          return this;
84      }
85  
86      @Override
87      public int getSoTimeout() {
88          try {
89              return javaSocket.getSoTimeout();
90          } catch (IOException e) {
91              throw new ChannelException(e);
92          }
93      }
94  
95      @Override
96      public OioSocketChannelConfig setTcpNoDelay(boolean tcpNoDelay) {
97          super.setTcpNoDelay(tcpNoDelay);
98          return this;
99      }
100 
101     @Override
102     public OioSocketChannelConfig setSoLinger(int soLinger) {
103         super.setSoLinger(soLinger);
104         return this;
105     }
106 
107     @Override
108     public OioSocketChannelConfig setSendBufferSize(int sendBufferSize) {
109         super.setSendBufferSize(sendBufferSize);
110         return this;
111     }
112 
113     @Override
114     public OioSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
115         super.setReceiveBufferSize(receiveBufferSize);
116         return this;
117     }
118 
119     @Override
120     public OioSocketChannelConfig setKeepAlive(boolean keepAlive) {
121         super.setKeepAlive(keepAlive);
122         return this;
123     }
124 
125     @Override
126     public OioSocketChannelConfig setTrafficClass(int trafficClass) {
127         super.setTrafficClass(trafficClass);
128         return this;
129     }
130 
131     @Override
132     public OioSocketChannelConfig setReuseAddress(boolean reuseAddress) {
133         super.setReuseAddress(reuseAddress);
134         return this;
135     }
136 
137     @Override
138     public OioSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
139         super.setPerformancePreferences(connectionTime, latency, bandwidth);
140         return this;
141     }
142 
143     @Override
144     public OioSocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure) {
145         super.setAllowHalfClosure(allowHalfClosure);
146         return this;
147     }
148 
149     @Override
150     public OioSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
151         super.setConnectTimeoutMillis(connectTimeoutMillis);
152         return this;
153     }
154 
155     @Override
156     @Deprecated
157     public OioSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
158         super.setMaxMessagesPerRead(maxMessagesPerRead);
159         return this;
160     }
161 
162     @Override
163     public OioSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
164         super.setWriteSpinCount(writeSpinCount);
165         return this;
166     }
167 
168     @Override
169     public OioSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
170         super.setAllocator(allocator);
171         return this;
172     }
173 
174     @Override
175     public OioSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
176         super.setRecvByteBufAllocator(allocator);
177         return this;
178     }
179 
180     @Override
181     public OioSocketChannelConfig setAutoRead(boolean autoRead) {
182         super.setAutoRead(autoRead);
183         return this;
184     }
185 
186     @Override
187     protected void autoReadCleared() {
188         if (channel instanceof OioSocketChannel) {
189             ((OioSocketChannel) channel).clearReadPending0();
190         }
191     }
192 
193     @Override
194     public OioSocketChannelConfig setAutoClose(boolean autoClose) {
195         super.setAutoClose(autoClose);
196         return this;
197     }
198 
199     @Override
200     public OioSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
201         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
202         return this;
203     }
204 
205     @Override
206     public OioSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
207         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
208         return this;
209     }
210 
211     @Override
212     public OioSocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
213         super.setWriteBufferWaterMark(writeBufferWaterMark);
214         return this;
215     }
216 
217     @Override
218     public OioSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
219         super.setMessageSizeEstimator(estimator);
220         return this;
221     }
222 }