java.net.SocketOption Java Examples

The following examples show how to use java.net.SocketOption. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtendedSocketOptions.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getOption(FileDescriptor fd,
                        SocketOption<?> option)
    throws SocketException
{
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
        sm.checkPermission(new NetworkPermission("getOption." + option.name()));

    if (fd == null || !fd.valid())
        throw new SocketException("socket closed");

    if (option == SO_FLOW_SLA) {
        assert flowSupported;
        SocketFlow flow = SocketFlow.create();
        getFlowOption(fd, flow);
        return flow;
    } else {
        throw new InternalError("Unexpected option " + option);
    }
}
 
Example #2
Source File: AsynchronousSocketChannelImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <T> AsynchronousSocketChannel setOption(SocketOption<T> name, T value)
    throws IOException
{
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (writeShutdown)
            throw new IOException("Connection has been shutdown for writing");
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            isReuseAddress = (Boolean)value;
        } else {
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
        }
        return this;
    } finally {
        end();
    }
}
 
Example #3
Source File: AsynchronousServerSocketChannelImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }
        return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    } finally {
        end();
    }
}
 
Example #4
Source File: AsynchronousServerSocketChannelImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }
        return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    } finally {
        end();
    }
}
 
Example #5
Source File: AsynchronousServerSocketChannelImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }
        return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    } finally {
        end();
    }
}
 
Example #6
Source File: ExtendedSocketOptions.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
protected ExtendedSocketOptions(Set<SocketOption<?>> options) {
    this.options = options;
    var datagramOptions = new HashSet<SocketOption<?>>();
    var serverStreamOptions = new HashSet<SocketOption<?>>();
    var clientStreamOptions = new HashSet<SocketOption<?>>();
    for (var option : options) {
        if (isDatagramOption(option)) {
            datagramOptions.add(option);
        }
        if (isStreamOption(option, true)) {
            serverStreamOptions.add(option);
        }
        if (isStreamOption(option, false)) {
            clientStreamOptions.add(option);
        }
    }
    this.datagramOptions = Set.copyOf(datagramOptions);
    this.serverStreamOptions = Set.copyOf(serverStreamOptions);
    this.clientStreamOptions = Set.copyOf(clientStreamOptions);
}
 
Example #7
Source File: AsynchronousSocketChannelImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <T> AsynchronousSocketChannel setOption(SocketOption<T> name, T value)
    throws IOException
{
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (writeShutdown)
            throw new IOException("Connection has been shutdown for writing");
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            isReuseAddress = (Boolean)value;
        } else {
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
        }
        return this;
    } finally {
        end();
    }
}
 
Example #8
Source File: AsynchronousServerSocketChannelImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }
        return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    } finally {
        end();
    }
}
 
Example #9
Source File: DatagramSocketAdaptor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void setBooleanOption(SocketOption<Boolean> name, boolean value)
    throws SocketException
{
    try {
        dc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
Example #10
Source File: DatagramSocketAdaptor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void setIntOption(SocketOption<Integer> name, int value)
    throws SocketException
{
    try {
        dc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
Example #11
Source File: AsynchronousSocketChannelImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(5);
    set.add(StandardSocketOptions.SO_SNDBUF);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_KEEPALIVE);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    set.add(StandardSocketOptions.TCP_NODELAY);
    if (ExtendedOptionsImpl.flowSupported()) {
        set.add(jdk.net.ExtendedSocketOptions.SO_FLOW_SLA);
    }
    return Collections.unmodifiableSet(set);
}
 
Example #12
Source File: SocketAdaptor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void setIntOption(SocketOption<Integer> name, int value)
    throws SocketException
{
    try {
        sc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
Example #13
Source File: AsynchronousSocketChannelImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<>(5);
    set.add(StandardSocketOptions.SO_SNDBUF);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_KEEPALIVE);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    if (Net.isReusePortAvailable()) {
        set.add(StandardSocketOptions.SO_REUSEPORT);
    }
    set.add(StandardSocketOptions.TCP_NODELAY);
    ExtendedSocketOptions extendedOptions =
            ExtendedSocketOptions.getInstance();
    set.addAll(extendedOptions.options());
    return Collections.unmodifiableSet(set);
}
 
Example #14
Source File: AsynchronousSocketChannelImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(5);
    set.add(StandardSocketOptions.SO_SNDBUF);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_KEEPALIVE);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    set.add(StandardSocketOptions.TCP_NODELAY);
    if (ExtendedOptionsImpl.flowSupported()) {
        set.add(jdk.net.ExtendedSocketOptions.SO_FLOW_SLA);
    }
    return Collections.unmodifiableSet(set);
}
 
Example #15
Source File: SocketAdaptor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private int getIntOption(SocketOption<Integer> name) throws SocketException {
    try {
        return sc.getOption(name).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // keep compiler happy
    }
}
 
Example #16
Source File: SocketAdaptor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private int getIntOption(SocketOption<Integer> name) throws SocketException {
    try {
        return sc.getOption(name).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // keep compiler happy
    }
}
 
Example #17
Source File: DatagramSocketAdaptor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void setBooleanOption(SocketOption<Boolean> name, boolean value)
    throws SocketException
{
    try {
        dc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
Example #18
Source File: AsynchronousServerSocketChannelImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<>(2);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    if (Net.isReusePortAvailable()) {
        set.add(StandardSocketOptions.SO_REUSEPORT);
    }
    set.addAll(ExtendedSocketOptions.serverSocketOptions());
    return Collections.unmodifiableSet(set);
}
 
Example #19
Source File: DatagramSocketAdaptor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private int getIntOption(SocketOption<Integer> name) throws SocketException {
    try {
        return dc.getOption(name).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // keep compiler happy
    }
}
 
Example #20
Source File: ReusePortSocketOptionHolder.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public static ReusePortSocketOptionHolder create(int socketCount) {
    SocketOption reusePortSocketOption = getReusePortSocketOption();
    if (reusePortSocketOption == null) {
        LOGGER.warn("Failed to get ReusePort SocketOption. Please use Jvm9+ for using ReusePort SocketOption");
        return null;
    }

    return new ReusePortSocketOptionHolder(reusePortSocketOption, true, socketCount);
}
 
Example #21
Source File: SocketAdaptor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
    try {
        return sc.getOption(name).booleanValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return false;       // keep compiler happy
    }
}
 
Example #22
Source File: ExtendedSocketOptions.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private Set<SocketOption<?>> options0(short type, boolean server) {
    switch (type) {
        case SOCK_DGRAM:
            return datagramOptions;
        case SOCK_STREAM:
            if (server) {
                return serverStreamOptions;
            } else {
                return clientStreamOptions;
            }
        default:
            //this will never happen
            throw new IllegalArgumentException("Invalid socket option type");
    }
}
 
Example #23
Source File: AsynchronousSocketChannelImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<>(5);
    set.add(StandardSocketOptions.SO_SNDBUF);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_KEEPALIVE);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    if (Net.isReusePortAvailable()) {
        set.add(StandardSocketOptions.SO_REUSEPORT);
    }
    set.add(StandardSocketOptions.TCP_NODELAY);
    set.addAll(ExtendedSocketOptions.clientSocketOptions());
    return Collections.unmodifiableSet(set);
}
 
Example #24
Source File: DatagramSocketAdaptor.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setIntOption(SocketOption<Integer> name, int value)
    throws SocketException
{
    try {
        dc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
Example #25
Source File: DatagramSocketAdaptor.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private int getIntOption(SocketOption<Integer> name) throws SocketException {
    try {
        return dc.getOption(name).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // keep compiler happy
    }
}
 
Example #26
Source File: SSLServerSocketChannel.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T> T getOption(SocketOption<T> arg0) throws IOException {
	// TODO Auto-generated method stub
	return null;
}
 
Example #27
Source File: DefaultGrpcClientBuilder.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Override
public <T> GrpcClientBuilder<U, R> socketOption(final SocketOption<T> option, final T value) {
    httpClientBuilder.socketOption(option, value);
    return this;
}
 
Example #28
Source File: MockSocketChannel.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T getOption(SocketOption<T> name) throws IOException {
    return null;
}
 
Example #29
Source File: ServerSocketChannelEmul.java    From jmeter-plugins with Apache License 2.0 4 votes vote down vote up
@Override
public Set<SocketOption<?>> supportedOptions() {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example #30
Source File: ExtendedSocketOptions.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static Set<SocketOption<?>> options() {
    if (flowSupported)
        return Set.of(SO_FLOW_SLA);
    else
        return Collections.<SocketOption<?>>emptySet();
}