Java Code Examples for java.net.StandardSocketOptions#SO_LINGER

The following examples show how to use java.net.StandardSocketOptions#SO_LINGER . 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: 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 2
Source File: NioSocketImpl.java    From Bytecoder with 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);
        }
    }
}