equal
deleted
inserted
replaced
46 import java.util.Collections; |
46 import java.util.Collections; |
47 import java.util.HashSet; |
47 import java.util.HashSet; |
48 import java.util.Objects; |
48 import java.util.Objects; |
49 import java.util.Set; |
49 import java.util.Set; |
50 import java.util.concurrent.locks.ReentrantLock; |
50 import java.util.concurrent.locks.ReentrantLock; |
|
51 import jdk.internal.access.JavaNetDatagramPacketAccess; |
|
52 import jdk.internal.access.SharedSecrets; |
51 import jdk.internal.ref.CleanerFactory; |
53 import jdk.internal.ref.CleanerFactory; |
52 import sun.net.PlatformDatagramSocketImpl; |
54 import sun.net.PlatformDatagramSocketImpl; |
53 import sun.net.ResourceManager; |
55 import sun.net.ResourceManager; |
54 import sun.net.ext.ExtendedSocketOptions; |
56 import sun.net.ext.ExtendedSocketOptions; |
55 import sun.net.util.IPAddressUtil; |
57 import sun.net.util.IPAddressUtil; |
62 * A DatagramSocketImpl based on low-level NIO primitives. |
64 * A DatagramSocketImpl based on low-level NIO primitives. |
63 */ |
65 */ |
64 public class NioDatagramSocketImpl extends PlatformDatagramSocketImpl { |
66 public class NioDatagramSocketImpl extends PlatformDatagramSocketImpl { |
65 |
67 |
66 private static final NativeDispatcher nd = new SocketDispatcher(); |
68 private static final NativeDispatcher nd = new SocketDispatcher(); |
|
69 |
|
70 private static final JavaNetDatagramPacketAccess DATAGRAM_PACKET_ACCESS = |
|
71 SharedSecrets.getJavaNetDatagramPacketAccess(); |
67 |
72 |
68 private static final int MAX_PACKET_LEN = 65536; |
73 private static final int MAX_PACKET_LEN = 65536; |
69 |
74 |
70 private static final ProtocolFamily family = family(); |
75 private static final ProtocolFamily family = family(); |
71 |
76 |
408 |
413 |
409 private void receive(DatagramPacket p, boolean isPeek) throws IOException { |
414 private void receive(DatagramPacket p, boolean isPeek) throws IOException { |
410 Objects.requireNonNull(p); |
415 Objects.requireNonNull(p); |
411 byte[] b = p.getData(); |
416 byte[] b = p.getData(); |
412 int off = p.getOffset(); |
417 int off = p.getOffset(); |
413 int len = b.length - off; |
418 int len = DATAGRAM_PACKET_ACCESS.getBufLengthField(p); |
414 assert len >= 0; |
419 assert len >= 0; |
415 if (len > MAX_PACKET_LEN) |
420 if (len > MAX_PACKET_LEN) |
416 len = MAX_PACKET_LEN; |
421 len = MAX_PACKET_LEN; |
417 |
422 |
418 readLock.lock(); |
423 readLock.lock(); |
439 assert sender != null; |
444 assert sender != null; |
440 if (p.getAddress() == null || !p.getAddress().equals(sender.getAddress())) |
445 if (p.getAddress() == null || !p.getAddress().equals(sender.getAddress())) |
441 p.setAddress(sender.getAddress()); |
446 p.setAddress(sender.getAddress()); |
442 if (p.getPort() != sender.getPort()) |
447 if (p.getPort() != sender.getPort()) |
443 p.setPort(sender.getPort()); |
448 p.setPort(sender.getPort()); |
444 p.setLength(n); |
449 DATAGRAM_PACKET_ACCESS.setLengthField(p, n); |
445 } catch (IOException e) { |
450 } catch (IOException e) { |
446 // #### reset packet offset and length! ?? |
451 // #### reset packet offset and length! ?? |
447 throw e; |
452 throw e; |
448 } finally { |
453 } finally { |
449 endRead(n > 0); |
454 endRead(n > 0); |