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