< prev index next >

src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java

Print this page




  40 import java.net.StandardProtocolFamily;
  41 import java.net.StandardSocketOptions;
  42 import java.nio.ByteBuffer;
  43 import java.nio.channels.AlreadyBoundException;
  44 import java.nio.channels.AlreadyConnectedException;
  45 import java.nio.channels.AsynchronousCloseException;
  46 import java.nio.channels.ClosedChannelException;
  47 import java.nio.channels.DatagramChannel;
  48 import java.nio.channels.MembershipKey;
  49 import java.nio.channels.NotYetConnectedException;
  50 import java.nio.channels.SelectionKey;
  51 import java.nio.channels.spi.SelectorProvider;
  52 import java.util.Collections;
  53 import java.util.HashSet;
  54 import java.util.Objects;
  55 import java.util.Set;
  56 import java.util.concurrent.locks.ReentrantLock;
  57 
  58 import sun.net.ResourceManager;
  59 import sun.net.ext.ExtendedSocketOptions;
  60 import sun.net.util.IPAddressUtil;
  61 
  62 /**
  63  * An implementation of DatagramChannels.
  64  */
  65 
  66 class DatagramChannelImpl
  67     extends DatagramChannel
  68     implements SelChImpl
  69 {
  70     // Used to make native read and write calls
  71     private static NativeDispatcher nd = new DatagramDispatcher();
  72 
  73     // The protocol family of the socket
  74     private final ProtocolFamily family;
  75 
  76     // Our file descriptor
  77     private final FileDescriptor fd;
  78     private final int fdVal;
  79 
  80     // Cached InetAddress and port for unconnected DatagramChannels


 511         try {
 512             boolean blocking = isBlocking();
 513             int n = 0;
 514             try {
 515                 SocketAddress remote = beginWrite(blocking, false);
 516                 if (remote != null) {
 517                     // connected
 518                     if (!target.equals(remote)) {
 519                         throw new AlreadyConnectedException();
 520                     }
 521                     n = IOUtil.write(fd, src, -1, nd);
 522                     if (blocking) {
 523                         while (IOStatus.okayToRetry(n) && isOpen()) {
 524                             park(Net.POLLOUT);
 525                             n = IOUtil.write(fd, src, -1, nd);
 526                         }
 527                     }
 528                 } else {
 529                     // not connected
 530                     SecurityManager sm = System.getSecurityManager();
 531                     InetAddress ia = isa.getAddress();
 532                     if (sm != null) {

 533                         if (ia.isMulticastAddress()) {
 534                             sm.checkMulticast(ia);
 535                         } else {
 536                             sm.checkConnect(ia.getHostAddress(), isa.getPort());
 537                         }
 538                     }
 539                     if (ia.isLinkLocalAddress())
 540                         isa = IPAddressUtil.toScopedAddress(isa);
 541                     n = send(fd, src, isa);
 542                     if (blocking) {
 543                         while (IOStatus.okayToRetry(n) && isOpen()) {
 544                             park(Net.POLLOUT);
 545                             n = send(fd, src, isa);
 546                         }
 547                     }
 548                 }
 549             } finally {
 550                 endWrite(blocking, n > 0);
 551                 assert IOStatus.check(n);
 552             }
 553             return IOStatus.normalize(n);
 554         } finally {
 555             writeLock.unlock();
 556         }
 557     }
 558 
 559     private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target)
 560         throws IOException




  40 import java.net.StandardProtocolFamily;
  41 import java.net.StandardSocketOptions;
  42 import java.nio.ByteBuffer;
  43 import java.nio.channels.AlreadyBoundException;
  44 import java.nio.channels.AlreadyConnectedException;
  45 import java.nio.channels.AsynchronousCloseException;
  46 import java.nio.channels.ClosedChannelException;
  47 import java.nio.channels.DatagramChannel;
  48 import java.nio.channels.MembershipKey;
  49 import java.nio.channels.NotYetConnectedException;
  50 import java.nio.channels.SelectionKey;
  51 import java.nio.channels.spi.SelectorProvider;
  52 import java.util.Collections;
  53 import java.util.HashSet;
  54 import java.util.Objects;
  55 import java.util.Set;
  56 import java.util.concurrent.locks.ReentrantLock;
  57 
  58 import sun.net.ResourceManager;
  59 import sun.net.ext.ExtendedSocketOptions;

  60 
  61 /**
  62  * An implementation of DatagramChannels.
  63  */
  64 
  65 class DatagramChannelImpl
  66     extends DatagramChannel
  67     implements SelChImpl
  68 {
  69     // Used to make native read and write calls
  70     private static NativeDispatcher nd = new DatagramDispatcher();
  71 
  72     // The protocol family of the socket
  73     private final ProtocolFamily family;
  74 
  75     // Our file descriptor
  76     private final FileDescriptor fd;
  77     private final int fdVal;
  78 
  79     // Cached InetAddress and port for unconnected DatagramChannels


 510         try {
 511             boolean blocking = isBlocking();
 512             int n = 0;
 513             try {
 514                 SocketAddress remote = beginWrite(blocking, false);
 515                 if (remote != null) {
 516                     // connected
 517                     if (!target.equals(remote)) {
 518                         throw new AlreadyConnectedException();
 519                     }
 520                     n = IOUtil.write(fd, src, -1, nd);
 521                     if (blocking) {
 522                         while (IOStatus.okayToRetry(n) && isOpen()) {
 523                             park(Net.POLLOUT);
 524                             n = IOUtil.write(fd, src, -1, nd);
 525                         }
 526                     }
 527                 } else {
 528                     // not connected
 529                     SecurityManager sm = System.getSecurityManager();

 530                     if (sm != null) {
 531                         InetAddress ia = isa.getAddress();
 532                         if (ia.isMulticastAddress()) {
 533                             sm.checkMulticast(ia);
 534                         } else {
 535                             sm.checkConnect(ia.getHostAddress(), isa.getPort());
 536                         }
 537                     }


 538                     n = send(fd, src, isa);
 539                     if (blocking) {
 540                         while (IOStatus.okayToRetry(n) && isOpen()) {
 541                             park(Net.POLLOUT);
 542                             n = send(fd, src, isa);
 543                         }
 544                     }
 545                 }
 546             } finally {
 547                 endWrite(blocking, n > 0);
 548                 assert IOStatus.check(n);
 549             }
 550             return IOStatus.normalize(n);
 551         } finally {
 552             writeLock.unlock();
 553         }
 554     }
 555 
 556     private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target)
 557         throws IOException


< prev index next >