28 import java.io.FileDescriptor; 29 import java.util.Set; 30 import java.util.HashSet; 31 import sun.net.ext.ExtendedSocketOptions; 32 33 /* 34 * On Unix systems we simply delegate to native methods. 35 * 36 * @author Chris Hegarty 37 */ 38 39 class PlainSocketImpl extends AbstractPlainSocketImpl 40 { 41 static { 42 initProto(); 43 } 44 45 /** 46 * Constructs an empty instance. 47 */ 48 PlainSocketImpl() { } 49 50 /** 51 * Constructs an instance with the given file descriptor. 52 */ 53 PlainSocketImpl(FileDescriptor fd) { 54 this.fd = fd; 55 } 56 57 static final ExtendedSocketOptions extendedOptions = 58 ExtendedSocketOptions.getInstance(); 59 60 protected <T> void setOption(SocketOption<T> name, T value) throws IOException { 61 if (isClosedOrPending()) { 62 throw new SocketException("Socket closed"); 63 } 64 if (supportedOptions().contains(name)) { 65 if (extendedOptions.isOptionSupported(name)) { 66 extendedOptions.setOption(fd, name, value); 67 } else { 68 super.setOption(name, value); 69 } 70 } else { 71 throw new UnsupportedOperationException("unsupported option"); 72 } 73 } 74 75 @SuppressWarnings("unchecked") 76 protected <T> T getOption(SocketOption<T> name) throws IOException { 77 if (isClosedOrPending()) { 78 throw new SocketException("Socket closed"); 79 } 80 if (supportedOptions().contains(name)) { 81 if (extendedOptions.isOptionSupported(name)) { 82 return (T) extendedOptions.getOption(fd, name); 83 } else { 84 return super.getOption(name); 85 } 86 } else { 87 throw new UnsupportedOperationException("unsupported option"); 88 } 89 } 90 91 protected Set<SocketOption<?>> supportedOptions() { 92 HashSet<SocketOption<?>> options = new HashSet<>(super.supportedOptions()); 93 if (getServerSocket() != null) { 94 options.addAll(ExtendedSocketOptions.serverSocketOptions()); 95 } else { 96 options.addAll(ExtendedSocketOptions.clientSocketOptions()); 97 } 98 return options; 99 } 100 101 protected void socketSetOption(int opt, boolean b, Object val) throws SocketException { 102 if (opt == SocketOptions.SO_REUSEPORT && 103 !supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) { 104 throw new UnsupportedOperationException("unsupported option"); 105 } 106 try { 107 socketSetOption0(opt, b, val); 108 } catch (SocketException se) { 109 if (socket == null || !socket.isConnected()) 110 throw se; 111 } 112 } 113 114 native void socketCreate(boolean isServer) throws IOException; 115 116 native void socketConnect(InetAddress address, int port, int timeout) 117 throws IOException; 118 119 native void socketBind(InetAddress address, int port) 120 throws IOException; 121 122 native void socketListen(int count) throws IOException; 123 124 native void socketAccept(SocketImpl s) throws IOException; 125 126 native int socketAvailable() throws IOException; 127 128 native void socketClose0(boolean useDeferredClose) throws IOException; 129 130 native void socketShutdown(int howto) throws IOException; 131 132 static native void initProto(); 133 134 native void socketSetOption0(int cmd, boolean on, Object value) | 28 import java.io.FileDescriptor; 29 import java.util.Set; 30 import java.util.HashSet; 31 import sun.net.ext.ExtendedSocketOptions; 32 33 /* 34 * On Unix systems we simply delegate to native methods. 35 * 36 * @author Chris Hegarty 37 */ 38 39 class PlainSocketImpl extends AbstractPlainSocketImpl 40 { 41 static { 42 initProto(); 43 } 44 45 /** 46 * Constructs an empty instance. 47 */ 48 PlainSocketImpl(boolean server) { 49 super(server); 50 } 51 52 static final ExtendedSocketOptions extendedOptions = 53 ExtendedSocketOptions.getInstance(); 54 55 protected <T> void setOption(SocketOption<T> name, T value) throws IOException { 56 if (isClosedOrPending()) { 57 throw new SocketException("Socket closed"); 58 } 59 if (supportedOptions().contains(name)) { 60 if (extendedOptions.isOptionSupported(name)) { 61 extendedOptions.setOption(fd, name, value); 62 } else { 63 super.setOption(name, value); 64 } 65 } else { 66 throw new UnsupportedOperationException("unsupported option"); 67 } 68 } 69 70 @SuppressWarnings("unchecked") 71 protected <T> T getOption(SocketOption<T> name) throws IOException { 72 if (isClosedOrPending()) { 73 throw new SocketException("Socket closed"); 74 } 75 if (supportedOptions().contains(name)) { 76 if (extendedOptions.isOptionSupported(name)) { 77 return (T) extendedOptions.getOption(fd, name); 78 } else { 79 return super.getOption(name); 80 } 81 } else { 82 throw new UnsupportedOperationException("unsupported option"); 83 } 84 } 85 86 protected Set<SocketOption<?>> supportedOptions() { 87 HashSet<SocketOption<?>> options = new HashSet<>(super.supportedOptions()); 88 if (server) { 89 options.addAll(ExtendedSocketOptions.serverSocketOptions()); 90 } else { 91 options.addAll(ExtendedSocketOptions.clientSocketOptions()); 92 } 93 return options; 94 } 95 96 protected void socketSetOption(int opt, boolean b, Object val) throws SocketException { 97 if (opt == SocketOptions.SO_REUSEPORT && 98 !supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) { 99 throw new UnsupportedOperationException("unsupported option"); 100 } 101 try { 102 socketSetOption0(opt, b, val); 103 } catch (SocketException se) { 104 if (server || !connected) 105 throw se; 106 } 107 } 108 109 native void socketCreate(boolean stream, boolean isServer) throws IOException; 110 111 native void socketConnect(InetAddress address, int port, int timeout) 112 throws IOException; 113 114 native void socketBind(InetAddress address, int port) 115 throws IOException; 116 117 native void socketListen(int count) throws IOException; 118 119 native void socketAccept(SocketImpl s) throws IOException; 120 121 native int socketAvailable() throws IOException; 122 123 native void socketClose0(boolean useDeferredClose) throws IOException; 124 125 native void socketShutdown(int howto) throws IOException; 126 127 static native void initProto(); 128 129 native void socketSetOption0(int cmd, boolean on, Object value) |