Search in sources :

Example 1 with ChannelOption

use of io.netty.channel.ChannelOption in project netty by netty.

the class AbstractBootstrapConfig method toString.

@Override
public String toString() {
    StringBuilder buf = new StringBuilder().append(StringUtil.simpleClassName(this)).append('(');
    EventLoopGroup group = group();
    if (group != null) {
        buf.append("group: ").append(StringUtil.simpleClassName(group)).append(", ");
    }
    @SuppressWarnings("deprecation") ChannelFactory<? extends C> factory = channelFactory();
    if (factory != null) {
        buf.append("channelFactory: ").append(factory).append(", ");
    }
    SocketAddress localAddress = localAddress();
    if (localAddress != null) {
        buf.append("localAddress: ").append(localAddress).append(", ");
    }
    Map<ChannelOption<?>, Object> options = options();
    if (!options.isEmpty()) {
        buf.append("options: ").append(options).append(", ");
    }
    Map<AttributeKey<?>, Object> attrs = attrs();
    if (!attrs.isEmpty()) {
        buf.append("attrs: ").append(attrs).append(", ");
    }
    ChannelHandler handler = handler();
    if (handler != null) {
        buf.append("handler: ").append(handler).append(", ");
    }
    if (buf.charAt(buf.length() - 1) == '(') {
        buf.append(')');
    } else {
        buf.setCharAt(buf.length() - 2, ')');
        buf.setLength(buf.length() - 1);
    }
    return buf.toString();
}
Also used : AttributeKey(io.netty.util.AttributeKey) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelOption(io.netty.channel.ChannelOption) ChannelHandler(io.netty.channel.ChannelHandler) SocketAddress(java.net.SocketAddress)

Example 2 with ChannelOption

use of io.netty.channel.ChannelOption in project netty by netty.

the class Bootstrap method init.

@Override
@SuppressWarnings("unchecked")
void init(Channel channel) throws Exception {
    ChannelPipeline p = channel.pipeline();
    p.addLast(config.handler());
    final Map<ChannelOption<?>, Object> options = options0();
    synchronized (options) {
        setChannelOptions(channel, options, logger);
    }
    final Map<AttributeKey<?>, Object> attrs = attrs0();
    synchronized (attrs) {
        for (Entry<AttributeKey<?>, Object> e : attrs.entrySet()) {
            channel.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
        }
    }
}
Also used : AttributeKey(io.netty.util.AttributeKey) ChannelOption(io.netty.channel.ChannelOption) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 3 with ChannelOption

use of io.netty.channel.ChannelOption in project netty by netty.

the class ServerBootstrap method init.

@Override
void init(Channel channel) throws Exception {
    final Map<ChannelOption<?>, Object> options = options0();
    synchronized (options) {
        setChannelOptions(channel, options, logger);
    }
    final Map<AttributeKey<?>, Object> attrs = attrs0();
    synchronized (attrs) {
        for (Entry<AttributeKey<?>, Object> e : attrs.entrySet()) {
            @SuppressWarnings("unchecked") AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
            channel.attr(key).set(e.getValue());
        }
    }
    ChannelPipeline p = channel.pipeline();
    final EventLoopGroup currentChildGroup = childGroup;
    final ChannelHandler currentChildHandler = childHandler;
    final Entry<ChannelOption<?>, Object>[] currentChildOptions;
    final Entry<AttributeKey<?>, Object>[] currentChildAttrs;
    synchronized (childOptions) {
        currentChildOptions = childOptions.entrySet().toArray(newOptionArray(childOptions.size()));
    }
    synchronized (childAttrs) {
        currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(childAttrs.size()));
    }
    p.addLast(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(final Channel ch) throws Exception {
            final ChannelPipeline pipeline = ch.pipeline();
            ChannelHandler handler = config.handler();
            if (handler != null) {
                pipeline.addLast(handler);
            }
            // We add this handler via the EventLoop as the user may have used a ChannelInitializer as handler.
            // In this case the initChannel(...) method will only be called after this method returns. Because
            // of this we need to ensure we add our handler in a delayed fashion so all the users handler are
            // placed in front of the ServerBootstrapAcceptor.
            ch.eventLoop().execute(new Runnable() {

                @Override
                public void run() {
                    pipeline.addLast(new ServerBootstrapAcceptor(ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
                }
            });
        }
    });
}
Also used : ChannelOption(io.netty.channel.ChannelOption) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) AttributeKey(io.netty.util.AttributeKey) Entry(java.util.Map.Entry) EventLoopGroup(io.netty.channel.EventLoopGroup)

Example 4 with ChannelOption

use of io.netty.channel.ChannelOption in project grpc-java by grpc.

the class NettyClientTransport method start.

@SuppressWarnings("unchecked")
@Override
public Runnable start(Listener transportListener) {
    lifecycleManager = new ClientTransportLifecycleManager(Preconditions.checkNotNull(transportListener, "listener"));
    handler = newHandler();
    HandlerSettings.setAutoWindow(handler);
    negotiationHandler = negotiator.newHandler(handler);
    Bootstrap b = new Bootstrap();
    b.group(group);
    b.channel(channelType);
    if (NioSocketChannel.class.isAssignableFrom(channelType)) {
        b.option(SO_KEEPALIVE, true);
    }
    for (Map.Entry<ChannelOption<?>, ?> entry : channelOptions.entrySet()) {
        // Every entry in the map is obtained from
        // NettyChannelBuilder#withOption(ChannelOption<T> option, T value)
        // so it is safe to pass the key-value pair to b.option().
        b.option((ChannelOption<Object>) entry.getKey(), entry.getValue());
    }
    /**
     * We don't use a ChannelInitializer in the client bootstrap because its "initChannel" method
     * is executed in the event loop and we need this handler to be in the pipeline immediately so
     * that it may begin buffering writes.
     */
    b.handler(negotiationHandler);
    channel = b.register().channel();
    // Start the connection operation to the server.
    channel.connect(address).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                ChannelHandlerContext ctx = future.channel().pipeline().context(handler);
                if (ctx != null) {
                    // NettyClientHandler doesn't propagate exceptions, but the negotiator will need the
                    // exception to fail any writes. Note that this fires after handler, because it is as if
                    // handler was propagating the notification.
                    ctx.fireExceptionCaught(future.cause());
                }
                future.channel().pipeline().fireExceptionCaught(future.cause());
            }
        }
    });
    // Start the write queue as soon as the channel is constructed
    handler.startWriteQueue(channel);
    // This write will have no effect, yet it will only complete once the negotiationHandler
    // flushes any pending writes.
    channel.write(NettyClientHandler.NOOP_MESSAGE).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                // Need to notify of this failure, because NettyClientHandler may not have been added to
                // the pipeline before the error occurred.
                lifecycleManager.notifyTerminated(Utils.statusFromThrowable(future.cause()));
            }
        }
    });
    // Handle transport shutdown when the channel is closed.
    channel.closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            // Typically we should have noticed shutdown before this point.
            lifecycleManager.notifyTerminated(Status.INTERNAL.withDescription("Connection closed with unknown cause"));
        }
    });
    if (enableKeepAlive) {
        keepAliveManager = new KeepAliveManager(this, channel.eventLoop(), keepAliveDelayNanos, keepAliveTimeoutNanos, false);
        keepAliveManager.onTransportStarted();
    }
    return null;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOption(io.netty.channel.ChannelOption) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) KeepAliveManager(io.grpc.internal.KeepAliveManager) Map(java.util.Map) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Http2ChannelClosedException(io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 5 with ChannelOption

use of io.netty.channel.ChannelOption in project grpc-java by grpc.

the class NettyClientTransportTest method setSoLingerChannelOption.

@Test
public void setSoLingerChannelOption() throws IOException {
    startServer();
    Map<ChannelOption<?>, Object> channelOptions = new HashMap<ChannelOption<?>, Object>();
    // set SO_LINGER option
    int soLinger = 123;
    channelOptions.put(ChannelOption.SO_LINGER, soLinger);
    NettyClientTransport transport = new NettyClientTransport(address, NioSocketChannel.class, channelOptions, group, newNegotiator(), DEFAULT_WINDOW_SIZE, DEFAULT_MAX_MESSAGE_SIZE, GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, authority, null);
    transports.add(transport);
    callMeMaybe(transport.start(clientTransportListener));
    // verify SO_LINGER has been set
    ChannelConfig config = transport.channel().config();
    assertTrue(config instanceof SocketChannelConfig);
    assertEquals(soLinger, ((SocketChannelConfig) config).getSoLinger());
}
Also used : ChannelOption(io.netty.channel.ChannelOption) HashMap(java.util.HashMap) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) ChannelConfig(io.netty.channel.ChannelConfig) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) Test(org.junit.Test)

Aggregations

ChannelOption (io.netty.channel.ChannelOption)6 AttributeKey (io.netty.util.AttributeKey)4 ChannelHandler (io.netty.channel.ChannelHandler)3 EventLoopGroup (io.netty.channel.EventLoopGroup)3 ChannelPipeline (io.netty.channel.ChannelPipeline)2 KeepAliveManager (io.grpc.internal.KeepAliveManager)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelConfig (io.netty.channel.ChannelConfig)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ServerChannel (io.netty.channel.ServerChannel)1 SocketChannelConfig (io.netty.channel.socket.SocketChannelConfig)1 Http2ChannelClosedException (io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException)1 SocketAddress (java.net.SocketAddress)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1