View Javadoc
1   /*
2    * Copyright 2012 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.epoll;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelOption;
20  import io.netty.channel.FixedRecvByteBufAllocator;
21  import io.netty.channel.MessageSizeEstimator;
22  import io.netty.channel.RecvByteBufAllocator;
23  import io.netty.channel.socket.DatagramChannelConfig;
24  
25  import java.net.InetAddress;
26  import java.net.NetworkInterface;
27  import java.util.Map;
28  
29  public final class EpollDatagramChannelConfig extends EpollChannelConfig implements DatagramChannelConfig {
30      private static final RecvByteBufAllocator DEFAULT_RCVBUF_ALLOCATOR = new FixedRecvByteBufAllocator(2048);
31      private final EpollDatagramChannel datagramChannel;
32      private boolean activeOnOpen;
33  
34      EpollDatagramChannelConfig(EpollDatagramChannel channel) {
35          super(channel);
36          datagramChannel = channel;
37          setRecvByteBufAllocator(DEFAULT_RCVBUF_ALLOCATOR);
38      }
39  
40      @Override
41      @SuppressWarnings("deprecation")
42      public Map<ChannelOption<?>, Object> getOptions() {
43          return getOptions(
44                  super.getOptions(),
45                  ChannelOption.SO_BROADCAST, ChannelOption.SO_RCVBUF, ChannelOption.SO_SNDBUF,
46                  ChannelOption.SO_REUSEADDR, ChannelOption.IP_MULTICAST_LOOP_DISABLED,
47                  ChannelOption.IP_MULTICAST_ADDR, ChannelOption.IP_MULTICAST_IF, ChannelOption.IP_MULTICAST_TTL,
48                  ChannelOption.IP_TOS, ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION,
49                  EpollChannelOption.SO_REUSEPORT);
50      }
51  
52      @SuppressWarnings({ "unchecked", "deprecation" })
53      @Override
54      public <T> T getOption(ChannelOption<T> option) {
55          if (option == ChannelOption.SO_BROADCAST) {
56              return (T) Boolean.valueOf(isBroadcast());
57          }
58          if (option == ChannelOption.SO_RCVBUF) {
59              return (T) Integer.valueOf(getReceiveBufferSize());
60          }
61          if (option == ChannelOption.SO_SNDBUF) {
62              return (T) Integer.valueOf(getSendBufferSize());
63          }
64          if (option == ChannelOption.SO_REUSEADDR) {
65              return (T) Boolean.valueOf(isReuseAddress());
66          }
67          if (option == ChannelOption.IP_MULTICAST_LOOP_DISABLED) {
68              return (T) Boolean.valueOf(isLoopbackModeDisabled());
69          }
70          if (option == ChannelOption.IP_MULTICAST_ADDR) {
71              return (T) getInterface();
72          }
73          if (option == ChannelOption.IP_MULTICAST_IF) {
74              return (T) getNetworkInterface();
75          }
76          if (option == ChannelOption.IP_MULTICAST_TTL) {
77              return (T) Integer.valueOf(getTimeToLive());
78          }
79          if (option == ChannelOption.IP_TOS) {
80              return (T) Integer.valueOf(getTrafficClass());
81          }
82          if (option == ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
83              return (T) Boolean.valueOf(activeOnOpen);
84          }
85          if (option == EpollChannelOption.SO_REUSEPORT) {
86              return (T) Boolean.valueOf(isReusePort());
87          }
88          return super.getOption(option);
89      }
90  
91      @Override
92      @SuppressWarnings("deprecation")
93      public <T> boolean setOption(ChannelOption<T> option, T value) {
94          validate(option, value);
95  
96          if (option == ChannelOption.SO_BROADCAST) {
97              setBroadcast((Boolean) value);
98          } else if (option == ChannelOption.SO_RCVBUF) {
99              setReceiveBufferSize((Integer) value);
100         } else if (option == ChannelOption.SO_SNDBUF) {
101             setSendBufferSize((Integer) value);
102         } else if (option == ChannelOption.SO_REUSEADDR) {
103             setReuseAddress((Boolean) value);
104         } else if (option == ChannelOption.IP_MULTICAST_LOOP_DISABLED) {
105             setLoopbackModeDisabled((Boolean) value);
106         } else if (option == ChannelOption.IP_MULTICAST_ADDR) {
107             setInterface((InetAddress) value);
108         } else if (option == ChannelOption.IP_MULTICAST_IF) {
109             setNetworkInterface((NetworkInterface) value);
110         } else if (option == ChannelOption.IP_MULTICAST_TTL) {
111             setTimeToLive((Integer) value);
112         } else if (option == ChannelOption.IP_TOS) {
113             setTrafficClass((Integer) value);
114         } else if (option == ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
115             setActiveOnOpen((Boolean) value);
116         } else if (option == EpollChannelOption.SO_REUSEPORT) {
117             setReusePort((Boolean) value);
118         } else {
119             return super.setOption(option, value);
120         }
121 
122         return true;
123     }
124 
125     private void setActiveOnOpen(boolean activeOnOpen) {
126         if (channel.isRegistered()) {
127             throw new IllegalStateException("Can only changed before channel was registered");
128         }
129         this.activeOnOpen = activeOnOpen;
130     }
131 
132     @Override
133     public EpollDatagramChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
134         super.setMessageSizeEstimator(estimator);
135         return this;
136     }
137 
138     @Override
139     public EpollDatagramChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
140         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
141         return this;
142     }
143 
144     @Override
145     public EpollDatagramChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
146         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
147         return this;
148     }
149 
150     @Override
151     public EpollDatagramChannelConfig setAutoRead(boolean autoRead) {
152         super.setAutoRead(autoRead);
153         return this;
154     }
155 
156     @Override
157     public EpollDatagramChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
158         super.setRecvByteBufAllocator(allocator);
159         return this;
160     }
161 
162     @Override
163     public EpollDatagramChannelConfig setWriteSpinCount(int writeSpinCount) {
164         super.setWriteSpinCount(writeSpinCount);
165         return this;
166     }
167 
168     @Override
169     public EpollDatagramChannelConfig setAllocator(ByteBufAllocator allocator) {
170         super.setAllocator(allocator);
171         return this;
172     }
173 
174     @Override
175     public EpollDatagramChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
176         super.setConnectTimeoutMillis(connectTimeoutMillis);
177         return this;
178     }
179 
180     @Override
181     public EpollDatagramChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
182         super.setMaxMessagesPerRead(maxMessagesPerRead);
183         return this;
184     }
185 
186     @Override
187     public int getSendBufferSize() {
188         return Native.getSendBufferSize(datagramChannel.fd().intValue());
189     }
190 
191     @Override
192     public EpollDatagramChannelConfig setSendBufferSize(int sendBufferSize) {
193         Native.setSendBufferSize(datagramChannel.fd().intValue(), sendBufferSize);
194         return this;
195     }
196 
197     @Override
198     public int getReceiveBufferSize() {
199         return Native.getReceiveBufferSize(datagramChannel.fd().intValue());
200     }
201 
202     @Override
203     public EpollDatagramChannelConfig setReceiveBufferSize(int receiveBufferSize) {
204         Native.setReceiveBufferSize(datagramChannel.fd().intValue(), receiveBufferSize);
205         return this;
206     }
207 
208     @Override
209     public int getTrafficClass() {
210         return Native.getTrafficClass(datagramChannel.fd().intValue());
211     }
212 
213     @Override
214     public EpollDatagramChannelConfig setTrafficClass(int trafficClass) {
215         Native.setTrafficClass(datagramChannel.fd().intValue(), trafficClass);
216         return this;
217     }
218 
219     @Override
220     public boolean isReuseAddress() {
221         return Native.isReuseAddress(datagramChannel.fd().intValue()) == 1;
222     }
223 
224     @Override
225     public EpollDatagramChannelConfig setReuseAddress(boolean reuseAddress) {
226         Native.setReuseAddress(datagramChannel.fd().intValue(), reuseAddress ? 1 : 0);
227         return this;
228     }
229 
230     @Override
231     public boolean isBroadcast() {
232         return Native.isBroadcast(datagramChannel.fd().intValue()) == 1;
233     }
234 
235     @Override
236     public EpollDatagramChannelConfig setBroadcast(boolean broadcast) {
237         Native.setBroadcast(datagramChannel.fd().intValue(), broadcast ? 1 : 0);
238         return this;
239     }
240 
241     @Override
242     public boolean isLoopbackModeDisabled() {
243         return false;
244     }
245 
246     @Override
247     public DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled) {
248         throw new UnsupportedOperationException("Multicast not supported");
249     }
250 
251     @Override
252     public int getTimeToLive() {
253         return -1;
254     }
255 
256     @Override
257     public EpollDatagramChannelConfig setTimeToLive(int ttl) {
258         throw new UnsupportedOperationException("Multicast not supported");
259     }
260 
261     @Override
262     public InetAddress getInterface() {
263         return null;
264     }
265 
266     @Override
267     public EpollDatagramChannelConfig setInterface(InetAddress interfaceAddress) {
268         throw new UnsupportedOperationException("Multicast not supported");
269     }
270 
271     @Override
272     public NetworkInterface getNetworkInterface() {
273         return null;
274     }
275 
276     @Override
277     public EpollDatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface) {
278         throw new UnsupportedOperationException("Multicast not supported");
279     }
280 
281     @Override
282     public EpollDatagramChannelConfig setEpollMode(EpollMode mode) {
283         super.setEpollMode(mode);
284         return this;
285     }
286 
287     /**
288      * Returns {@code true} if the SO_REUSEPORT option is set.
289      */
290     public boolean isReusePort() {
291         return Native.isReusePort(datagramChannel.fd().intValue()) == 1;
292     }
293 
294     /**
295      * Set the SO_REUSEPORT option on the underlying Channel. This will allow to bind multiple
296      * {@link EpollSocketChannel}s to the same port and so accept connections with multiple threads.
297      *
298      * Be aware this method needs be called before {@link EpollDatagramChannel#bind(java.net.SocketAddress)} to have
299      * any affect.
300      */
301     public EpollDatagramChannelConfig setReusePort(boolean reusePort) {
302         Native.setReusePort(datagramChannel.fd().intValue(), reusePort ? 1 : 0);
303         return this;
304     }
305 }