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