Example usage for io.netty.channel ChannelConfig getOption

List of usage examples for io.netty.channel ChannelConfig getOption

Introduction

In this page you can find the example usage for io.netty.channel ChannelConfig getOption.

Prototype

<T> T getOption(ChannelOption<T> option);

Source Link

Document

Return the value of the given ChannelOption

Usage

From source file:io.grpc.netty.Utils.java

License:Apache License

static InternalChannelz.SocketOptions getSocketOptions(Channel channel) {
    ChannelConfig config = channel.config();
    InternalChannelz.SocketOptions.Builder b = new InternalChannelz.SocketOptions.Builder();

    // The API allows returning null but not sure if it can happen in practice.
    // Let's be paranoid and do null checking just in case.
    Integer lingerSeconds = config.getOption(SO_LINGER);
    if (lingerSeconds != null) {
        b.setSocketOptionLingerSeconds(lingerSeconds);
    }//from  w  w w.j a v  a 2 s  .co  m

    Integer timeoutMillis = config.getOption(SO_TIMEOUT);
    if (timeoutMillis != null) {
        // in java, SO_TIMEOUT only applies to receiving
        b.setSocketOptionTimeoutMillis(timeoutMillis);
    }

    for (Entry<ChannelOption<?>, Object> opt : config.getOptions().entrySet()) {
        ChannelOption<?> key = opt.getKey();
        // Constants are pooled, so there should only be one instance of each constant
        if (key.equals(SO_LINGER) || key.equals(SO_TIMEOUT)) {
            continue;
        }
        Object value = opt.getValue();
        // zpencer: Can a netty option be null?
        b.addOption(key.name(), String.valueOf(value));
    }

    NativeSocketOptions nativeOptions = NettySocketSupport.getNativeSocketOptions(channel);
    if (nativeOptions != null) {
        b.setTcpInfo(nativeOptions.tcpInfo); // may be null
        for (Entry<String, String> entry : nativeOptions.otherInfo.entrySet()) {
            b.addOption(entry.getKey(), entry.getValue());
        }
    }
    return b.build();
}