1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.epoll;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufAllocator;
20 import io.netty.channel.ChannelException;
21 import io.netty.channel.ChannelOption;
22 import io.netty.channel.FixedRecvByteBufAllocator;
23 import io.netty.channel.MessageSizeEstimator;
24 import io.netty.channel.RecvByteBufAllocator;
25 import io.netty.channel.WriteBufferWaterMark;
26 import io.netty.channel.socket.DatagramChannelConfig;
27 import io.netty.util.internal.ObjectUtil;
28
29 import java.io.IOException;
30 import java.net.InetAddress;
31 import java.net.NetworkInterface;
32 import java.util.Map;
33
34 public final class EpollDatagramChannelConfig extends EpollChannelConfig implements DatagramChannelConfig {
35 private boolean activeOnOpen;
36 private volatile int maxDatagramSize;
37
38 EpollDatagramChannelConfig(EpollDatagramChannel channel) {
39 super(channel, new FixedRecvByteBufAllocator(2048));
40 }
41
42 @Override
43 @SuppressWarnings("deprecation")
44 public Map<ChannelOption<?>, Object> getOptions() {
45 return getOptions(
46 super.getOptions(),
47 ChannelOption.SO_BROADCAST, ChannelOption.SO_RCVBUF, ChannelOption.SO_SNDBUF,
48 ChannelOption.SO_REUSEADDR, ChannelOption.IP_MULTICAST_LOOP_DISABLED,
49 ChannelOption.IP_MULTICAST_ADDR, ChannelOption.IP_MULTICAST_IF, ChannelOption.IP_MULTICAST_TTL,
50 ChannelOption.IP_TOS, ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION,
51 EpollChannelOption.SO_REUSEPORT, EpollChannelOption.IP_FREEBIND, EpollChannelOption.IP_TRANSPARENT,
52 EpollChannelOption.IP_RECVORIGDSTADDR, EpollChannelOption.MAX_DATAGRAM_PAYLOAD_SIZE,
53 EpollChannelOption.UDP_GRO);
54 }
55
56 @SuppressWarnings({ "unchecked", "deprecation" })
57 @Override
58 public <T> T getOption(ChannelOption<T> option) {
59 if (option == ChannelOption.SO_BROADCAST) {
60 return (T) Boolean.valueOf(isBroadcast());
61 }
62 if (option == ChannelOption.SO_RCVBUF) {
63 return (T) Integer.valueOf(getReceiveBufferSize());
64 }
65 if (option == ChannelOption.SO_SNDBUF) {
66 return (T) Integer.valueOf(getSendBufferSize());
67 }
68 if (option == ChannelOption.SO_REUSEADDR) {
69 return (T) Boolean.valueOf(isReuseAddress());
70 }
71 if (option == ChannelOption.IP_MULTICAST_LOOP_DISABLED) {
72 return (T) Boolean.valueOf(isLoopbackModeDisabled());
73 }
74 if (option == ChannelOption.IP_MULTICAST_ADDR) {
75 return (T) getInterface();
76 }
77 if (option == ChannelOption.IP_MULTICAST_IF) {
78 return (T) getNetworkInterface();
79 }
80 if (option == ChannelOption.IP_MULTICAST_TTL) {
81 return (T) Integer.valueOf(getTimeToLive());
82 }
83 if (option == ChannelOption.IP_TOS) {
84 return (T) Integer.valueOf(getTrafficClass());
85 }
86 if (option == ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
87 return (T) Boolean.valueOf(activeOnOpen);
88 }
89 if (option == EpollChannelOption.SO_REUSEPORT) {
90 return (T) Boolean.valueOf(isReusePort());
91 }
92 if (option == EpollChannelOption.IP_TRANSPARENT) {
93 return (T) Boolean.valueOf(isIpTransparent());
94 }
95 if (option == EpollChannelOption.IP_FREEBIND) {
96 return (T) Boolean.valueOf(isFreeBind());
97 }
98 if (option == EpollChannelOption.IP_RECVORIGDSTADDR) {
99 return (T) Boolean.valueOf(isIpRecvOrigDestAddr());
100 }
101 if (option == EpollChannelOption.MAX_DATAGRAM_PAYLOAD_SIZE) {
102 return (T) Integer.valueOf(getMaxDatagramPayloadSize());
103 }
104 if (option == EpollChannelOption.UDP_GRO) {
105 return (T) Boolean.valueOf(isUdpGro());
106 }
107 return super.getOption(option);
108 }
109
110 @Override
111 @SuppressWarnings("deprecation")
112 public <T> boolean setOption(ChannelOption<T> option, T value) {
113 validate(option, value);
114
115 if (option == ChannelOption.SO_BROADCAST) {
116 setBroadcast((Boolean) value);
117 } else if (option == ChannelOption.SO_RCVBUF) {
118 setReceiveBufferSize((Integer) value);
119 } else if (option == ChannelOption.SO_SNDBUF) {
120 setSendBufferSize((Integer) value);
121 } else if (option == ChannelOption.SO_REUSEADDR) {
122 setReuseAddress((Boolean) value);
123 } else if (option == ChannelOption.IP_MULTICAST_LOOP_DISABLED) {
124 setLoopbackModeDisabled((Boolean) value);
125 } else if (option == ChannelOption.IP_MULTICAST_ADDR) {
126 setInterface((InetAddress) value);
127 } else if (option == ChannelOption.IP_MULTICAST_IF) {
128 setNetworkInterface((NetworkInterface) value);
129 } else if (option == ChannelOption.IP_MULTICAST_TTL) {
130 setTimeToLive((Integer) value);
131 } else if (option == ChannelOption.IP_TOS) {
132 setTrafficClass((Integer) value);
133 } else if (option == ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
134 setActiveOnOpen((Boolean) value);
135 } else if (option == EpollChannelOption.SO_REUSEPORT) {
136 setReusePort((Boolean) value);
137 } else if (option == EpollChannelOption.IP_FREEBIND) {
138 setFreeBind((Boolean) value);
139 } else if (option == EpollChannelOption.IP_TRANSPARENT) {
140 setIpTransparent((Boolean) value);
141 } else if (option == EpollChannelOption.IP_RECVORIGDSTADDR) {
142 setIpRecvOrigDestAddr((Boolean) value);
143 } else if (option == EpollChannelOption.MAX_DATAGRAM_PAYLOAD_SIZE) {
144 setMaxDatagramPayloadSize((Integer) value);
145 } else if (option == EpollChannelOption.UDP_GRO) {
146 setUdpGro((Boolean) value);
147 } else {
148 return super.setOption(option, value);
149 }
150
151 return true;
152 }
153
154 private void setActiveOnOpen(boolean activeOnOpen) {
155 if (channel.isRegistered()) {
156 throw new IllegalStateException("Can only changed before channel was registered");
157 }
158 this.activeOnOpen = activeOnOpen;
159 }
160
161 boolean getActiveOnOpen() {
162 return activeOnOpen;
163 }
164
165 @Override
166 public EpollDatagramChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
167 super.setMessageSizeEstimator(estimator);
168 return this;
169 }
170
171 @Override
172 @Deprecated
173 public EpollDatagramChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
174 super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
175 return this;
176 }
177
178 @Override
179 @Deprecated
180 public EpollDatagramChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
181 super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
182 return this;
183 }
184
185 @Override
186 public EpollDatagramChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
187 super.setWriteBufferWaterMark(writeBufferWaterMark);
188 return this;
189 }
190
191 @Override
192 public EpollDatagramChannelConfig setAutoClose(boolean autoClose) {
193 super.setAutoClose(autoClose);
194 return this;
195 }
196
197 @Override
198 public EpollDatagramChannelConfig setAutoRead(boolean autoRead) {
199 super.setAutoRead(autoRead);
200 return this;
201 }
202
203 @Override
204 public EpollDatagramChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
205 super.setRecvByteBufAllocator(allocator);
206 return this;
207 }
208
209 @Override
210 public EpollDatagramChannelConfig setWriteSpinCount(int writeSpinCount) {
211 super.setWriteSpinCount(writeSpinCount);
212 return this;
213 }
214
215 @Override
216 public EpollDatagramChannelConfig setAllocator(ByteBufAllocator allocator) {
217 super.setAllocator(allocator);
218 return this;
219 }
220
221 @Override
222 public EpollDatagramChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
223 super.setConnectTimeoutMillis(connectTimeoutMillis);
224 return this;
225 }
226
227 @Override
228 @Deprecated
229 public EpollDatagramChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
230 super.setMaxMessagesPerRead(maxMessagesPerRead);
231 return this;
232 }
233
234 @Override
235 public int getSendBufferSize() {
236 try {
237 return ((EpollDatagramChannel) channel).socket.getSendBufferSize();
238 } catch (IOException e) {
239 throw new ChannelException(e);
240 }
241 }
242
243 @Override
244 public EpollDatagramChannelConfig setSendBufferSize(int sendBufferSize) {
245 try {
246 ((EpollDatagramChannel) channel).socket.setSendBufferSize(sendBufferSize);
247 return this;
248 } catch (IOException e) {
249 throw new ChannelException(e);
250 }
251 }
252
253 @Override
254 public int getReceiveBufferSize() {
255 try {
256 return ((EpollDatagramChannel) channel).socket.getReceiveBufferSize();
257 } catch (IOException e) {
258 throw new ChannelException(e);
259 }
260 }
261
262 @Override
263 public EpollDatagramChannelConfig setReceiveBufferSize(int receiveBufferSize) {
264 try {
265 ((EpollDatagramChannel) channel).socket.setReceiveBufferSize(receiveBufferSize);
266 return this;
267 } catch (IOException e) {
268 throw new ChannelException(e);
269 }
270 }
271
272 @Override
273 public int getTrafficClass() {
274 try {
275 return ((EpollDatagramChannel) channel).socket.getTrafficClass();
276 } catch (IOException e) {
277 throw new ChannelException(e);
278 }
279 }
280
281 @Override
282 public EpollDatagramChannelConfig setTrafficClass(int trafficClass) {
283 try {
284 ((EpollDatagramChannel) channel).socket.setTrafficClass(trafficClass);
285 return this;
286 } catch (IOException e) {
287 throw new ChannelException(e);
288 }
289 }
290
291 @Override
292 public boolean isReuseAddress() {
293 try {
294 return ((EpollDatagramChannel) channel).socket.isReuseAddress();
295 } catch (IOException e) {
296 throw new ChannelException(e);
297 }
298 }
299
300 @Override
301 public EpollDatagramChannelConfig setReuseAddress(boolean reuseAddress) {
302 try {
303 ((EpollDatagramChannel) channel).socket.setReuseAddress(reuseAddress);
304 return this;
305 } catch (IOException e) {
306 throw new ChannelException(e);
307 }
308 }
309
310 @Override
311 public boolean isBroadcast() {
312 try {
313 return ((EpollDatagramChannel) channel).socket.isBroadcast();
314 } catch (IOException e) {
315 throw new ChannelException(e);
316 }
317 }
318
319 @Override
320 public EpollDatagramChannelConfig setBroadcast(boolean broadcast) {
321 try {
322 ((EpollDatagramChannel) channel).socket.setBroadcast(broadcast);
323 return this;
324 } catch (IOException e) {
325 throw new ChannelException(e);
326 }
327 }
328
329 @Override
330 public boolean isLoopbackModeDisabled() {
331 try {
332 return ((EpollDatagramChannel) channel).socket.isLoopbackModeDisabled();
333 } catch (IOException e) {
334 throw new ChannelException(e);
335 }
336 }
337
338 @Override
339 public DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled) {
340 try {
341 ((EpollDatagramChannel) channel).socket.setLoopbackModeDisabled(loopbackModeDisabled);
342 return this;
343 } catch (IOException e) {
344 throw new ChannelException(e);
345 }
346 }
347
348 @Override
349 public int getTimeToLive() {
350 try {
351 return ((EpollDatagramChannel) channel).socket.getTimeToLive();
352 } catch (IOException e) {
353 throw new ChannelException(e);
354 }
355 }
356
357 @Override
358 public EpollDatagramChannelConfig setTimeToLive(int ttl) {
359 try {
360 ((EpollDatagramChannel) channel).socket.setTimeToLive(ttl);
361 return this;
362 } catch (IOException e) {
363 throw new ChannelException(e);
364 }
365 }
366
367 @Override
368 public InetAddress getInterface() {
369 try {
370 return ((EpollDatagramChannel) channel).socket.getInterface();
371 } catch (IOException e) {
372 throw new ChannelException(e);
373 }
374 }
375
376 @Override
377 public EpollDatagramChannelConfig setInterface(InetAddress interfaceAddress) {
378 try {
379 ((EpollDatagramChannel) channel).socket.setInterface(interfaceAddress);
380 return this;
381 } catch (IOException e) {
382 throw new ChannelException(e);
383 }
384 }
385
386 @Override
387 public NetworkInterface getNetworkInterface() {
388 try {
389 return ((EpollDatagramChannel) channel).socket.getNetworkInterface();
390 } catch (IOException e) {
391 throw new ChannelException(e);
392 }
393 }
394
395 @Override
396 public EpollDatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface) {
397 try {
398 EpollDatagramChannel datagramChannel = (EpollDatagramChannel) channel;
399 datagramChannel.socket.setNetworkInterface(networkInterface);
400 return this;
401 } catch (IOException e) {
402 throw new ChannelException(e);
403 }
404 }
405
406 @Override
407 public EpollDatagramChannelConfig setEpollMode(EpollMode mode) {
408 super.setEpollMode(mode);
409 return this;
410 }
411
412
413
414
415 public boolean isReusePort() {
416 try {
417 return ((EpollDatagramChannel) channel).socket.isReusePort();
418 } catch (IOException e) {
419 throw new ChannelException(e);
420 }
421 }
422
423
424
425
426
427
428
429
430 public EpollDatagramChannelConfig setReusePort(boolean reusePort) {
431 try {
432 ((EpollDatagramChannel) channel).socket.setReusePort(reusePort);
433 return this;
434 } catch (IOException e) {
435 throw new ChannelException(e);
436 }
437 }
438
439
440
441
442
443 public boolean isIpTransparent() {
444 try {
445 return ((EpollDatagramChannel) channel).socket.isIpTransparent();
446 } catch (IOException e) {
447 throw new ChannelException(e);
448 }
449 }
450
451
452
453
454
455 public EpollDatagramChannelConfig setIpTransparent(boolean ipTransparent) {
456 try {
457 ((EpollDatagramChannel) channel).socket.setIpTransparent(ipTransparent);
458 return this;
459 } catch (IOException e) {
460 throw new ChannelException(e);
461 }
462 }
463
464
465
466
467
468 public boolean isFreeBind() {
469 try {
470 return ((EpollDatagramChannel) channel).socket.isIpFreeBind();
471 } catch (IOException e) {
472 throw new ChannelException(e);
473 }
474 }
475
476
477
478
479
480 public EpollDatagramChannelConfig setFreeBind(boolean freeBind) {
481 try {
482 ((EpollDatagramChannel) channel).socket.setIpFreeBind(freeBind);
483 return this;
484 } catch (IOException e) {
485 throw new ChannelException(e);
486 }
487 }
488
489
490
491
492
493 public boolean isIpRecvOrigDestAddr() {
494 try {
495 return ((EpollDatagramChannel) channel).socket.isIpRecvOrigDestAddr();
496 } catch (IOException e) {
497 throw new ChannelException(e);
498 }
499 }
500
501
502
503
504
505 public EpollDatagramChannelConfig setIpRecvOrigDestAddr(boolean ipTransparent) {
506 try {
507 ((EpollDatagramChannel) channel).socket.setIpRecvOrigDestAddr(ipTransparent);
508 return this;
509 } catch (IOException e) {
510 throw new ChannelException(e);
511 }
512 }
513
514
515
516
517
518
519
520
521
522 public EpollDatagramChannelConfig setMaxDatagramPayloadSize(int maxDatagramSize) {
523 this.maxDatagramSize = ObjectUtil.checkPositiveOrZero(maxDatagramSize, "maxDatagramSize");
524 return this;
525 }
526
527
528
529
530 public int getMaxDatagramPayloadSize() {
531 return maxDatagramSize;
532 }
533
534 private volatile boolean gro;
535
536
537
538
539
540
541 public EpollDatagramChannelConfig setUdpGro(boolean gro) {
542 try {
543 ((EpollDatagramChannel) channel).socket.setUdpGro(gro);
544 } catch (IOException e) {
545 throw new ChannelException(e);
546 }
547 this.gro = gro;
548 return this;
549 }
550
551
552
553
554
555 public boolean isUdpGro() {
556
557
558 return gro;
559 }
560
561 @Override
562 public EpollDatagramChannelConfig setMaxMessagesPerWrite(int maxMessagesPerWrite) {
563 super.setMaxMessagesPerWrite(maxMessagesPerWrite);
564 return this;
565 }
566 }