< prev index next >

src/java.base/share/classes/java/net/SocketImpl.java

Print this page

        

*** 44,66 **** * @author unascribed * @since 1.0 */ public abstract class SocketImpl implements SocketOptions { /** ! * Creates an instance of platform's SocketImpl */ ! @SuppressWarnings("unchecked") ! static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) { ! return (S) new PlainSocketImpl(); } /** ! * The actual Socket object. */ ! Socket socket = null; ! ServerSocket serverSocket = null; /** * The file descriptor object for this socket. */ protected FileDescriptor fd; --- 44,79 ---- * @author unascribed * @since 1.0 */ public abstract class SocketImpl implements SocketOptions { + // true if this SocketImpl for a ServerSocket. By default, this field + // will be set to true. It can only be set to false by platform impls. + /*package*/ final boolean server; + /** ! * Creates a new SocketImpl. */ ! public SocketImpl() { ! this(true); ! } ! ! /** ! * Creates a new SocketImpl. ! * @param server true if this SocketImpl for a ServerSocket ! */ ! /*package*/ SocketImpl(boolean server) { ! this.server = server; } /** ! * Creates an instance of platform's SocketImpl */ ! @SuppressWarnings("unchecked") ! static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) { ! return (S) new PlainSocketImpl(server); ! } /** * The file descriptor object for this socket. */ protected FileDescriptor fd;
*** 298,323 **** */ protected int getLocalPort() { return localport; } - void setSocket(Socket soc) { - this.socket = soc; - } - - Socket getSocket() { - return socket; - } - - void setServerSocket(ServerSocket soc) { - this.serverSocket = soc; - } - - ServerSocket getServerSocket() { - return serverSocket; - } - /** * Returns the address and port of this socket as a {@code String}. * * @return a string representation of this socket. */ --- 311,320 ----
*** 393,422 **** * @throws IOException if an I/O error occurs, or if the socket is closed. * * @since 9 */ protected <T> void setOption(SocketOption<T> name, T value) throws IOException { ! if (name == StandardSocketOptions.SO_KEEPALIVE && ! (getSocket() != null)) { setOption(SocketOptions.SO_KEEPALIVE, value); ! } else if (name == StandardSocketOptions.SO_SNDBUF && ! (getSocket() != null)) { setOption(SocketOptions.SO_SNDBUF, value); } else if (name == StandardSocketOptions.SO_RCVBUF) { setOption(SocketOptions.SO_RCVBUF, value); } else if (name == StandardSocketOptions.SO_REUSEADDR) { setOption(SocketOptions.SO_REUSEADDR, value); } else if (name == StandardSocketOptions.SO_REUSEPORT && supportedOptions().contains(name)) { setOption(SocketOptions.SO_REUSEPORT, value); ! } else if (name == StandardSocketOptions.SO_LINGER && ! (getSocket() != null)) { setOption(SocketOptions.SO_LINGER, value); } else if (name == StandardSocketOptions.IP_TOS) { setOption(SocketOptions.IP_TOS, value); ! } else if (name == StandardSocketOptions.TCP_NODELAY && ! (getSocket() != null)) { setOption(SocketOptions.TCP_NODELAY, value); } else { throw new UnsupportedOperationException("unsupported option"); } } --- 390,415 ---- * @throws IOException if an I/O error occurs, or if the socket is closed. * * @since 9 */ protected <T> void setOption(SocketOption<T> name, T value) throws IOException { ! if (name == StandardSocketOptions.SO_KEEPALIVE && !server) { setOption(SocketOptions.SO_KEEPALIVE, value); ! } else if (name == StandardSocketOptions.SO_SNDBUF && !server) { setOption(SocketOptions.SO_SNDBUF, value); } else if (name == StandardSocketOptions.SO_RCVBUF) { setOption(SocketOptions.SO_RCVBUF, value); } else if (name == StandardSocketOptions.SO_REUSEADDR) { setOption(SocketOptions.SO_REUSEADDR, value); } else if (name == StandardSocketOptions.SO_REUSEPORT && supportedOptions().contains(name)) { setOption(SocketOptions.SO_REUSEPORT, value); ! } else if (name == StandardSocketOptions.SO_LINGER && !server) { setOption(SocketOptions.SO_LINGER, value); } else if (name == StandardSocketOptions.IP_TOS) { setOption(SocketOptions.IP_TOS, value); ! } else if (name == StandardSocketOptions.TCP_NODELAY && !server) { setOption(SocketOptions.TCP_NODELAY, value); } else { throw new UnsupportedOperationException("unsupported option"); } }
*** 436,465 **** * * @since 9 */ @SuppressWarnings("unchecked") protected <T> T getOption(SocketOption<T> name) throws IOException { ! if (name == StandardSocketOptions.SO_KEEPALIVE && ! (getSocket() != null)) { return (T)getOption(SocketOptions.SO_KEEPALIVE); ! } else if (name == StandardSocketOptions.SO_SNDBUF && ! (getSocket() != null)) { return (T)getOption(SocketOptions.SO_SNDBUF); } else if (name == StandardSocketOptions.SO_RCVBUF) { return (T)getOption(SocketOptions.SO_RCVBUF); } else if (name == StandardSocketOptions.SO_REUSEADDR) { return (T)getOption(SocketOptions.SO_REUSEADDR); } else if (name == StandardSocketOptions.SO_REUSEPORT && supportedOptions().contains(name)) { return (T)getOption(SocketOptions.SO_REUSEPORT); ! } else if (name == StandardSocketOptions.SO_LINGER && ! (getSocket() != null)) { return (T)getOption(SocketOptions.SO_LINGER); } else if (name == StandardSocketOptions.IP_TOS) { return (T)getOption(SocketOptions.IP_TOS); ! } else if (name == StandardSocketOptions.TCP_NODELAY && ! (getSocket() != null)) { return (T)getOption(SocketOptions.TCP_NODELAY); } else { throw new UnsupportedOperationException("unsupported option"); } } --- 429,454 ---- * * @since 9 */ @SuppressWarnings("unchecked") protected <T> T getOption(SocketOption<T> name) throws IOException { ! if (name == StandardSocketOptions.SO_KEEPALIVE && !server) { return (T)getOption(SocketOptions.SO_KEEPALIVE); ! } else if (name == StandardSocketOptions.SO_SNDBUF && !server) { return (T)getOption(SocketOptions.SO_SNDBUF); } else if (name == StandardSocketOptions.SO_RCVBUF) { return (T)getOption(SocketOptions.SO_RCVBUF); } else if (name == StandardSocketOptions.SO_REUSEADDR) { return (T)getOption(SocketOptions.SO_REUSEADDR); } else if (name == StandardSocketOptions.SO_REUSEPORT && supportedOptions().contains(name)) { return (T)getOption(SocketOptions.SO_REUSEPORT); ! } else if (name == StandardSocketOptions.SO_LINGER && !server) { return (T)getOption(SocketOptions.SO_LINGER); } else if (name == StandardSocketOptions.IP_TOS) { return (T)getOption(SocketOptions.IP_TOS); ! } else if (name == StandardSocketOptions.TCP_NODELAY && !server) { return (T)getOption(SocketOptions.TCP_NODELAY); } else { throw new UnsupportedOperationException("unsupported option"); } }
*** 502,513 **** * @return a Set of SocketOptions * * @since 9 */ protected Set<SocketOption<?>> supportedOptions() { ! if (getSocket() != null) { ! return socketOptions; ! } else { return serverSocketOptions; } } } --- 491,502 ---- * @return a Set of SocketOptions * * @since 9 */ protected Set<SocketOption<?>> supportedOptions() { ! if (server) { return serverSocketOptions; + } else { + return socketOptions; } } }
< prev index next >