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