1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.socket.nio;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.AddressedEnvelope;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelException;
22 import io.netty.channel.ChannelFuture;
23 import io.netty.channel.ChannelMetadata;
24 import io.netty.channel.ChannelOption;
25 import io.netty.channel.ChannelOutboundBuffer;
26 import io.netty.channel.ChannelPromise;
27 import io.netty.channel.DefaultAddressedEnvelope;
28 import io.netty.channel.RecvByteBufAllocator;
29 import io.netty.channel.nio.AbstractNioMessageChannel;
30 import io.netty.channel.socket.DatagramChannelConfig;
31 import io.netty.channel.socket.DatagramPacket;
32 import io.netty.channel.socket.InternetProtocolFamily;
33 import io.netty.util.UncheckedBooleanSupplier;
34 import io.netty.util.internal.ObjectUtil;
35 import io.netty.util.internal.SocketUtils;
36 import io.netty.util.internal.PlatformDependent;
37 import io.netty.util.internal.StringUtil;
38 import io.netty.util.internal.SuppressJava6Requirement;
39
40 import java.io.IOException;
41 import java.net.InetAddress;
42 import java.net.InetSocketAddress;
43 import java.net.NetworkInterface;
44 import java.net.SocketAddress;
45 import java.net.SocketException;
46 import java.nio.ByteBuffer;
47 import java.nio.channels.DatagramChannel;
48 import java.nio.channels.MembershipKey;
49 import java.nio.channels.SelectionKey;
50 import java.nio.channels.UnresolvedAddressException;
51 import java.nio.channels.spi.SelectorProvider;
52 import java.util.ArrayList;
53 import java.util.HashMap;
54 import java.util.Iterator;
55 import java.util.List;
56 import java.util.Map;
57
58
59
60
61
62
63
64
65 public final class NioDatagramChannel
66 extends AbstractNioMessageChannel implements io.netty.channel.socket.DatagramChannel {
67
68 private static final ChannelMetadata METADATA = new ChannelMetadata(true, 16);
69 private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
70 private static final String EXPECTED_TYPES =
71 " (expected: " + StringUtil.simpleClassName(DatagramPacket.class) + ", " +
72 StringUtil.simpleClassName(AddressedEnvelope.class) + '<' +
73 StringUtil.simpleClassName(ByteBuf.class) + ", " +
74 StringUtil.simpleClassName(SocketAddress.class) + ">, " +
75 StringUtil.simpleClassName(ByteBuf.class) + ')';
76
77 private final DatagramChannelConfig config;
78
79 private Map<InetAddress, List<MembershipKey>> memberships;
80
81 private static DatagramChannel newSocket(SelectorProvider provider) {
82 try {
83
84
85
86
87
88
89 return provider.openDatagramChannel();
90 } catch (IOException e) {
91 throw new ChannelException("Failed to open a socket.", e);
92 }
93 }
94
95 @SuppressJava6Requirement(reason = "Usage guarded by java version check")
96 private static DatagramChannel newSocket(SelectorProvider provider, InternetProtocolFamily ipFamily) {
97 if (ipFamily == null) {
98 return newSocket(provider);
99 }
100
101 checkJavaVersion();
102
103 try {
104 return provider.openDatagramChannel(ProtocolFamilyConverter.convert(ipFamily));
105 } catch (IOException e) {
106 throw new ChannelException("Failed to open a socket.", e);
107 }
108 }
109
110 private static void checkJavaVersion() {
111 if (PlatformDependent.javaVersion() < 7) {
112 throw new UnsupportedOperationException("Only supported on java 7+.");
113 }
114 }
115
116
117
118
119 public NioDatagramChannel() {
120 this(newSocket(DEFAULT_SELECTOR_PROVIDER));
121 }
122
123
124
125
126
127 public NioDatagramChannel(SelectorProvider provider) {
128 this(newSocket(provider));
129 }
130
131
132
133
134
135 public NioDatagramChannel(InternetProtocolFamily ipFamily) {
136 this(newSocket(DEFAULT_SELECTOR_PROVIDER, ipFamily));
137 }
138
139
140
141
142
143
144 public NioDatagramChannel(SelectorProvider provider, InternetProtocolFamily ipFamily) {
145 this(newSocket(provider, ipFamily));
146 }
147
148
149
150
151 public NioDatagramChannel(DatagramChannel socket) {
152 super(null, socket, SelectionKey.OP_READ);
153 config = new NioDatagramChannelConfig(this, socket);
154 }
155
156 @Override
157 public ChannelMetadata metadata() {
158 return METADATA;
159 }
160
161 @Override
162 public DatagramChannelConfig config() {
163 return config;
164 }
165
166 @Override
167 @SuppressWarnings("deprecation")
168 public boolean isActive() {
169 DatagramChannel ch = javaChannel();
170 return ch.isOpen() && (
171 config.getOption(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) && isRegistered()
172 || ch.socket().isBound());
173 }
174
175 @Override
176 public boolean isConnected() {
177 return javaChannel().isConnected();
178 }
179
180 @Override
181 protected DatagramChannel javaChannel() {
182 return (DatagramChannel) super.javaChannel();
183 }
184
185 @Override
186 protected SocketAddress localAddress0() {
187 return javaChannel().socket().getLocalSocketAddress();
188 }
189
190 @Override
191 protected SocketAddress remoteAddress0() {
192 return javaChannel().socket().getRemoteSocketAddress();
193 }
194
195 @Override
196 protected void doBind(SocketAddress localAddress) throws Exception {
197 doBind0(localAddress);
198 }
199
200 private void doBind0(SocketAddress localAddress) throws Exception {
201 if (PlatformDependent.javaVersion() >= 7) {
202 SocketUtils.bind(javaChannel(), localAddress);
203 } else {
204 javaChannel().socket().bind(localAddress);
205 }
206 }
207
208 @Override
209 protected boolean doConnect(SocketAddress remoteAddress,
210 SocketAddress localAddress) throws Exception {
211 if (localAddress != null) {
212 doBind0(localAddress);
213 }
214
215 boolean success = false;
216 try {
217 javaChannel().connect(remoteAddress);
218 success = true;
219 return true;
220 } finally {
221 if (!success) {
222 doClose();
223 }
224 }
225 }
226
227 @Override
228 protected void doFinishConnect() throws Exception {
229 throw new Error();
230 }
231
232 @Override
233 protected void doDisconnect() throws Exception {
234 javaChannel().disconnect();
235 }
236
237 @Override
238 protected void doClose() throws Exception {
239 javaChannel().close();
240 }
241
242 @Override
243 protected int doReadMessages(List<Object> buf) throws Exception {
244 DatagramChannel ch = javaChannel();
245 DatagramChannelConfig config = config();
246 RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
247
248 ByteBuf data = allocHandle.allocate(config.getAllocator());
249 allocHandle.attemptedBytesRead(data.writableBytes());
250 boolean free = true;
251 try {
252 ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
253 int pos = nioData.position();
254 InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
255 if (remoteAddress == null) {
256 return 0;
257 }
258
259 allocHandle.lastBytesRead(nioData.position() - pos);
260 buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()),
261 localAddress(), remoteAddress));
262 free = false;
263 return 1;
264 } catch (Throwable cause) {
265 PlatformDependent.throwException(cause);
266 return -1;
267 } finally {
268 if (free) {
269 data.release();
270 }
271 }
272 }
273
274 @Override
275 protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
276 final SocketAddress remoteAddress;
277 final ByteBuf data;
278 if (msg instanceof AddressedEnvelope) {
279 @SuppressWarnings("unchecked")
280 AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) msg;
281 remoteAddress = envelope.recipient();
282 data = envelope.content();
283 } else {
284 data = (ByteBuf) msg;
285 remoteAddress = null;
286 }
287
288 final int dataLen = data.readableBytes();
289 final ByteBuffer nioData = data.nioBufferCount() == 1 ? data.internalNioBuffer(data.readerIndex(), dataLen)
290 : data.nioBuffer(data.readerIndex(), dataLen);
291 final int writtenBytes;
292 if (remoteAddress != null) {
293 writtenBytes = javaChannel().send(nioData, remoteAddress);
294 } else {
295 writtenBytes = javaChannel().write(nioData);
296 }
297
298 return writtenBytes > 0 || dataLen == 0;
299 }
300
301 private static void checkUnresolved(AddressedEnvelope<?, ?> envelope) {
302 if (envelope.recipient() instanceof InetSocketAddress
303 && (((InetSocketAddress) envelope.recipient()).isUnresolved())) {
304 throw new UnresolvedAddressException();
305 }
306 }
307
308 @Override
309 protected Object filterOutboundMessage(Object msg) {
310 if (msg instanceof DatagramPacket) {
311 DatagramPacket p = (DatagramPacket) msg;
312 checkUnresolved(p);
313 ByteBuf content = p.content();
314 if (isSingleDirectBuffer(content)) {
315 return p;
316 }
317 return new DatagramPacket(newDirectBuffer(p, content), p.recipient());
318 }
319
320 if (msg instanceof ByteBuf) {
321 ByteBuf buf = (ByteBuf) msg;
322 if (isSingleDirectBuffer(buf)) {
323 return buf;
324 }
325 return newDirectBuffer(buf);
326 }
327
328 if (msg instanceof AddressedEnvelope) {
329 @SuppressWarnings("unchecked")
330 AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg;
331 checkUnresolved(e);
332 if (e.content() instanceof ByteBuf) {
333 ByteBuf content = (ByteBuf) e.content();
334 if (isSingleDirectBuffer(content)) {
335 return e;
336 }
337 return new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(newDirectBuffer(e, content), e.recipient());
338 }
339 }
340
341 throw new UnsupportedOperationException(
342 "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES);
343 }
344
345
346
347
348
349 private static boolean isSingleDirectBuffer(ByteBuf buf) {
350 return buf.isDirect() && buf.nioBufferCount() == 1;
351 }
352
353 @Override
354 protected boolean continueOnWriteError() {
355
356
357
358 return true;
359 }
360
361 @Override
362 public InetSocketAddress localAddress() {
363 return (InetSocketAddress) super.localAddress();
364 }
365
366 @Override
367 public InetSocketAddress remoteAddress() {
368 return (InetSocketAddress) super.remoteAddress();
369 }
370
371 @Override
372 public ChannelFuture joinGroup(InetAddress multicastAddress) {
373 return joinGroup(multicastAddress, newPromise());
374 }
375
376 @Override
377 public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) {
378 final NetworkInterface iface;
379 try {
380 iface = defaultMulticastInterface();
381 } catch (SocketException e) {
382 return promise.setFailure(e);
383 }
384 return joinGroup(multicastAddress, iface, null, promise);
385 }
386
387
388
389
390
391 private NetworkInterface defaultMulticastInterface() throws SocketException {
392 NetworkInterface iface = config.getNetworkInterface();
393 if (iface == null) {
394 iface = NetworkInterface.getByInetAddress(localAddress().getAddress());
395 }
396 return iface;
397 }
398
399 @Override
400 public ChannelFuture joinGroup(
401 InetSocketAddress multicastAddress, NetworkInterface networkInterface) {
402 return joinGroup(multicastAddress, networkInterface, newPromise());
403 }
404
405 @Override
406 public ChannelFuture joinGroup(
407 InetSocketAddress multicastAddress, NetworkInterface networkInterface,
408 ChannelPromise promise) {
409 return joinGroup(multicastAddress.getAddress(), networkInterface, null, promise);
410 }
411
412 @Override
413 public ChannelFuture joinGroup(
414 InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source) {
415 return joinGroup(multicastAddress, networkInterface, source, newPromise());
416 }
417
418 @SuppressJava6Requirement(reason = "Usage guarded by java version check")
419 @Override
420 public ChannelFuture joinGroup(
421 InetAddress multicastAddress, NetworkInterface networkInterface,
422 InetAddress source, ChannelPromise promise) {
423
424 checkJavaVersion();
425
426 ObjectUtil.checkNotNull(multicastAddress, "multicastAddress");
427 ObjectUtil.checkNotNull(networkInterface, "networkInterface");
428
429 try {
430 MembershipKey key;
431 if (source == null) {
432 key = javaChannel().join(multicastAddress, networkInterface);
433 } else {
434 key = javaChannel().join(multicastAddress, networkInterface, source);
435 }
436
437 synchronized (this) {
438 List<MembershipKey> keys = null;
439 if (memberships == null) {
440 memberships = new HashMap<InetAddress, List<MembershipKey>>();
441 } else {
442 keys = memberships.get(multicastAddress);
443 }
444 if (keys == null) {
445 keys = new ArrayList<MembershipKey>();
446 memberships.put(multicastAddress, keys);
447 }
448 keys.add(key);
449 }
450
451 promise.setSuccess();
452 } catch (Throwable e) {
453 promise.setFailure(e);
454 }
455
456 return promise;
457 }
458
459 @Override
460 public ChannelFuture leaveGroup(InetAddress multicastAddress) {
461 return leaveGroup(multicastAddress, newPromise());
462 }
463
464 @Override
465 public ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelPromise promise) {
466 try {
467 return leaveGroup(multicastAddress, defaultMulticastInterface(), null, promise);
468 } catch (SocketException e) {
469 promise.setFailure(e);
470 }
471 return promise;
472 }
473
474 @Override
475 public ChannelFuture leaveGroup(
476 InetSocketAddress multicastAddress, NetworkInterface networkInterface) {
477 return leaveGroup(multicastAddress, networkInterface, newPromise());
478 }
479
480 @Override
481 public ChannelFuture leaveGroup(
482 InetSocketAddress multicastAddress,
483 NetworkInterface networkInterface, ChannelPromise promise) {
484 return leaveGroup(multicastAddress.getAddress(), networkInterface, null, promise);
485 }
486
487 @Override
488 public ChannelFuture leaveGroup(
489 InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source) {
490 return leaveGroup(multicastAddress, networkInterface, source, newPromise());
491 }
492
493 @SuppressJava6Requirement(reason = "Usage guarded by java version check")
494 @Override
495 public ChannelFuture leaveGroup(
496 InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source,
497 ChannelPromise promise) {
498 checkJavaVersion();
499
500 ObjectUtil.checkNotNull(multicastAddress, "multicastAddress");
501 ObjectUtil.checkNotNull(networkInterface, "networkInterface");
502
503 synchronized (this) {
504 if (memberships != null) {
505 List<MembershipKey> keys = memberships.get(multicastAddress);
506 if (keys != null) {
507 Iterator<MembershipKey> keyIt = keys.iterator();
508
509 while (keyIt.hasNext()) {
510 MembershipKey key = keyIt.next();
511 if (networkInterface.equals(key.networkInterface())) {
512 if (source == null && key.sourceAddress() == null ||
513 source != null && source.equals(key.sourceAddress())) {
514 key.drop();
515 keyIt.remove();
516 }
517 }
518 }
519 if (keys.isEmpty()) {
520 memberships.remove(multicastAddress);
521 }
522 }
523 }
524 }
525
526 promise.setSuccess();
527 return promise;
528 }
529
530
531
532
533 @Override
534 public ChannelFuture block(
535 InetAddress multicastAddress, NetworkInterface networkInterface,
536 InetAddress sourceToBlock) {
537 return block(multicastAddress, networkInterface, sourceToBlock, newPromise());
538 }
539
540
541
542
543 @SuppressJava6Requirement(reason = "Usage guarded by java version check")
544 @Override
545 public ChannelFuture block(
546 InetAddress multicastAddress, NetworkInterface networkInterface,
547 InetAddress sourceToBlock, ChannelPromise promise) {
548 checkJavaVersion();
549
550 ObjectUtil.checkNotNull(multicastAddress, "multicastAddress");
551 ObjectUtil.checkNotNull(sourceToBlock, "sourceToBlock");
552 ObjectUtil.checkNotNull(networkInterface, "networkInterface");
553
554 synchronized (this) {
555 if (memberships != null) {
556 List<MembershipKey> keys = memberships.get(multicastAddress);
557 for (MembershipKey key: keys) {
558 if (networkInterface.equals(key.networkInterface())) {
559 try {
560 key.block(sourceToBlock);
561 } catch (Throwable e) {
562 return promise.setFailure(e);
563 }
564 }
565 }
566 }
567 }
568 promise.setSuccess();
569 return promise;
570 }
571
572
573
574
575
576 @Override
577 public ChannelFuture block(InetAddress multicastAddress, InetAddress sourceToBlock) {
578 return block(multicastAddress, sourceToBlock, newPromise());
579 }
580
581
582
583
584
585 @Override
586 public ChannelFuture block(
587 InetAddress multicastAddress, InetAddress sourceToBlock, ChannelPromise promise) {
588 final NetworkInterface iface;
589 try {
590 iface = defaultMulticastInterface();
591 } catch (SocketException e) {
592 return promise.setFailure(e);
593 }
594 return block(multicastAddress, iface, sourceToBlock, promise);
595 }
596
597 @Override
598 @Deprecated
599 protected void setReadPending(boolean readPending) {
600 super.setReadPending(readPending);
601 }
602
603 void clearReadPending0() {
604 clearReadPending();
605 }
606
607 @Override
608 protected boolean closeOnReadError(Throwable cause) {
609
610
611 if (cause instanceof SocketException) {
612 return false;
613 }
614 return super.closeOnReadError(cause);
615 }
616
617 @Override
618 protected boolean continueReading(RecvByteBufAllocator.Handle allocHandle) {
619 if (allocHandle instanceof RecvByteBufAllocator.ExtendedHandle) {
620
621
622 return ((RecvByteBufAllocator.ExtendedHandle) allocHandle)
623 .continueReading(UncheckedBooleanSupplier.TRUE_SUPPLIER);
624 }
625 return allocHandle.continueReading();
626 }
627 }