src/java.base/unix/classes/sun/nio/ch/InheritedChannel.java
branchunixdomainchannels
changeset 58801 119ac9128c1b
parent 58295 7973073dd048
child 58847 692de65ab293
equal deleted inserted replaced
58799:eb491334113f 58801:119ac9128c1b
    26 package sun.nio.ch;
    26 package sun.nio.ch;
    27 
    27 
    28 import java.lang.reflect.Constructor;
    28 import java.lang.reflect.Constructor;
    29 import java.io.FileDescriptor;
    29 import java.io.FileDescriptor;
    30 import java.io.IOException;
    30 import java.io.IOException;
       
    31 import java.io.UncheckedIOException;
    31 import java.net.InetAddress;
    32 import java.net.InetAddress;
    32 import java.net.InetSocketAddress;
    33 import java.net.InetSocketAddress;
       
    34 import java.net.Socket;
       
    35 import java.net.SocketAddress;
       
    36 import java.net.SocketOption;
       
    37 import java.nio.ByteBuffer;
    33 import java.nio.channels.Channel;
    38 import java.nio.channels.Channel;
    34 import java.nio.channels.SocketChannel;
    39 import java.nio.channels.SocketChannel;
    35 import java.nio.channels.ServerSocketChannel;
    40 import java.nio.channels.ServerSocketChannel;
    36 import java.nio.channels.DatagramChannel;
    41 import java.nio.channels.DatagramChannel;
    37 import java.nio.channels.spi.SelectorProvider;
    42 import java.nio.channels.spi.SelectorProvider;
    38 
    43 import java.util.Set;
    39 class InheritedChannel {
    44 import java.util.function.BiFunction;
       
    45 
       
    46 public class InheritedChannel {
    40 
    47 
    41     // the "types" of socket returned by soType0
    48     // the "types" of socket returned by soType0
    42     private static final int UNKNOWN            = -1;
    49     private static final int UNKNOWN            = -1;
    43     private static final int SOCK_STREAM        = 1;
    50     private static final int SOCK_STREAM        = 1;
    44     private static final int SOCK_DGRAM         = 2;
    51     private static final int SOCK_DGRAM         = 2;
    77     /*
    84     /*
    78      * Override the implCloseSelectableChannel for each channel type - this
    85      * Override the implCloseSelectableChannel for each channel type - this
    79      * allows us to "detach" the standard streams after closing and ensures
    86      * allows us to "detach" the standard streams after closing and ensures
    80      * that the underlying socket really closes.
    87      * that the underlying socket really closes.
    81      */
    88      */
    82     public static class InheritedSocketChannelImpl extends SocketChannelImpl {
    89     public static class InheritedInetSocketChannelImpl extends InetSocketChannelImpl {
    83 
    90 
    84         InheritedSocketChannelImpl(SelectorProvider sp,
    91         InheritedInetSocketChannelImpl(SelectorProvider sp,
    85                                    FileDescriptor fd,
    92                                    FileDescriptor fd,
    86                                    InetSocketAddress remote)
    93                                    InetSocketAddress remote)
    87             throws IOException
    94             throws IOException
    88         {
    95         {
    89             super(sp, fd, remote);
    96             super(sp, fd, remote);
    93             super.implCloseSelectableChannel();
   100             super.implCloseSelectableChannel();
    94             detachIOStreams();
   101             detachIOStreams();
    95         }
   102         }
    96     }
   103     }
    97 
   104 
    98     public static class InheritedUnixChannelImpl extends UnixDomainSocketChannelImpl {
   105     public static class InheritedUnixSocketChannelImpl extends UnixDomainSocketChannelImpl {
    99 
   106 
   100         InheritedUnixChannelImpl(FileDescriptor fd)
   107         InheritedUnixSocketChannelImpl(SelectorProvider sp, FileDescriptor fd)
   101             throws IOException
   108             throws IOException
   102         {
   109         {
   103             super(fd);
   110             super(sp, fd, true);
   104         }
   111         }
   105 
   112 
   106         protected void implCloseSelectableChannel() throws IOException {
   113         @Override
   107             super.implCloseChannel();
   114         protected void implCloseSelectableChannel() throws IOException {
   108             detachIOStreams();
   115             super.localImplCloseSelectableChannel();
   109         }
   116             detachIOStreams();
   110     }
   117         }
   111 
   118     }
   112     public static class InheritedServerSocketChannelImpl extends
   119 
   113         ServerSocketChannelImpl {
   120     public static class InheritedInetServerSocketChannelImpl extends
   114 
   121         InetServerSocketChannelImpl {
   115         InheritedServerSocketChannelImpl(SelectorProvider sp,
   122 
       
   123         InheritedInetServerSocketChannelImpl(SelectorProvider sp,
   116                                          FileDescriptor fd)
   124                                          FileDescriptor fd)
   117             throws IOException
   125             throws IOException
   118         {
   126         {
   119             super(sp, fd, true);
   127             super(sp, fd, true);
   120         }
   128         }
   204             int family = addressFamily(fdVal);
   212             int family = addressFamily(fdVal);
   205             if (family == AF_UNKNOWN)
   213             if (family == AF_UNKNOWN)
   206                 return null;
   214                 return null;
   207             if (family == AF_UNIX) {
   215             if (family == AF_UNIX) {
   208                 if (isConnected(fdVal)) {
   216                 if (isConnected(fdVal)) {
   209                     return new InheritedUnixChannelImpl(fd);
   217                     return new InheritedUnixSocketChannelImpl(provider, fd);
   210                 } else {
   218                 } else {
   211                     // listener. unsupported.
   219                     // listener. unsupported.
   212                     return null;
   220                     return null;
   213                 }
   221                 }
   214             }
   222             }
   215             InetAddress ia = peerAddress0(fdVal);
   223             InetAddress ia = peerAddress0(fdVal);
   216             if (ia == null) {
   224             if (ia == null) {
   217                c = new InheritedServerSocketChannelImpl(provider, fd);
   225                c = new InheritedInetServerSocketChannelImpl(provider, fd);
   218             } else {
   226             } else {
   219                int port = peerPort0(fdVal);
   227                int port = peerPort0(fdVal);
   220                assert port > 0;
   228                assert port > 0;
   221                InetSocketAddress isa = new InetSocketAddress(ia, port);
   229                InetSocketAddress isa = new InetSocketAddress(ia, port);
   222                c = new InheritedSocketChannelImpl(provider, fd, isa);
   230                c = new InheritedInetSocketChannelImpl(provider, fd, isa);
   223             }
   231             }
   224         } else {
   232         } else {
   225             c = new InheritedDatagramChannelImpl(provider, fd);
   233             c = new InheritedDatagramChannelImpl(provider, fd);
   226         }
   234         }
   227         return c;
   235         return c;