Java Code Examples for java.net.StandardSocketOptions

The following examples show how to use java.net.StandardSocketOptions. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: tephra   Source File: AioServerImpl.java    License: 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 2
@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
@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 4
@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 Project: parity   Source File: FIXAcceptor.java    License: 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 6
@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 7
@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
@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 Project: j2objc   Source File: DatagramChannelTest.java    License: 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 Project: Bytecoder   Source File: NioSocketImpl.java    License: 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 11
@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 12
Source Project: hottub   Source File: JdpBroadcaster.java    License: 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 13
@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 14
/**
 * 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 15
Source Project: Bytecoder   Source File: NioSocketImpl.java    License: 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 16
Source Project: Mycat2   Source File: NIOAcceptor.java    License: 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 17
Source Project: openjdk-jdk8u   Source File: JdpBroadcaster.java    License: 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 18
Source Project: jdk8u-jdk   Source File: JdpBroadcaster.java    License: 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 Project: nifi   Source File: ChannelListener.java    License: 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 20
@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 21
Source Project: nifi   Source File: DatagramChannelSender.java    License: 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 22
Source Project: jdk8u-jdk   Source File: JdpBroadcaster.java    License: 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 Project: Bytecoder   Source File: NioSocketImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Closes the socket. If there are I/O operations in progress then the
 * socket is pre-closed and the threads are signalled. The socket will be
 * closed when the last I/O operation aborts.
 */
@Override
protected void close() throws IOException {
    synchronized (stateLock) {
        int state = this.state;
        if (state >= ST_CLOSING)
            return;
        if (state == ST_NEW) {
            // stillborn
            this.state = ST_CLOSED;
            return;
        }
        this.state = ST_CLOSING;

        // shutdown output when linger interval not set to 0
        try {
            var SO_LINGER = StandardSocketOptions.SO_LINGER;
            if ((int) Net.getSocketOption(fd, SO_LINGER) != 0) {
                Net.shutdown(fd, Net.SHUT_WR);
            }
        } catch (IOException ignore) { }

        // attempt to close the socket. If there are I/O operations in progress
        // then the socket is pre-closed and the thread(s) signalled. The
        // last thread will close the file descriptor.
        if (!tryClose()) {
            nd.preClose(fd);
            long reader = readerThread;
            if (reader != 0)
                NativeThread.signal(reader);
            long writer = writerThread;
            if (writer != 0)
                NativeThread.signal(writer);
        }
    }
}
 
Example 24
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);
    return Collections.unmodifiableSet(set);
}
 
Example 25
public int getReceiveBufferSize() throws SocketException {
    try {
        return ssc.getOption(StandardSocketOptions.SO_RCVBUF).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // Never happens
    }
}
 
Example 26
Source Project: Bytecoder   Source File: NioSocketImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Set<SocketOption<?>> supportedOptions() {
    Set<SocketOption<?>> options = (server) ? serverSocketOptions : clientSocketOptions;
    if (options == null) {
        options = new HashSet<>();
        options.add(StandardSocketOptions.SO_RCVBUF);
        options.add(StandardSocketOptions.SO_REUSEADDR);
        if (server) {
            // IP_TOS added for server socket to maintain compatibility
            options.add(StandardSocketOptions.IP_TOS);
            options.addAll(ExtendedSocketOptions.serverSocketOptions());
        } else {
            options.add(StandardSocketOptions.IP_TOS);
            options.add(StandardSocketOptions.SO_KEEPALIVE);
            options.add(StandardSocketOptions.SO_SNDBUF);
            options.add(StandardSocketOptions.SO_LINGER);
            options.add(StandardSocketOptions.TCP_NODELAY);
            options.addAll(ExtendedSocketOptions.clientSocketOptions());
        }
        if (Net.isReusePortAvailable())
            options.add(StandardSocketOptions.SO_REUSEPORT);
        options = Collections.unmodifiableSet(options);
        if (server) {
            serverSocketOptions = options;
        } else {
            clientSocketOptions = options;
        }
    }
    return options;
}
 
Example 27
Source Project: netcrusher-java   Source File: TcpBulkServer.java    License: Apache License 2.0 5 votes vote down vote up
public TcpBulkServer(InetSocketAddress address, long limit) throws IOException {
    this.serverSocketChannel = ServerSocketChannel.open();
    this.serverSocketChannel.configureBlocking(true);
    this.serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    this.serverSocketChannel.bind(address);

    this.acceptor = new Acceptor(serverSocketChannel, limit);
}
 
Example 28
Source Project: dble   Source File: NIOAcceptor.java    License: GNU General Public License v2.0 5 votes vote down vote up
public NIOAcceptor(String name, String bindIp, int port, int backlog, FrontendConnectionFactory factory,
                   NIOReactorPool reactorPool) throws IOException {
    super.setName(name);
    this.port = port;
    this.selector = Selector.open();
    this.serverChannel = ServerSocketChannel.open();
    this.serverChannel.configureBlocking(false);
    //set TCP option
    serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2);
    serverChannel.bind(new InetSocketAddress(bindIp, port), backlog);
    this.serverChannel.register(selector, SelectionKey.OP_ACCEPT);
    this.factory = factory;
    this.reactorPool = reactorPool;
}
 
Example 29
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
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);
    }
}