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