java.net.StandardSocketOptions Java Examples

The following examples show how to use java.net.StandardSocketOptions. 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: NIOAcceptor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public NIOAcceptor(String name, String bindIp,int port, 
		FrontendConnectionFactory factory, NIOReactorPool reactorPool)
		throws IOException {
	super.setName(name);
	this.port = port;
	this.selector = Selector.open();
	this.serverChannel = ServerSocketChannel.open();
	this.serverChannel.configureBlocking(false);
	/** 设置TCP属性 */
	serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2);
	// backlog=100
	serverChannel.bind(new InetSocketAddress(bindIp, port), 100);
	this.serverChannel.register(selector, SelectionKey.OP_ACCEPT);
	this.factory = factory;
	this.reactorPool = reactorPool;
}
 
Example #2
Source File: AsynchronousSocketChannelImpl.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 #3
Source File: NioSocketImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T> T getOption(SocketOption<T> opt) throws IOException {
    if (!supportedOptions().contains(opt))
        throw new UnsupportedOperationException("'" + opt + "' not supported");
    synchronized (stateLock) {
        ensureOpen();
        if (opt == StandardSocketOptions.IP_TOS) {
            return (T) Net.getSocketOption(fd, family(), opt);
        } else if (opt == StandardSocketOptions.SO_REUSEADDR) {
            if (Net.useExclusiveBind()) {
                return (T) Boolean.valueOf(isReuseAddress);
            } else {
                return (T) Net.getSocketOption(fd, opt);
            }
        } else {
            // option does not need special handling
            return (T) Net.getSocketOption(fd, opt);
        }
    }
}
 
Example #4
Source File: AsynchronousSocketChannelImpl.java    From dragonwell8_jdk 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 #5
Source File: AioServerImpl.java    From tephra with MIT License 6 votes vote down vote up
@Override
public void listen(int thread, int port, AioServerListener listener) {
    this.port = port;
    this.listener = listener;
    try {
        channelGroup = AsynchronousChannelGroup.withFixedThreadPool(thread, Executors.defaultThreadFactory());
        serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup);
        serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        serverSocketChannel.bind(new InetSocketAddress(port));
        serverSocketChannel.accept(null, this);

        if (logger.isInfoEnable())
            logger.info("启动AIO监听[{}]服务。", port);
    } catch (IOException e) {
        logger.warn(e, "启动AIO监听[{}]服务时发生异常!", port);
    }
}
 
Example #6
Source File: JdpBroadcaster.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example #7
Source File: AsynchronousServerSocketChannelImpl.java    From openjdk-jdk9 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 #8
Source File: DatagramChannelSender.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void open() throws IOException {
    if (channel == null) {
        channel = DatagramChannel.open();

        if (maxSendBufferSize > 0) {
            channel.setOption(StandardSocketOptions.SO_SNDBUF, maxSendBufferSize);
            final int actualSendBufSize = channel.getOption(StandardSocketOptions.SO_SNDBUF);
            if (actualSendBufSize < maxSendBufferSize) {
                logger.warn("Attempted to set Socket Send Buffer Size to " + maxSendBufferSize
                        + " bytes but could only set to " + actualSendBufSize + "bytes. You may want to "
                        + "consider changing the Operating System's maximum receive buffer");
            }
        }
    }

    if (!channel.isConnected()) {
        channel.connect(new InetSocketAddress(InetAddress.getByName(host), port));
    }
}
 
Example #9
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_setOption() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    // There were problems in the past as the number used here was below the minimum for
    // some platforms (b/27821554). It was increased from 1024 to 4096.
    dc.setOption(StandardSocketOptions.SO_SNDBUF, 4096);

    // Assert that we can read back the option from the channel...
    assertEquals(4096, (int) dc.getOption(StandardSocketOptions.SO_SNDBUF));
    // ... and its socket adaptor.
    assertEquals(4096, dc.socket().getSendBufferSize());

    dc.close();
    try {
        dc.setOption(StandardSocketOptions.SO_SNDBUF, 4096);
        fail();
    } catch (ClosedChannelException expected) {
    }
}
 
Example #10
Source File: JdpBroadcaster.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example #11
Source File: AsynchronousServerSocketChannelImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <T> AsynchronousServerSocketChannel 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 (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 #12
Source File: FIXAcceptor.java    From parity with Apache License 2.0 6 votes vote down vote up
Session accept() {
    try {
        SocketChannel fix = serverChannel.accept();
        if (fix == null)
            return null;

        try {
            fix.setOption(StandardSocketOptions.TCP_NODELAY, true);
            fix.configureBlocking(false);

            return new Session(orderEntry, fix, config, instruments);
        } catch (IOException e1) {
            fix.close();

            return null;
        }
    } catch (IOException e2) {
        return null;
    }
}
 
Example #13
Source File: AsynchronousSocketChannelImpl.java    From jdk8u-jdk 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 #14
Source File: NioSocketImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> void setOption(SocketOption<T> opt, T value) throws IOException {
    if (!supportedOptions().contains(opt))
        throw new UnsupportedOperationException("'" + opt + "' not supported");
    if (!opt.type().isInstance(value))
        throw new IllegalArgumentException("Invalid value '" + value + "'");
    synchronized (stateLock) {
        ensureOpen();
        if (opt == StandardSocketOptions.IP_TOS) {
            // maps to IP_TOS or IPV6_TCLASS
            Net.setSocketOption(fd, family(), opt, value);
        } else if (opt == StandardSocketOptions.SO_REUSEADDR) {
            boolean b = (boolean) value;
            if (Net.useExclusiveBind()) {
                isReuseAddress = b;
            } else {
                Net.setSocketOption(fd, opt, b);
            }
        } else {
            // option does not need special handling
            Net.setSocketOption(fd, opt, value);
        }
    }
}
 
Example #15
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 #16
Source File: JdpBroadcaster.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example #17
Source File: AsynchronousServerSocketChannelImpl.java    From Bytecoder with Apache License 2.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 #18
Source File: JdpBroadcaster.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example #19
Source File: AsynchronousServerSocketChannelImpl.java    From jdk8u-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 #20
Source File: ChannelListener.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a server socket channel for listening to connections.
 *
 * @param nicIPAddress - if null binds to wildcard address
 * @param port - port to bind to
 * @param receiveBufferSize - size of OS receive buffer to request. If less
 * than 0 then will not be set and OS default will win.
 * @throws IOException if unable to add socket
 */
public void addServerSocket(final InetAddress nicIPAddress, final int port, final int receiveBufferSize)
        throws IOException {
    final ServerSocketChannel ssChannel = ServerSocketChannel.open();
    ssChannel.configureBlocking(false);
    if (receiveBufferSize > 0) {
        ssChannel.setOption(StandardSocketOptions.SO_RCVBUF, receiveBufferSize);
        final int actualReceiveBufSize = ssChannel.getOption(StandardSocketOptions.SO_RCVBUF);
        if (actualReceiveBufSize < receiveBufferSize) {
            LOGGER.warn(this + " attempted to set TCP Receive Buffer Size to "
                    + receiveBufferSize + " bytes but could only set to " + actualReceiveBufSize
                    + "bytes. You may want to consider changing the Operating System's "
                    + "maximum receive buffer");
        }
    }
    ssChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    ssChannel.bind(new InetSocketAddress(nicIPAddress, port));
    ssChannel.register(serverSocketSelector, SelectionKey.OP_ACCEPT);
}
 
Example #21
Source File: AsynchronousServerSocketChannelImpl.java    From jdk8u_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 #22
Source File: JdpBroadcaster.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example #23
Source File: AIOAcceptor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public AIOAcceptor(String name, String ip, int port,
		FrontendConnectionFactory factory, AsynchronousChannelGroup group)
		throws IOException {
	this.name = name;
	this.port = port;
	this.factory = factory;
	serverChannel = AsynchronousServerSocketChannel.open(group);
	/** 设置TCP属性 */
	serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2);
	// backlog=100
	serverChannel.bind(new InetSocketAddress(ip, port), 100);
}
 
Example #24
Source File: Initiator.java    From philadelphia with Apache License 2.0 5 votes vote down vote up
static Initiator open(SocketAddress address) throws IOException {
    SocketChannel channel = SocketChannel.open();

    channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
    channel.connect(address);
    channel.configureBlocking(false);

    return new Initiator(channel);
}
 
Example #25
Source File: ServerSocketAdaptor.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void setReceiveBufferSize(int size) throws SocketException {
    // size 0 valid for ServerSocketChannel, invalid for ServerSocket
    if (size <= 0)
        throw new IllegalArgumentException("size cannot be 0 or negative");
    try {
        ssc.setOption(StandardSocketOptions.SO_RCVBUF, size);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
Example #26
Source File: NioSendSystem.java    From andesite-node with MIT License 5 votes vote down vote up
public NioSendSystem(Vertx vertx, IPacketProvider packetProvider) {
    this.vertx = vertx;
    this.packetProvider = packetProvider;
    try {
        this.channel = DatagramChannel.open()
                .setOption(StandardSocketOptions.SO_REUSEADDR, true);
    } catch(IOException e) {
        throw new IllegalStateException("Unable to create UDP channel", e);
    }
}
 
Example #27
Source File: SocketOptionUtils.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
/**
 * Convert and add the given {@link SocketOption} and value to the {@link ChannelOption}s {@link Map}.
 *
 * @param channelOpts the {@link Map} into which add the converted {@link SocketOption}
 * @param option the {@link SocketOption} to convert and add
 * @param value the value to add
 * @param <T> the type of the {@link SocketOption} value
 * @throws IllegalArgumentException if the specified {@link SocketOption} is not supported
 */
@SuppressWarnings("rawtypes")
public static <T> void addOption(final Map<ChannelOption, Object> channelOpts, final SocketOption<T> option,
                                 final Object value) {
    if (option == StandardSocketOptions.IP_MULTICAST_IF) {
        channelOpts.put(ChannelOption.IP_MULTICAST_IF, value);
    } else if (option == StandardSocketOptions.IP_MULTICAST_LOOP) {
        channelOpts.put(ChannelOption.IP_MULTICAST_LOOP_DISABLED, !(Boolean) value);
    } else if (option == StandardSocketOptions.IP_MULTICAST_TTL) {
        channelOpts.put(ChannelOption.IP_MULTICAST_TTL, value);
    } else if (option == StandardSocketOptions.IP_TOS) {
        channelOpts.put(ChannelOption.IP_TOS, value);
    } else if (option == StandardSocketOptions.SO_BROADCAST) {
        channelOpts.put(ChannelOption.SO_BROADCAST, value);
    } else if (option == StandardSocketOptions.SO_KEEPALIVE) {
        channelOpts.put(ChannelOption.SO_KEEPALIVE, value);
    } else if (option == StandardSocketOptions.SO_LINGER) {
        channelOpts.put(ChannelOption.SO_LINGER, value);
    } else if (option == StandardSocketOptions.SO_RCVBUF) {
        channelOpts.put(ChannelOption.SO_RCVBUF, value);
    } else if (option == StandardSocketOptions.SO_REUSEADDR) {
        channelOpts.put(ChannelOption.SO_REUSEADDR, value);
    } else if (option == StandardSocketOptions.SO_SNDBUF) {
        channelOpts.put(ChannelOption.SO_SNDBUF, value);
    } else if (option == StandardSocketOptions.TCP_NODELAY) {
        channelOpts.put(ChannelOption.TCP_NODELAY, value);
    } else if (option == ServiceTalkSocketOptions.CONNECT_TIMEOUT) {
        channelOpts.put(ChannelOption.CONNECT_TIMEOUT_MILLIS, value);
    } else if (option == ServiceTalkSocketOptions.WRITE_BUFFER_THRESHOLD) {
        final int writeBufferThreshold = (Integer) value;
        channelOpts.put(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(writeBufferThreshold >>> 1,
                writeBufferThreshold));
    } else {
        throw unsupported(option);
    }
}
 
Example #28
Source File: AioSocketServer.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
public void setUpHandlers() {
	try {
		AsynchronousChannelGroup asyncChannelGroup = AsynchronousChannelGroup.withFixedThreadPool(poolSize, Executors.defaultThreadFactory());
		serverSocketChannel = AsynchronousServerSocketChannel.open(asyncChannelGroup).bind(new InetSocketAddress(listenPort));
		serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
	log.info("** " + poolSize + " handler thread has been setup! **");
	log.info("** Socket Server has been startup, listen port is " + listenPort + "! **");
}
 
Example #29
Source File: ServerSocketAdaptor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean getReuseAddress() throws SocketException {
    try {
        return ssc.getOption(StandardSocketOptions.SO_REUSEADDR).booleanValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return false;       // Never happens
    }
}
 
Example #30
Source File: AsynchronousSocketChannelImpl.java    From TencentKona-8 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);
}